WinSock Control

Creating a Server

Add a new form, called frmServer, and add a WinSock control to the form, naming it tcpServer. Then, add the other controls listed below:

Control Name Control Type Caption/Text
txtSend TextBox  
txtReceive TextBox  
cmdSend CommandButton Send

You can also add labels next to the text boxes so that you know which is which.

Now, if you don't know the IP Address or PC_Name of the PC you are currently on, we will find this out now. Press F5 to run the project. Then, open the Immediate Window (View|Immediate Window), and type tcpServer.LocalHostName and press Enter. This will return the name of your current PC. Then, type tcpServer.LocalIP, press enter, and you will be given the IP Address. It is normally better to use the PC Name, instead of the IP Address, unless you know that the IP Address never changes (on some networks, the IP address changes each time the PC is turned on).

Now that we know the name of the PC, enter the following code into the Form_Load() procedure:

Private Sub Form_Load()
    '// Set the LocalPort property to an integer.
    '// Then invoke the Listen method.
    tcpServer.LocalPort = 100
    tcpServer.Listen
    '// Get our Server Name, and set the form's caption
    Caption = "WinSock Tutorial - TCP Server @ " & tcpServer.LocalHostName
End Sub

The first line of code sets the LocalPort property to 100. This is the Port that the server will listen to. This can be any value that is an integer (but the Client must know this value too). The second line tells the Server to start listening to that port.

The next piece of code we need to enter is to tell the control what to do if a connection is requested by the Client. Enter the code below:

Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)
    ' Check if the control's State is closed. If not,
    ' close the connection before accepting the new
    ' connection.
    If tcpServer.State <> sckClosed Then tcpServer.Close
    ' Accept the request with the requestID
    ' parameter.
    tcpServer.Accept requestID
End Sub

The first line checks if there is already a connection. If there is, it will terminate it. The second line accepts the request. When this line is executed, we now have a connection with the client.

The final bit of code we need is so that the control knows what to do when data arrives, and also so that when the Send button is clicked, it will send the data. To do this, enter this code:

Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    tcpServer.GetData strData
    txtReceive.Text = strData
End Sub

Private Sub cmdSend_Click()
    If tcpServer.State <> sckConnected Then
        MsgBox "You must be connected to the client first"
    Else
        tcpServer.SendData txtSend.Text
    End If
End Sub

The first procedure occurs when data has arrived from the Client. First, it calls the GetData method, passing the empty variable strData. Then, it fills the txtReceive textbox with the string now contained in strData. Note that once you have called the GetData method, the data is erased from the controls memory. If you want to take a look at the data that has arrived, without removing it, use the Peek property instead.

The second procedure is called when the Send button is clicked. All this does is instructs the WinSock control to send the data contained in the txtSend textbox.

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The trouble with programmers is that you can never tell what a programmer is doing until it's too late.” - Seymour Cray