ACC97: Two Examples Using the Winsock ActiveX Control

ID: Q163999


The information in this article applies to:
  • Microsoft Access 97


SUMMARY

Advanced: Requires expert coding, interoperability, and multiuser skills.

This article shows you two examples of how to use the Winsock ActiveX control installed with the Microsoft Office 97 Developer Edition Tools.

This article assumes that you are familiar with Visual Basic for Applications and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information about Visual Basic for Applications, please refer to the "Building Applications with Microsoft Access 97" manual.


MORE INFORMATION

The Winsock control enables you to connect to a remote computer and exchange data between both client and server computers. The Winsock control supports two protocols: Transmission Control Protocol (TCP) and User Datagram Protocol (UDP).

TCP is a connection-based protocol. A common analogy used to describe TCP is that of a telephone. In this analogy, callers must establish a connection on both ends of the telephone line before they can exchange information. A computer using TCP must get confirmation from the receiving computer that a connection has been established before the two computers can transfer data.

UDP is a connectionless protocol. A common analogy used to describe UDP is that of a radio. In this analogy, a radio station simply broadcasts its signal without knowing for sure if anyone is listening. A computer using UDP sends data and does not require a connection with the computer on the receiving end of the transmission.

Example 1 - Using the Winsock ActiveX Control with TCP

This example uses the same computer to both send and to receive data. You create a form with three Winsock controls. One of the controls emulates the client computer environment; it sends a connection request to a server. The other two controls emulate the environment on a server computer: one control listens for a connection request, and the other accepts the request when it comes.
  1. Create a new blank database named WinsockDemo.mdb.


  2. Create a new form not based on any table or query in Design view:


  3. 
          Form: TCPForm
          ---------------------------------
          Caption: TCP Form
    
          Command button:
             Name: cmdListen
             Caption: Listen
          Command button:
             Name: cmdConnect
             Caption: Establish Connection
          Command button:
             Name: cmdSend
             Caption: Send Data
          Command button:
             Name: cmdRespond
             Caption: Respond
          Command button:
             Name: cmdClose
             Caption: Close Connection
          Text box:
             Name: Text1
             Label Caption: Data Received:
          Winsock control:
             Name: axWinsockListen
          Winsock control:
             Name: axWinsockClient
          Winsock control:
             Name: axWinsockServer 
  4. On the View menu, click Code.


  5. Type the following line in the Declaration section of the form's class module:


  6. 
    Dim wsListen, wsClient, wsServer As Winsock 
  7. Type the following procedures. To aid in understanding how the Winsock control works between client and server, the procedures are listed in the order in which they will occur:


  8. 
          Private Sub Form_Load()
             ' Set one server Winsock control and the client Winsock control
             ' when the form loads.
             Set wsListen = Me!axWinsockListen.Object
             Set wsClient = Me!axWinsockClient.Object
    
             ' Set the protocol for each control.
             wsListen.Protocol = sckTCPProtocol
             wsClient.Protocol = sckTCPProtocol
    
             ' Set the remote host on the client Winsock control. Because
             ' client and server are the same computer in this example, set
             ' RemoteHost equal to LocalIP.
             wsClient.RemoteHost = wsListen.LocalIP
    
             ' Set a local and a remote port for the client.
             wsClient.RemotePort = 100
             wsClient.LocalPort = 99
    
             ' Set a local and a remote port for the server. Note that the
             ' server RemotePort is the client LocalPort and vice versa.
             wsListen.LocalPort = 100
             wsListen.RemotePort = 99
          End Sub
    
          Private Sub cmdListen_Click()
             ' Start the server listening for a connection request.
             wsListen.Listen
             Msgbox "Server is waiting for a connection request."
          End Sub
    
          Private Sub cmdConnect_Click()
             ' The client requests a connection with the server.
             wsClient.Connect
             Msgbox "Client requested connection with server."
          End Sub
    
          Private Sub axWinsockListen_ConnectionRequest(ByVal requestID As _
             Long)
             ' When the server receives a connection request, set the second
             ' Winsock on the server to accept the request.
             Set wsServer = Me!axWinsockServer.Object
             wsServer.Protocol = sckTCPProtocol
    
             ' Accept the connection request.
             wsServer.Accept requestID
             Msgbox "Server accepted client connection request."
          End Sub
    
          Private Sub axWinsockClient_Connect()
             ' When the server accepts the connection request, the Connect
             ' event fires on the client. Display a message indicating success.
             MsgBox "Connection Successful!"
          End Sub
    
          Private Sub cmdSend_Click()
             ' After a connection is established, use a command button to send
             ' data from client to server.
             wsClient.SendData "Hello"
          End Sub
    
          Private Sub axWinsockServer_DataArrival(ByVal bytesTotal As Long)
             Dim strClientMsg As String
    
             ' The DataArrival event fires on the server when the client sends
             ' information. Get the data and display it in a text box.
             wsServer.GetData strClientMsg, vbString
             Me!Text1.Value = strClientMsg
          End Sub
    
          Private Sub cmdRespond_Click()
             ' Send a message from the server to the client.
             wsServer.SendData "Thanks for the message!"
          End Sub
    
          Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
             Dim strServerMsg As String
    
             ' The DataArrival event fires on the client when the server sends
             ' information. Get the data and display it in a text box.
             wsClient.GetData strServerMsg
             Me!Text1.Value = strServerMsg
          End Sub
    
          Private Sub cmdClose_Click()
             ' Close the server connections
             wsServer.Close
             wsListen.Close
             Msgbox "Server connections closed."
          End Sub
    
          Private Sub axWinsockClient_Close()
             ' Close event on client fires after server closes connection.
             ' Close the client connection and display a message box.
             wsClient.Close
             MsgBox "Client connections closed. Good-Bye!"
          End Sub 
  9. Save and close TCPForm.


  10. Open TCPForm in Form View and perform the following tasks:


    1. Click the Listen button to start the server listening for a connection request. Note the message box that appears to indicate the server is waiting for a connection.


    2. Click the Establish Connection button. Note the message box from the server that the request was accepted, and the message box from the client that the connection was successful.


    3. Click the Send Data button, and note that the client message "Hello" appears in the text box on the form.


    4. Click the Respond button, and note that the server message "Thanks for the message!" is displayed in the text box.


    5. Click the Close Connection button, and note the message boxes from both client and server indicating connections have been closed.


