|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
INTRODUCTIONMost of you might have worked with Internet Transfer Control which is very handy control when it comes to Internet Programming but there is another control which even more robust and helps programmers creating more flexible applications. Winsock control comes with VB6 and is used to create applications that access the low-level functions of the Transmission Control Protocol/Internet Protocol (TCP/IP).
Winsock is a standard that is maintained by Microsoft. This standard is basically a set of routines that describe communications to and from the TCP/IP stack. These routines reside in a dynamic link library that runs under Windows. The winsock DLL is interfaced with TCP/IP and from there through the Internet. In this article, I am going to show how to use the winsock in a client server environment, we will create two separate applications, one of which will be a server and the other will be a client. Both client and server will interact with each other to exchange data. Client will send a request to the server and the server which will be connected to a database will retrieve the information requested by the client from the database and will return the requested information back to the client. You will a database with this article, the database contains the item numbers and their prices. In real life situations, database might be located on a machine different from the one that hosts the client application.
Using the Winsock ControlWinsock is above the TCP/IP protocol stack in the ISO/OSI model. TCP/IP is an industry standard communication protocol that defines methods for packaging data into packets for transmission between computing devices on a heterogeneous network. TCP/IP is the standard for data transmission over networks, including the Internet. TCP establishes a connection for data transmission and IP defines the method for sending data packets. The Microsoft Winsock control makes using the TCP/IP a breeze. Microsoft has wrapped up the Winsock and INetAPI API calls into a nice neat package that you can easily incorporate into your Visual Basic applications.Winsock Operating ModesThe Transport layer (also known as the Host-to-Host Transport layer) is responsible for providing the Application layer with session and datagram communication services. The core protocols of the Transport layer are TCP and User Datagram Protocol (UDP). The Winsock control supports the following two operating modes:
Winsock PropertiesWinsock enables you to create clients and servers using the same control. This dual functionality enables you to specify through property setting the type of application you will be building. The Winsock control uses a number of the same properties, whether you are creating client or a server, thereby all but eliminating the learning curve needed to create applications. Some of the important properties of the control are as following: BytesReceived Property Winsock MethodsSome of the important methods of Winsock control are as following: Creating the ClientFollow the steps shown below: 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 ServerThe 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: 1. Start a new Standard EXE in VB. 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 example1. Create executable for both the applications.
|
||||||||||||||||||||||||||||||||||||||||