February 28, 2021
Hot Topics:

Using Winsock

  • By Karl Moore
  • Send Email »
  • More Articles »

Let's briefly review the raw code for building a simple messaging program with the Winsock control. All of this code is based around what we have discovered already, with a few slight enhancements:

To Open a Connection

If Winsock1.State <> sckClosed Then Winsock1.Close

Winsock1.RemotePort = 1008Winsock1.RemoteHost = ""Winsock1.Connect

To Wait until Connected

Do Until Winsock1.State = sckConnectedDoEvents: DoEvents: DoEvents: DoEventsIf Winsock1.State = sckError ThenMsgBox "Problem connecting!"Exit SubEnd IfLoop

To Listen Out for Communication

Winsock1.LocalPort = 1008Winsock1.Listen

To Accept an Incoming Connection Request

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

To Send Data

Winsock1.SendData ("Data Goes Here")

To Receive Incoming Data

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)        Dim strIncoming As StringWinsock1.GetData strIncomingx = strIncoming        End Sub

Page 4 of 9

This article was originally published on November 20, 2002

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