Creating TCP Servers and Clients
The following image demonstrates how to use the LabWindows/CVI TCP Support functions to create a TCP server and client.

The communication process begins when an application registers itself as a valid server. The client then connects to the server using the port number specified in the server registration function. When the client connects to the server, the server application receives the TCP_CONNECT event. Once the connection is established, both the server and the client applications can receive TCP_DATAREADY events, which indicate to the applications to call their respective read functions. When the server disconnects from the client, the client application receives the TCP_DISCONNECT event.
Similarly, when the client disconnects from the server, the server application receives the TCP_DISCONNECT event. The client and server applications also can receive the TCP_DISCONNECT event if the connection terminates because of an error.
![]() |
Note Unlike TCP servers, it is not necessary for TCP clients to explicitly specify or register a port for the connection. However, because of the presence of network firewalls and so on, you might want to specify the local host port the client program uses to establish a connection to the TCP server. To specify the client port, you must use the ConnectToTCPServerEx function, which includes the clientPort parameter. This parameter is different from the serverPort parameter, which you must specify correctly no matter which function you use. If you specify the client port explicitly, you must make sure that other TCP programs are not using that client port. National Instruments recommends that you not explicitly specify the client port unless it is necessary to use a specific client port. Use TCP_ANY_LOCAL_PORT if you do not need to specify the client port explicitly. |
You can use TCP only for one-to-one communication. You can connect a server or client application to several other client or server applications concurrently; however, only one server and one client can exchange data in a single communication session. In the LabWindows/CVI TCP Support Library, a conversation handle represents an individual communication session. In a client application, the ConnectToTCPServer and ConnectToTCPServerEx functions return conversation handles when the client connects to the server application. In a server application, the LabWindows/CVI TCP Support Library passes the conversation handle to the TCP callback function when the function receives a TCP_CONNECT event, which occurs when a new client connects to the server. Note that 0 is a valid conversation handle.