Results 1 to 5 of 5

Thread: [RESOLVED] winsock error handler

  1. #1

    Thread Starter
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    Resolved [RESOLVED] winsock error handler

    I have the following code. It's a simple client/server chat. Can someone please tell me what errors to look for when using winsock and how to handle/avoid them. I know if I press the button to LISTEN or CONNECT when it's doing that stuff already, the program errors out.

    vb Code:
    1. Option Explicit
    2.  
    3. Private Sub cmdconnect_Click()
    4.  
    5. Winsock.RemoteHost = txtIp.Text  ' this is the Ip of the server 127.0.0.1 loops back to your own computer
    6. Winsock.RemotePort = 15151  ' this is the port it must be the same as the server
    7. Winsock.Connect  ' connect!
    8.  
    9. End Sub
    10.  
    11. Private Sub cmdListen_Click()
    12.  
    13. Winsock.LocalPort = 15151 'open the port on the local machine try to use ports between 3000 and 50000
    14. Winsock.Listen ' start listening, simple!
    15.  
    16. End Sub
    17.  
    18. Private Sub Winsock_ConnectionRequest(ByVal requestID As Long)
    19.  
    20. Winsock.Close 'this resets our socket ready to accept the incoming connection
    21. Winsock.Accept requestID 'accept the connection!
    22.  
    23. End Sub
    24.  
    25. Private Sub winsock_DataArrival(ByVal bytesTotal As Long)
    26.  
    27. Dim incommingData As String 'this stores the data we receive
    28.  
    29. Winsock.GetData incommingData ' this stores the data in the variable we set above
    30.  
    31. ' now we will display it in the textbox
    32.  
    33. txtChat.Text = txtChat.Text & vbCrLf & incommingData
    34.  
    35. End Sub
    36.  
    37. Private Sub cmdSend_click()
    38.  
    39. Winsock.SendData txtMessage.Text ' sends the data in the textbox
    40. txtChat.Text = txtChat.Text & vbCrLf & txtMessage.Text
    41.  
    42. End Sub
    [vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: winsock error handler

    You can't use Listen, Connect, SendData, or set some of the other properties (RemoteHost, RemotePort, LocalPort) if you are already connected. This is why it's always a good idea to use Winsock.Close before any of those methods.

    Some things to know if you don't already:

    If there is an error connecting, the Winsock_Error() event will fire. Use the Number and Description arguments to see more about the error.

    If the other end closes the connection, the Winsock_Close() event will fire.

    You can also check the Winsock.State property before trying to send data or whatever. Here is a list of the Winsock states.

    sckClosed = 0
    sckOpen = 1
    sckListening = 2
    sckConnectionPending = 3
    sckResolvingHost = 4
    sckHostResolved = 5
    sckConnecting = 6
    sckConnected = 7
    sckClosing = 8
    sckError = 9

  3. #3

    Thread Starter
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    Re: winsock error handler

    That's good stuff to know DigiRev. One question about your response that I have is where you say:

    "You can't use Listen, Connect, SendData, or set some of the other properties (RemoteHost, RemotePort, LocalPort) if you are already connected."

    I learned the hard way about LISTEN & CONNECT. You can't use those if you are already connected. I got that. However, you also say SendData can't be used if you're already connected. Shouldn't one be able to send data now that they are connected? Why should one close winsock for this? Did you type that by mistake or am I missing something? Because if SendData should be closed before sending, then wouldn't the same be true about GetData? I'm confused now.
    [vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.

  4. #4
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: winsock error handler

    Quote Originally Posted by drivenbywhat
    That's good stuff to know DigiRev. One question about your response that I have is where you say:

    "You can't use Listen, Connect, SendData, or set some of the other properties (RemoteHost, RemotePort, LocalPort) if you are already connected."

    I learned the hard way about LISTEN & CONNECT. You can't use those if you are already connected. I got that. However, you also say SendData can't be used if you're already connected. Shouldn't one be able to send data now that they are connected? Why should one close winsock for this? Did you type that by mistake or am I missing something? Because if SendData should be closed before sending, then wouldn't the same be true about GetData? I'm confused now.
    Sorry to confuse you, not sure why I typed that. SendData can ONLY be called when connected or you will get an error.

    Unless Winsock's Protocol property is set to UDP. UDP is a "connectionless" protocol so you can send data without connecting.

  5. #5

    Thread Starter
    Fanatic Member drivenbywhat's Avatar
    Join Date
    Jan 2007
    Location
    VA - USA
    Posts
    866

    Re: winsock error handler

    Ok, cool. I'm un-confused again.

    PS
    Seems every body is on vacation or something. Could you take a look at my other post when you have a chance. Thanks.

    http://www.vbforums.com/showthread.p...79#post2934779
    [vb5 & starting to move to vb2008] I appreciate the help I get from everyone. Thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width