Microsoft KB Archive/209905

= ACC2000: Two Examples That Use the Winsock ActiveX Control =

Article ID: 209905

Article Last Modified on 6/28/2004

-

APPLIES TO


 * Microsoft Access 2000 Standard Edition

-



This article was previously published under Q209905



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 ActiveX control installed with the Microsoft Office 2000 Developer Edition Tools.



MORE INFORMATION
The Winsock control allows 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 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 is that of a radio. In this analogy, a radio station 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 receive data. You create a form with three Winsock controls. One of the controls emulates the client computer environment to send 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.  Create a new blank database named WinsockDemo.mdb.  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  On the View menu, click Code.  Type the following line in the Declaration section of the form's class module: Dim wsListen, wsClient, wsServer As Winsock   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 = 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. Msgbox "Client requested connection with server." wsClient.Connect 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  Save and close TCPForm.</li> Open TCPForm in Form view and perform the following tasks: <ol style="list-style-type: lower-alpha;"> 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.</li> 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.</li> Click the Send Data button, and note that the client message "Hello" appears in the text box on the form.</li> Click the Respond button, and note that the server message "Thanks for the message!" is displayed in the text box.</li> Click the Close Connection button, and note the message boxes from both client and server indicating that the connections have been closed.</li></ol> </li></ol>

Example 2 - Using the Winsock ActiveX Control with UDP
This example uses the same computer to both send and 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. <ol> Create a new blank database named WinsockDemo.mdb, or use the database that you created in the earlier example.</li>  Create the following new form (not based on any table or query) in Design view: <pre class="fixed_text">  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 </li> On the View menu, click Code.</li>  Type the following line in the Declaration section of the form's class module: Dim wsClient, wsServer As Winsock </li>  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 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 </li> Save and close the form UDPForm.</li> 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.</li></ol>

Additional query words: ODE

Keywords: kbhowto kbinfo kbusage KB209905

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.