ACC2000: Two Examples That Use the Winsock ActiveX Control

This article has been archived. It is offered "as is" and will no longer be updated.
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access database (.mdb).

Summary
This article provides two examples of how to use the Winsock ActiveXcontrol installed with the Microsoft Office 2000 Developer Edition Tools.
More information
The Winsock control allows you to connect to a remote computer andexchange data between both client and server computers. The Winsockcontrol supports two protocols: Transmission Control Protocol (TCP)and User Datagram Protocol (UDP).

TCP is a connection-based protocol. A common analogy used to describe TCPis that of a telephone. In this analogy, callers must establish aconnection on both ends of the telephone line before they can exchangeinformation. A computer using TCP must receive 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 isthat of a radio. In this analogy, a radio station broadcasts its signal without knowing for sure if anyone is listening. A computer usingUDP sends data and does not require a connection with the computer on thereceiving end of the transmission.

Example 1 - Using the Winsock ActiveX Control with TCP

This example uses the same computer to both send and receive data. Youcreate a form with three Winsock controls. One of the controls emulatesthe client computer environment to send a connection request to aserver. The other two controls emulate the environment on a servercomputer: One control listens for a connection request, and the otheraccepts the request when it comes.
  1. Create a new blank database named WinsockDemo.mdb.
  2. Create the following new form (not based on any table or query) in Design view:
       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					
  3. On the View menu, click Code.
  4. Type the following line in the Declaration section of the form's class module:
    Dim wsListen, wsClient, wsServer As Winsock					
  5. Type the following procedures.

    To optimally illustrate how the Winsock control works between client and server, the procedures are listed in the order in which they occur.
    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 = 99End SubPrivate Sub cmdListen_Click()   ' Start the server listening for a connection request.   wsListen.Listen   Msgbox "Server is waiting for a connection request."End SubPrivate Sub cmdConnect_Click()   ' The client requests a connection with the server.   Msgbox "Client requested connection with server."   wsClient.ConnectEnd SubPrivate 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 SubPrivate 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 SubPrivate Sub cmdSend_Click()   ' After a connection is established, use a command button to send   ' data from client to server.   wsClient.SendData "Hello"End SubPrivate 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 = strClientMsgEnd SubPrivate Sub cmdRespond_Click()   ' Send a message from the server to the client.   wsServer.SendData "Thanks for the message!"End SubPrivate 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 = strServerMsgEnd SubPrivate Sub cmdClose_Click()   ' Close the server connections   wsServer.Close   wsListen.Close   Msgbox "Server connections closed."End SubPrivate 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					
  6. Save and close TCPForm.
  7. Open TCPForm in Form view and perform the following tasks:
    1. Click the Listen button to start the server in listening for a connection request.

      Note the message box that appears to indicate that the server is waiting for a connection.
    2. Click the Establish Connection button.

      Note the message box from the client verifying that the connection was successful, and the message box from the server verifying that the request was accepted.
    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 that the connections have been closed.

Example 2 - Using the Winsock ActiveX Control with UDP

This example uses the same computer to both send and receive data. Youcreate a form with two Winsock controls: One of the controls emulates theclient computer and the other control emulates the server.
  1. Create a new blank database named WinsockDemo.mdb, or use the database that you created in the earlier example.
  2. Create the following new form (not based on any table or query) in Design view:
       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					
  3. On the View menu, click Code.
  4. Type the following line in the Declaration section of the form's class module:
    Dim wsClient, wsServer As Winsock					
  5. Type the following procedures.

    To optimally illustrate how the Winsock control works between client and server, the procedures are listed in the order in which they occur.
    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 1007End SubPrivate Sub CmdSend_Click()   ' Send a broadcast message from the server.   wsServer.SendData "Hello"End SubPrivate 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 = strServerMsgEnd Sub					
  6. Save and close the form UDPForm.
  7. 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.
ODE
Properties

Article ID: 209905 - Last Review: 01/14/2015 08:25:00 - Revision: 3.0

  • Microsoft Access 2000 Standard Edition
  • kbnosurvey kbarchive kbhowto kbinfo kbusage KB209905
Feedback