LabWindows/CVI

Content Type
Programming Language
Current manual

RegisterTCPServerEx

RegisterTCPServerEx

int RegisterTCPServerEx (unsigned int portNumber, tcpFuncPtr callbackFunction, void *callbackData, const char localhostAddress[]);

Purpose

Registers your program as a valid TCP server on the specified localhost address and port and allows other applications to connect to it for network communication.

Clients attempting to connect to your program must use the same port number. Thereafter, all requests by the client are routed through the specified server callback function. You can register your program as a TCP server multiple times as long as you specify different localhost address and port number combinations.

Note   You must process messages to receive events in your callback. A thread processes messages when it calls RunUserInterface, ProcessSystemEvents, GetUserEvent, or any of the built-in User Interface Library pop-up functions.

Parameters

Input
Name Type Description
portNumber unsigned int The port number under which your program is registered as a TCP server.

You can choose any port number in the range from 1 to 65,535. You can also pass zero if you want the operating system to use an unused port. If you pass zero, this function outputs the port number assigned by the system in the return value.
callbackFunction tcpFuncPtr Pointer to the synchronous callback function that processes client requests.

The callback function must be of the following form:

int (*tcpFuncPtr) (unsigned handle, int xType, int errCode, void *callbackData);

xType specifies the type of message the server sends. The server callback function can receive the following transaction types:

TCP_CONNECT—Received when a client requests a connection.
TCP_DISCONNECT—Received when a client requests the termination of a connection or when a connection terminates because of an error. If the connection terminates because of an error, the errCode parameter contains a negative error code.
TCP_DATAREADY—Received when there is data to be read by the server. Your program, acting as the server, calls ServerTCPRead to obtain the data. Note that this event is received again if you do not read all the available data.

Use errCode only when the transaction type is TCP_DISCONNECT.

Note    The return value of the callback is ignored. Server callbacks should be short and should return as soon as possible.
callbackData void * A pointer-width value that the TCP Support Library passes to the callback function each time the library invokes the callback for the same server.

You must define the meaning of the callback data. The following examples show how you can use the callback data.

  • You can register your program as a TCP server multiple times under different port numbers. You can use the same callback function for all the server instances by using the callback data to differentiate between them.
  • You can use the callback data as a pointer to a data object that you need to access in the callback function. This way, you can avoid declaring the data object as a global variable.

You can pass zero if you do not want to use the callback data.
localhostAddress const char [] The address of the localhost network interface to be used for the connection. Use one of the addresses returned by the GetAllTCPHostAddresses function or the corresponding DNS name.

You can pass NULL or an empty string if you have only one network interface.

Return Value

Name Type Description
status int Return value indicating whether the function was successful. Unless otherwise stated, zero represents successful execution and a negative number represents the error code.

You can call the GetTCPSystemErrorString function to obtain a system message that describes the error. The system messages can be more descriptive than the TCP Support Library error codes. To obtain the correct system error message, you must call GetTCPSystemErrorString immediately after calling the TCP Support Library function that failed.

For RegisterTCPServer and RegisterTCPServerEx, the return value is the port number assigned by the system if you passed zero for the port and the function was successful.

For functions that read or write data (ClientTCPRead, ClientTCPWrite, ServerTCPRead, ServerTCPWrite), if the function was successful, the return value is the number of bytes transferred.

You can have a maximum of 255 concurrent conversations and up to 1,024 connections. If you exceed this limit, -kTCP_TooManyConnections will be returned. You may not be able to open the maximum number of connections allowed by LabWindows/CVI because of limitations imposed by the operating system.

Additional Information

Library: TCP Support Library

Include file: tcpsupp.h

LabWindows/CVI compatibility: LabWindows/CVI 7.1 and later

Was this information helpful?