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 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:
1. Start a new Standard EXE in VB.
2. Add the Winsock control to your application.
3. 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:\Prices.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
1. Create executable for both the applications.
2. Launch both the applications.
3. Click the Connect button.
4. 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.
Comments