# TCP Callback Function

Callback functions provide the mechanism for receiving notification of connection, connection termination, and data availability. Similar to the method in which a callback function responds to user interface events from User Interface Library object files, a TCP callback function responds to incoming TCP messages and information.

A callback function can respond to three types of TCP messages: TCP_CONNECT, TCP_DISCONNECT, and TCP_DATAREADY.

If a program acts as a TCP server, client applications can trigger the TCP callback function in a number of ways. Whenever a client application attempts to connect to the server program or requests information from the program, the TCP Support Library invokes the callback function to process the request.

The parameter prototype for the TCP callback function in LabWindows/CVI is defined as follows:

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

## Parameters

Name | Type | Description
handle | unsigned | Conversation handle.
xType | integer | Transaction type.
errCode | integer | When xType is TCP_DISCONNECT, errCode is negative if the connection terminates because of an error.
callbackData | void pointer | A user-defined data value.

The following table lists the TCP transaction types, xType, that can trigger a callback function.

## TCP Transaction Types (xType)

xType | Server Can Respond To? | Client Can Respond To? | Occurs When?
TCP_CONNECT | Y | N | When a new client requests connection.
TCP_DISCONNECT | Y | Y | When a conversation partner quits 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 | Y | Y | When a conversation partner sends data. 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.

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

Refer to RegisterTCPServer and ConnectToTCPServer for more information about the TCP callback function.