Working with the Winsock Control

The Winsock control is an ActiveX control that provides a way for applications to communicate using the TCP/IP or IrDA protocols. A Winsock control uses the underlying network connection or infrared port to transfer data.

Because a Winsock control can act as a client that connects to a server application or as a server that provides connections to network clients, the first step in using a Winsock control is to determine whether the control will act as a client or as a server.

Winsock server applications using the TCP/IP protocol set the Protocol property to sckTCPProtocol and the LocalPort property to the port number the control will use to receive data. After the port number is set, the Winsock control can listen for data arriving at the port. To configure a Winsock control to listen on the port, call the Listen method.

When a client attempts to connect to the server application, the Winsock control generates the ConnectionRequest event. To accept the connection request, call the Accept method. If the Winsock control can successfully accept the connection, it starts the Connect event and sets the RemoteHostIP and RemotePort properties with the IP address and port number of the connecting client. After a connection is made, the control sets the State property to sckConnected. You then can begin to transfer data.

A connected Winsock control receives data through the connection. When data is sent to the server, the Winsock control generates the DataArrival event, indicating that the data can be read from the control. To read the incoming data from the control, you can use the GetData method.

To send data through a connected Winsock control, you can use the SendData method. As the data is sent through the control to the IP address and port number specified in the RemoteHostIP and RemotePort properties, the Winsock control generates the SendProgress event to notify you of the number of bytes sent and the number of bytes left to send. When the data transfer is complete, the control generates the SendComplete event.

After all data transfer has been completed, you can close a TCP/IP connection by calling the Close method. When a connection closes, the control generates the Close event. In addition to the Connect and Close events, the Winsock control can initiate the Error event to notify you of network errors. Unlike control errors or code errors, network errors do not cause a run-time error, but only raise the Error event.

To create a client application, set the RemoteHost and RemotePort properties, then call the Connect method. Once the connection to the server is established, you can transfer data as usual by using the SendData and GetData methods.

In addition to the TCP/IP protocol, the Winsock control supports sending data over an infrared port using the IrDA protocol. To use IrDA, set the Protocol property to 2, then set the ServiceName property to a string. The string represents the service name for an IrDA connection. IrDA uses a service name to establish a connection instead of port numbers such as TCP. A client connecting to an IrDA server application must use the same ServiceName string as the server. In other respects, the process of creating a server or client for IrDA is identical to creating a server or client for the TCP/IP protocol.