Client Server Programming with Winsock

Creating the Client

In the sample provided with this article, we are going to create two applications, one server and client. This is a real world example, where the clients requests some information from the server and the server retrieves some specific information from the database and sends the retrieved information back to the client. The database used in the sample is also provided with the code. The database name is Prices.mdb. This is a small database comprising of a single table containing two fields. The fields are item number and price. The clients sends the item number to the server and the server retrieves the price against that item number from the database and sends it back to the client. One of the current trends in software development today is the issue of thick clients versus thin clients. A thick client is basically an application that performs the bulk of the processing on the individual client PC, whereas a thin client performs the processing on the server.

Creating the Client

Follow the steps shown below:

1. Start a new EXE project.
2. Add a Winsock control to your application.
3. Add all the controls to the form (See the application for details).

Here is the complete code:

Option Explicit

Private Sub cmdClose_Click()
Winsock1.Close
shpGo.Visible = False
shpWait.Visible = False
shpError.Visible = True
End Sub

Private Sub cmdConnect_Click()
    Winsock1.RemoteHost = "11.0.0.1" 'Change this to your host ip
    Winsock1.RemotePort = 1007
    Winsock1.Connect
    shpGo.Visible = True
    txtItem.SetFocus
End Sub

Private Sub cmdSend_Click()
    If Winsock1.State = sckConnected Then
        Winsock1.SendData txtItem.Text
        shpGo.Visible = True
        Label3.Caption = "Sending Data"
    Else
        shpGo.Visible = False
        shpWait.Visible = False
        shpError.Visible = True
        Label3.Caption = "Not currently connected to host"
    End If
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Dim sData As String
    Winsock1.GetData sData, vbString
    'Label1.Caption = sData
    txtPrice.Text = sData
    Label3.Caption = "Received Data"
    shpGo.Visible = True
    shpWait.Visible = False
    shpError.Visible = False

End Sub

Private Sub Winsock1_SendComplete()

Label3.Caption = "Completed Data Transmission"

End Sub

You might also like...

Comments

About the author

S.S. Ahmed United States

S.S. Ahmed is a senior IT Professional and works for a web and software development firm. Ahmed is a Microsoft Office SharePoint Server MVP. Ahmed specializes in creating database driven dynamic...

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.

“Linux is only free if your time has no value” - Jamie Zawinski