Results 1 to 3 of 3

Thread: Winsock question about the tutorial on vbworld.com

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    2

    Exclamation Winsock question about the tutorial on vbworld.com

    Hey everybody,
    I am pretty new to the whole visual basic scene, but I am very interested. So far, I have read 2 full tutorials on vbworld.com by Karl Moore.
    He is my question.
    I have read half of the Winsock tutorial and made the two programs; the receiver and sender.
    here is the code....
    Sender:


    Private Sub Command1_Click()
    If Winsock1.State <> sckClosed Then Winsock1.Close
    Winsock1.RemotePort = 1008
    Winsock1.RemoteHost = "192.168.1.0, also tried 127.0.0.1, and 192.168.1.1"
    Winsock1.Connect
    Do Until Winsock1.State = sckConnected
    DoEvents: DoEvents: DoEvents: DoEvents
    If Winsock1.State = sckError Then
    MsgBox "Problem connecting!"
    Exit Sub
    End If
    Loop
    Winsock1.SendData (txtMessage.Text)
    Winsock1.Close
    End Sub

    The receiver:

    Private Sub form_load()
    Winsock1.LocalPort = 1008
    Winsock1.Listen
    End Sub

    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
    If Winsock1.State <> sckClosed Then Winsock1.Close
    Winsock1.Accept requestID
    End Sub


    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim strIncoming As String
    Winsock1.GetData strIncoming
    MsgBox strIncoming
    End Sub


    The problem is it doesn't work... Whenever I try to send a message it gives me the sckerror.
    I have a network over a linksys router, and I tried both over the network and over one computer.
    Both tries failed.
    If anyone could be kind enough to help me out here, I would greatly appreciate it.

    Thanks in advance.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Location
    O!
    Posts
    1,177
    The 2 statements:
    VB Code:
    1. Winsock1.SendData (txtMessage.Text)
    2. Winsock1.Close
    will cause a problem. The SendData method will execute asyncronously. This means that as it is executing, VB will proceed to execute the Close method. This usually results in no data reaching the server.

    There are dozens of posts from new users like yourself who used this example to learn sockets programming and had problems.

    My first question is - do you need to close the connection after you send the data? If so, add a Winsock1_SendComplete sub and put close the connection there.

    A socket connection is just like a phone call. Both sides can 'talk' to each other for as long as is needed. Once the conversation is finished, one side can close the connection and the other side can detect the close (in a Winsock1_Close sub) and close their socket.

    So, if you need to get a reply to the send or you need to send more than 1 data item, you will need to keep the socket open.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2002
    Posts
    2
    Hey,
    Thank you very much for the reply; it worked.
    Thanks alot again.

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