Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB winsock client won't connect the server.

Status
Not open for further replies.

fechen

Technical User
Jan 25, 2002
48
0
0
US
Hi, guys,

I am real-new to winsock. I am experimenting the following VB winsocket program. The problem is the client socket never gets to sckConnected state, always in sckConnecting. So .sendData() always give out "wrong protocol or connection state for the requested transaction or request."

I would believe my server side is fine. I have a Java socket program that can send messages to and recieve messages from my VB socket server with no error. I can post the server code if needed.

Please help me out. Really frustrated. Thanks a lot in advance.


=======================================================
Private Sub Form_Load()
clientSocket.RemoteHost = "localhost"
clientSocket.RemotePort = 6000
End Sub

Private Sub SendBtn_Click()
clientSocket.Connect
'doesn't work even if I put wait here.
clientSocket.SendData "Test message from VB client"
clientSocket.Close
End Sub

 
silly questions but is your server app listening and if so is it accepting a request to connect??

server code, might be helpful to post!!

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Here is server code. As I said in last post, the code works fine with my Java socket program.

====================================================
Private Sub ExitBtn_Click()
serversock.Close
End
End Sub

Private Sub Form_Load()
serversock.LocalPort = 6000
serversock.Listen
appendText "Listening ..."
End Sub

Private Sub serversock_Close()
serversock.Close
serversock.Listen
End Sub

Private Sub serversock_ConnectionRequest _
(ByVal requestID As Long)
If serversock.State <> sckClosed Then serversock.Close
serversock.Accept requestID
End Sub

Private Sub serversock_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
serversock.GetData strData
appendText strData
serversock.SendData &quot;OK&quot;
End Sub

Private Sub appendText(txt As String)
If RecvText.Text = &quot;&quot; Then
RecvText.Text = txt
Else
RecvText.Text = RecvText.Text + vbCr + vbLf + txt
End If

RecvText.SelStart = Len(RecvText.Text)
End Sub
 
Have you tried waiting for the Connect event to fire before sending the data - i.e. putting SendData in the Connect event's procedure? You said &quot;'doesn't work even if I put wait here&quot;. Perhaps your &quot;wait&quot; method is halting event processing and your program isn't getting a chance to finish the connection.
 
at first glance it looks fine...

i would stick a wait in after the connection request (i realise you said you had done this)

Private Sub SendBtn_Click()
clientSocket.Connect

do
doevents
loop until clientSocket.state=sckConnected

clientSocket.SendData &quot;Test message from VB client&quot;
clientSocket.Close
End Sub

then stick a breakpoint on the server side connection request code and see if it is accepting

if that doesnt turn anything up (which is more than likely) im as stumped as you...

1 question are you using TCP or UDP??

good luck

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Thanks a lot for suggestions.

I am using TCP.

I tried both suggestions, tried doevent, tried _connect() method. No luck so far.

My Java socket program triggers the _ConnectionRequest() on server side, as expected.

I am really not sure what's wrong. Maybe my winsock installation is bad.

Can any of you guys give me a working client sample? Mabye I can find the difference by comparing the code.


 
In MSDN server side code sample, actually it is not a practical example. The problem is: it will not listen to 2nd (and later) connect request. I have to dig into the documentation to find the _close event.

And my client is the imitation of its client sample.

Sorry, I am killing one clue. :)
Microsoft technical writers sometimes just point the wrong way for new-comer. I have seen that several times in MSDN.

Anyway, I'll dig into Hope I can find something if I missed it previously

any other thoughts?
 
You're not trying to set up multiple, simultaneous TCP connections to the same port are you?
 
this is based on the MSDN code on TCP using winsock.

client has 2 commands, connect and disconnect and 2 text boxes
server only has disconnect button and 2 text boxes

client code:
Private Sub Form_Load()

tcpClient.RemoteHost = &quot;yourhost&quot;
tcpClient.RemotePort = 1001

End Sub

'''''''''''''''''''''''
'forms controls events'
'''''''''''''''''''''''
Private Sub cmdConnect_Click()

If Not tcpClient.State = sckConnected Then
tcpClient.Connect
End If

End Sub

Private Sub txtSendData_Change()

