LabWindows/CVI

Content Type
Programming Language
Current manual

RegisterTCPServer

RegisterTCPServer

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

Purpose

Note Note  This function has been superseded by RegisterTCPServerEx.

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 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.

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 3.0 and later

Examples

Refer to the following examples that use the RegisterTCPServer function:

  • apps\finger\finger.cws

    Open example
  • tcp\MultiClientServer.cws

    Open example
  • tcp\MultiThreading.cws

    Open example
  • tcp\server.cws

    Open example
Was this information helpful?