# RegisterTCPServer

int RegisterTCPServer (unsigned int portNumber, tcpFuncPtr serverCallbackFunction, void *callbackData);

## Purpose

Registers your program as a valid TCP server 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 port numbers.

| 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 integer | 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.
serverCallbackFunction | TCP function pointer | 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 pointer | A 4-byte 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.

## Return Value

Name | Type | Description
status | integer | 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 Library error codes. To obtain the correct system error message, you must call GetTCPSystemErrorString immediately after calling the TCP Library function that failed. 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.