February 18, 2019
Hot Topics:

Using Winsock

  • November 20, 2002
  • By Karl Moore
  • Send Email »
  • More Articles »

So far, we've created a program that sends messages. And a program that receives messages.

But the send program can't receive. And the receive program can't send.

Now, I may not be an Einstein but the world pretty much survives on two-way communication. And even though most introductions don't usually delve beyond the code we've explored so far, let's be honest ICQ wouldn't have a membership base of ninety million if you could only send messages one way.

So in order to implement this functionality, I've created a quick 'template' application that allows you to both send and receive. You can download that here go on, have a play around and see what you can do!

And for all the groovy guys and gals printing this article, here's a dump of the core code, complete with basic error handling. All objects, such as Text Boxes have been given 'English' names to make the code more readable:

Private Sub Form_Load()On Error GoTo PortErr' Listen out for any possible connection' requests or data, etc.Winsock1.LocalPort = 202Winsock1.Listen' This uses port 201. To work, this program' must be run on two separate computers' - otherwise there will be a conflict with' two programs trying to access port 201Exit Sub    PortErr:    MsgBox "Another program is using port 201. This " & _    "may be because you're running another copy " & _    "of this program on the same computer. " & _"Unfortunately, this only works over a real " & _"network, either an office setup or the Internet. " & _"When two computers try to 'listen in' to the same " & _"part of a computer, one of them receives an error. " & _"I am alas the unlucky soul. Sorry! Here's an idea " & _"though - alter my Form_Load source code, changing the " & _"LocalPort property to 202 instead of 201. This should " & _"allow you to test both programs on the same machine!", _         vbCritical            EndEnd SubPrivate Sub cmdSendMessage_Click()    If Winsock1.State = sckConnected Then                ' If connected, just send the data                Winsock1.SendData (txtOutgoingMessage.Text)            Else            If MsgBox("You are not connected. " & _            "Connect to " & txtAddress.Text & "?", _            vbYesNo + vbQuestion) = vbYes Then                        ' Connect to the remote machine                        Winsock1.Close            Winsock1.RemotePort = 201            Winsock1.RemoteHost = txtAddress.Text            Winsock1.Connect                            ' Wait until full connection is made                            Do Until Winsock1.State = sckConnected                DoEvents: DoEvents: DoEvents: DoEvents                If Winsock1.State = sckError Then                    MsgBox "Problem connecting!"                    Exit Sub                End If            Loop                    ' Send the data                    Winsock1.SendData (txtOutgoingMessage)                Else                    Call MsgBox("OK - not connected!", vbInformation)                End If        End If        End SubPrivate Sub Winsock1_ConnectionRequest(ByVal requestID As Long)    ' Accept an incoming connection request        If Winsock1.State <> sckClosed Then Winsock1.Close    Winsock1.Accept requestIDEnd SubPrivate Sub Winsock1_DataArrival(ByVal bytesTotal As Long)        ' Display incoming data        Dim strIncoming As String    Winsock1.GetData strIncoming    txtIncomingMessage.Text = strIncoming    End Sub

Page 5 of 9

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date