Results 1 to 4 of 4

Thread: No DataArrival?

  1. #1

    Thread Starter
    New Member MyTHoS_'s Avatar
    Join Date
    Feb 2006
    Location
    Everywhere!
    Posts
    5

    Unhappy No DataArrival?

    Hi i am new to visual basic..I have some knowledge about the winsocks in vbasic and i have succesfully made a chat program..Why am i saying all this?
    Because i can't explain the problem i have..Ok here goes:
    I have made a winsocket that listens to a specific port.The client can succesfully connect and i can see that when i accept the connection request..But when i try to receive the data the client sends i receive nothing, that is the DataArrival is never called..Here is the code part.I can't figure out what's the problem because i use the same code i used in my chat program

    VB Code:
    1. Winsock23.LocalPort = 23
    2. Winsock23.Listen
    3.  
    4. Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
    5.  
    6. On Error Resume Next
    7.  
    8. 'Now accept the Request
    9.  
    10. Winsock23.Accept requestID
    11. txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
    12. End Sub
    13.  
    14.  
    15.  
    16. Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
    17. Dim str As String
    18. MsgBox str
    19.  
    20. Winsock23.GetData str, vbString
    21. End Sub

    The msbox should at least display but nothing happens which means that i don't receive anything


    For the client i use

    VB Code:
    1. Winsock1.Connect txtIp.Text, 23
    2. Winsock1.SendData "Test"

    I don't think there's a client problem for not receiving anything right?
    Anyway i would really appreciate any help because i am on the finishing touches of my project and i am really stuck
    Last edited by MyTHoS_; Feb 6th, 2006 at 05:10 PM.

  2. #2
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: No DataArrival?

    You have 2 mistakes.

    Regarding this code:
    VB Code:
    1. Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
    2.  
    3. On Error Resume Next
    4.  
    5. Winsock23.Accept requestID
    6. txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
    7. End Sub
    You are accepting the connection on the Listening socket, and you can't do that...
    You have to Close the Listening state, THEN Accept the connection.

    On top of that you have "On Error Resume Next" wich will ignore the error that you should get in this case (accepting the connection while in Listening state)

    So, you should change your code to this:
    VB Code:
    1. Private Sub Winsock23_ConnectionRequest(ByVal requestID As Long)
    2. [b]Winsock23.Close[/b]
    3. Winsock23.Accept requestID
    4. txtEvent.Text = txtEvent.Text & vbCrLf & " connected"
    5. End Sub

    Next mistake is on the client side...
    You send the data right after you give the Connect command....
    It takes time to connect, and if you send data BEFORE it's actually connected, it won't send anything.

    You should send data AFTER you receive the "Connect" winsock event.

  3. #3
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    Re: No DataArrival?

    Just spoted a 3'rd mistake:
    VB Code:
    1. Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
    2. Dim str As String
    3. MsgBox str
    4.  
    5. Winsock23.GetData str, vbString
    6. End Sub
    If you want to show a message with the data you receive on the server, I sugest to show the message AFTER you read the data... like this:
    VB Code:
    1. Private Sub Winsock23_DataArrival(ByVal bytesTotal As Long)
    2. Dim str As String
    3.  
    4. [b]Winsock23.GetData str, vbString   ' read the data FIRST
    5. MsgBox str                        ' THEN display it[/b]
    6. End Sub

  4. #4

    Thread Starter
    New Member MyTHoS_'s Avatar
    Join Date
    Feb 2006
    Location
    Everywhere!
    Posts
    5

    Re: No DataArrival?

    Thank you very much man!I corrected the other mistakes before your post..The problem was on the " on Error resume next and the fact that i didn't close the winsock.Thank you very much!

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