TCP Library Chapter 7
LabWindows/CVI Standard Libraries 7-2 © National Instruments Corporation
TCP Clients and ServersNetwork communication using the TCP library involves a client and a server in each connection.
A TCP server can send and receive information to and from a client application through a
network. A TCP client can send and request data to and from a server application. Once
registered, a server waits for clients to request connection to it. A client, however, can only
request connection to a pre-existing server.
With the LabWindows/CVI TCP Library, you can write programs to act as a TCP client or
server. Under Windows, you cannot run both a server and a client on the same computer. The
procedure for writing a program using TCP is similar to the procedure followed for using DDE.
Refer to the sample program discussion in Chapter 6, DDE Library. Two additional sample
programs, TCPSERV.PRJ and TCPCLNT.PRJ, provide some guidelines on structuring your
TCP programs as a server or client. These programs are provided as templates only, and will
require modification for operation on your machine.
To connect to a TCP server from a LabWindows/CVI program, you must have some information
about the application to which you would like to connect. All TCP server applications must run
on a specified host, which either has a known host name (for example, aaa.bbb.ccc) or a
known IP address (for example, 123.456.78.90) associated with it. In addition, each server
specifies its own unique port number. These two pieces of information identify different servers
either on the same machine or on different machines. Before any client program can connect to a
server, it has to know the host name and server port number.
If your program is to act as a TCP server, you must call the RegisterTCPServer function in
your program. The RegisterTCPServer function establishes your program as the server
associated with a port number on the local host. Client applications can connect to your program
by using either the host name (where the server application is currently running) or the IP
address, and the port number associated with the server application. The callback function is
invoked whenever the conversation partner requests communication. This is discussed in the
following section.
The TCP Callback FunctionCallback functions provide the mechanism for receiving notification of connection, connection
termination, and data availability. Similar to the method in which callback function responds to
user interface events from your User Interface Library object files, a TCP callback function
responds to incoming TCP messages and information.
As shown in Table 7-2, a callback function can respond to three types of TCP messages:
TCP_CONNECT, TCP_DISCONNECT, and TCP_DATAREADY.
TCP callback functions, used in a program acting as a TCP server, can be triggered in a number
of ways from client applications. Whenever a client application attempts to connect to your
server program or requests information from your program, the callback function in your