If tcpClient.State = sckConnected Then
tcpClient.SendData txtSendData.Text
Else
MsgBox &quot;Socket not open&quot;, vbOKOnly, &quot;Error&quot;
End If

End Sub

Private Sub cmdDisconnect_Click()
tcpClient.Close
End Sub

'''''''''''''''''
'tcpClientEvents'
'''''''''''''''''
Private Sub tcpClient_Close()
tcpClient.Close
End Sub

Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long)

Dim strData As String
tcpClient.GetData strData
txtOutput.Text = strData

End Sub

server code:
Private Sub Form_Load()

tcpServer.LocalPort = 1001
tcpServer.Listen
frmClient.Show

End Sub

'''''''''''''''''''''''
'forms controls events'
'''''''''''''''''''''''
Private Sub cmdDisconnect_Click()

tcpServer.Close
tcpServer.Listen

End Sub

Private Sub txtSendData_Change()

If tcpServer.State = sckConnected Then
tcpServer.SendData txtSendData.Text
Else
MsgBox &quot;Socket not open&quot;, vbOKOnly, &quot;Error&quot;
End If

End Sub

'''''''''''''''''
'tcpServerEvents'
'''''''''''''''''
Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)

If tcpServer.State <> sckClosed Then tcpServer.Close

tcpServer.Accept requestID

End Sub

Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)

Dim strData As String
tcpServer.GetData strData
txtOutput.Text = strData

End Sub

Private Sub tcpServer_Close()
tcpServer.Close
tcpServer.Listen
End Sub

hope that makes sense... good luck!!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
K. When you said &quot;it will not listen to 2nd (and later) connect request&quot; I thought maybe...
You were wondering about the condition of the current installation of your Winsock control. You might just try the MSDN example by itself in order to rule out those concerns.
 
Finally, I got it working. Great thanks to all of you helping me. Special thank to ADoozer. Your code helps a lot.

To be honest, I just made the code working. Still not quite sure how winsock works.

All changes are on client side, including adding _Connect() event, _Close() event, etc. No change on server side.

Thanks all, again.
 
Hi fechen,

So what exactly did you figure out to make that error message goes away (&quot;Wrong protocol...&quot;)? I'm having a same problem with mine.

Thanks. :)
 
you usualy (from personal experience) see the wrong protocol message when:-

1: you use the wrong protocol

or

2: try to send before connection state = vbconnected (7)
(as in fechen's case [i think])

ive not seen it for any other reason but im sure there are more!

good luck!

If somethings hard to do, its not worth doing - Homer Simpson
------------------------------------------------------------------------
A General Guide To Excel in VB FAQ222-3383
File Formats Galore @
 
Here is the code:
=======================================================
Private Sub appendText(txt As String)
If RecvText.Text = &quot;&quot; Then
RecvText.Text = txt
Else
RecvText.Text = RecvText.Text + vbCr + vbLf + txt
End If

RecvText.SelStart = Len(RecvText.Text)
End Sub

Private Sub Form_Load()
clientSocket.RemoteHost = &quot;localhost&quot;
clientSocket.RemotePort = 6907
End Sub

Private Sub SendBtn_Click()
If clientSocket.State <> sckClosed Then clientSocket.Close
clientSocket.Connect 'issue connection request
End Sub

'====================================
'EVENTS
'====================================
'the connection request completed
Private Sub clientSocket_Connect()
clientSocket.SendData &quot;Test message from VB client&quot;
End Sub

Private Sub clientSocket_Close()
clientSocket.Close
End Sub

'data received from the other end
Private Sub clientSocket_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
clientSocket.GetData strData
appendText &quot;server replied: &quot; + strData
End Sub

Private Sub clientSocket_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox Description
clientSocket.Close
End Sub
====================================================
winsock VB control seems heavily event-based. So don't code like the way of other socket (such as java or C++/C). Separate your commands into different event handlers, such as don't close your socket in _connect() event. make sure have a _close() event. To fully understand this winsock control, probably I need read more documents.

Hope it helps. Or you can post the code. We got several experts here in this thread. :) They probably can help you.
 
Ok, thanks fechen.

You're right about the winsock VB control. It is heavily event-based. Mine is now working now. I send through it with a XML document. It works real good.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top