WEBINAR:
On-Demand
Application Security Testing: An Integral Part of DevOps
Accept Method
It accepts the request for connection from the client system.
For this method to be used, the control must be in the listening state.
Close Method
The Close method terminates a TCP connection from either the
client or server applications.
GetData Method
GetData is the method that retrieves the current block of
data from the buffer and then stores it in a variable of the variant type.
PeekData Method
The PeekData method operates in a fashion similar to the
GetData method. However, it does not remove data from the input queue.
Listen Method
This is invoked on the server application to have the server
application wait for a TCP request for connection from a client system.
SendData Method
This method dispatches data to the remote computer. It is
used for both the client and server systems.
Connect Method
The Connect method requests a connection to a remote
computer.
I am not going to discuss events here. You can find the
complete details of events on the Microsoft site (http://www.microsoft.com).
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:
-
Start a new EXE project.
-
Add a Winsock control to your application.
-
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
Creating the Server
The server portion of the price lookup example is designed to
accept the item number sent from the client and look up the associated price in
a database. The server than sends the information back to the client. There is
file named as "path.txt" in the folder called as "server". Locate that
file and change the database path in the file to the location where the database
is located on your machine. The connection to the database is made in the
DataArrival event of the Winsock control. The following code segment opens the
database and finds the first occurrence of the value in sItemData. When the
record is found, the value contained in the price field is sent back to the
client.
' Get clients request from database
strData = "Item = '" & sItemData & "'"
rs.Open "select * from prices", strConnect, adOpenKeyset, adLockOptimistic
rs.Find strData
strOutData = rs.Fields("Price")
Follow the steps shown below to create the server:
-
Start a new Standard EXE in VB.
-
Add the Winsock control to your application.
-
Add the controls to the form as shown in the accompanying
code (See the folder named as "server").
Here is the complete code:
Option Explicit
Dim iSockets As Integer
Dim sServerMsg As String
Dim sRequestID As String
Private Sub Form_Load()
Form1.Show
lblHostID.Caption = Socket(0).LocalHostName
lblAddress.Caption = Socket(0).LocalIP
Socket(0).LocalPort = 1007
sServerMsg = "Listening to port: " & Socket(0).LocalPort
List1.AddItem (sServerMsg)
Socket(0).Listen
End Sub
Private Sub socket_Close(Index As Integer)
sServerMsg = "Connection closed: " & Socket(Index).RemoteHostIP
List1.AddItem (sServerMsg)
Socket(Index).Close
Unload Socket(Index)
iSockets = iSockets - 1
lblConnections.Caption = iSockets
End Sub
Private Sub socket_ConnectionRequest(Index As Integer, ByVal requestID As Long)
sServerMsg = "Connection request id " & requestID & " from " & Socket(Index).RemoteHostIP
If Index = 0 Then
List1.AddItem (sServerMsg)
sRequestID = requestID
iSockets = iSockets + 1
lblConnections.Caption = iSockets
Load Socket(iSockets)
Socket(iSockets).LocalPort = 1007
Socket(iSockets).Accept requestID
End If
End Sub
Private Sub socket_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim sItemData As String
Dim strData As String
Dim strOutData As String
Dim strConnect As String
' get data from client
Socket(Index).GetData sItemData, vbString
sServerMsg = "Received: " & sItemData & " from " & Socket(Index).RemoteHostIP & "(" & sRequestID & ")"
List1.AddItem (sServerMsg)
'strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=G:.mdb;Persist Security Info=False"
Dim strPath As String
'Change the database path in the text file
Dim fso As New FileSystemObject, txtfile, _
fil1 As File, ts As TextStream
Set fil1 = fso.GetFile("path.txt")
' Read the contents of the file.
Set ts = fil1.OpenAsTextStream(ForReading)
strPath = ts.ReadLine
ts.Close
Set fso = Nothing
strConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Persist Security Info=False;Data Source=" & strPath & _
"; Mode=Read|Write"
Dim rs As New ADODB.Recordset
' Get clients request from database
strData = "Item = '" & sItemData & "'"
rs.Open "select * from prices", strConnect, adOpenKeyset, adLockOptimistic
rs.Find strData
strOutData = rs.Fields("Price")
'send data to client
sServerMsg = "Sending: " & strOutData & " to " & Socket(Index).RemoteHostIP
List1.AddItem (sServerMsg)
Socket(Index).SendData strOutData
End Sub
Running the example
-
Create executable for both the applications.
-
Launch both the applications.
-
Click the Connect button.
-
Enter a value from 0 to 6 (currently the database
contains only six records, error handling is not done in this code, you can
add the error handling yourself) and click the Lookup button. The associated
price will be displayed in the price field.

Client

Server