im making a basic chat program. well, not even chat. just to understand winsock.
here's my code. It's not working
For the server...
Private Sub cmdConnect_Click()
Winsock1.LocalPort = 1234
Winsock1.Listen
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim TempData As String
Winsock1.GetData TempData
MsgBox TempData
End Sub
For the client
Private Sub cmdConnect_Click()
Winsock1.Connect
End Sub
Private Sub cmdSend_Click()
Dim data As String
data = "Testing 123"
Winsock1.SendData data
End Sub
Private Sub Form_Load()
Winsock1.RemoteHost = "127.0.0.1"
Winsock1.RemotePort = 1234
End Sub
I run it and click connect, and try sending, but it says
QuoteWrong protocal or connection state for the requested transaction or request
Make sure to close the winsock before tryinhg to connect.
~-~(HDX)~-~
On which? Client or server?
I have it for the server..
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
and if i need it for the client, where do i put it? and would it still be
Winsock1.close
?
Private Sub cmdConnect_Click()
<~~~~poke
Winsock1.Connect
End Sub
~-~(HDX)~-~
Im assuming it's because you are not selecting the protocol either udp or tcp/ip. This can be done on the form load.
Winsock in VB defults to TCP\IP upon adding the object. But that could be the problem
~-~(HDX)~-~
Private Sub cmdConnect_Click()
Winsock1.Close
Winsock1.Connect
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub
why is your server closeing the connection befor accepting it????? Thats like hangeing up the phone befor saying hello.
First it's listening, so it has to stop listening, then accept the connection.
~-~(HDX)~-~
Why not set a conditional to check the connection state before sending? That way you can see if you're even connected at all.
How do I do that?
winsock.State
sckConnected
~-~(HDX)~-~
you are probably going to want to use an array of sockets. Otherwise you can only have one connection to the server.
Where would I put
winsock.State
sckConnected
Quote from: Nodda4you on May 18, 2005, 08:07 PM
How do I do that?
put this in a timer
Select Case socket.State
Case 0
form.Caption = "Closed"
Case 1
form.Caption = "Open"
Case 2
form.Caption = "Listening"
Case 3
form.Caption = "Connection pending"
Case 4
form.Caption = "Resolving host"
Case 5
form.Caption = "Host resolved"
Case 6
form.Caption = "Connecting"
Case 7
form.Caption = "Connected"
Case 8
form.Caption = "Peer is closing the connection"
Case 9
form.Caption = "Error"
End Select
How would I get
Select Case socket.State
Case 0
form.Caption = "Closed"
Case 1
form.Caption = "Open"
Case 2
form.Caption = "Listening"
Case 3
form.Caption = "Connection pending"
Case 4
form.Caption = "Resolving host"
Case 5
form.Caption = "Host resolved"
Case 6
form.Caption = "Connecting"
Case 7
form.Caption = "Connected"
Case 8
form.Caption = "Peer is closing the connection"
Case 9
form.Caption = "Error"
End Select
to work?
you exicute it. Replace Socket with your winsock control. All that does is change the forms caption to what state it is in.
~-~(HDX)~-~
Quote from: Nodda4you on May 18, 2005, 08:35 PM
How would I get
Select Case socket.State
Case 0
form.Caption = "Closed"
Case 1
form.Caption = "Open"
Case 2
form.Caption = "Listening"
Case 3
form.Caption = "Connection pending"
Case 4
form.Caption = "Resolving host"
Case 5
form.Caption = "Host resolved"
Case 6
form.Caption = "Connecting"
Case 7
form.Caption = "Connected"
Case 8
form.Caption = "Peer is closing the connection"
Case 9
form.Caption = "Error"
End Select
to work?
add a timer to your form
double click the timer and add that code
replace form with the name of your form and replace socket with the name of your winsock.
also http://msdn.microsoft.com/library/default.asp?url=/library/en-us/VBRef98/html/vbmscLROverview.asp should be of some help (look to the menu at the left)
Quote from: Nodda4you on May 18, 2005, 03:31 PM
For the client
Private Sub cmdConnect_Click()
Winsock1.Connect
End Sub
Also you need to provide it with a server, port to connect to
Ex:
Private sub cmdConnect_Click()
Socket.Close: Socket.Connect SERVERHERE, PORTHERE
end sub
The Socket.Close just makes sure the winsock closes before you try to connect again. Without it if you hit connect twice your program would error. Goodluck.