Example 2 - Using the Winsock ActiveX Control with UDP

This example uses the same computer to both send and to receive data. You create a form with two Winsock controls: one of the controls emulates the client computer and the other control emulates the server.
  1. Create a new blank database named WinsockDemo.mdb, or use the one you created in the earlier example.


  2. Create a new form not based on any table or query in Design view:


  3. 
          Form: UDPForm
          --------------------------------
          Caption: UDP Form
    
          Command button:
             Name: cmdSend
             Caption: Send Data
          Text box:
             Name: Text1
             Label Caption: Data Received:
          Winsock control:
             Name: axWinsockClient
          Winsock control:
             Name: axWinsockServer 
  4. On the View menu, click Code.


  5. Type the following line in the Declaration section of the form's class module:


  6. 
    Dim wsClient, wsServer As Winsock 
  7. Type the following procedures. To aid in understanding how the Winsock control works between client and server, the procedures are listed in the order in which they will occur:


  8. 
          Private Sub Form_Load()
             ' Set the control objects when the form loads.
             Set wsClient = Me!axWinsockClient.Object
             Set wsServer = Me!axWinsockServer.Object
    
             ' Set the protocol for client and server.
             wsClient.Protocol = sckUDPProtocol
             wsServer.Protocol = sckUDPProtocol
    
             ' Set the host and ports for client and server. Because client
             ' and server are the same computer in this example, set RemoteHost
             ' equal to LocalIP.
             wsServer.RemoteHost = wsClient.LocalIP
             wsServer.RemotePort = 1007
             wsClient.Bind 1007
          End Sub
    
          Private Sub CmdSend_Click()
             ' Send a broadcast message from the server.
             wsServer.SendData "Hello"
          End Sub
    
          Private Sub axWinsockClient_DataArrival(ByVal bytesTotal As Long)
             Dim strServerMsg As String
    
             ' When a message arrives from the server, display it in a text
             ' box.
             wsClient.GetData strServerMsg, vbString
             Me!Text1.Value = strServerMsg
          End Sub 
  9. Save and close the form UDPForm.


  10. Open UDPForm in Form view and click the Send Data button. Note that the text box displays "Hello." Because this is a connectionless transmission, you do not have to establish a client-server connection.



REFERENCES

For more information about the Winsock ActiveX control, search the Help Index for "Winsock control."

Additional query words: ODE

Keywords : kbusage
Version : WINDOWS:97
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: September 28, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.