fnTCP_Listen()

USOCKET fnTCP_Listen(USOCKET TCP_socket, unsigned short usPort, unsigned short usMaxWindow);

TCP_socket is the number of the TCP socket that should be set to listening mode.

usPort is the port number that the TCP socket should listen on (eg. HTTP_SERVERPORT for HTTP port 80).

usMaxWindow is the size of the receive TCP window that is advertised by the TCP socket. If the value is left at 0 the value TCP_MAX_WINDOW_SIZE will be used, which is the maximum size that a single Ethernet buffer can receive. Greater values are generally only used together with buffered TCP connections where the TCP socket has additional memory allocated for use as TCP reception window buffer. The TCP socket advertises the maximum value at TCP connection only when the project define ANNOUNCE_MAX_SEGMENT_SIZE is active. Furthermore, TCP reception windows size is only managed during data transfer when the project defined CONTROL_WINDOW_SIZE is active.


The routine is used to set a TCP socket to the listening state. It returns the number of the allocated socket, if there was no problem.
A negative return values indicate failure as follows: In the listening state the TCP socket acts as a TCP server and waits for a connection from a remote client. A socket in teh listening state can however also be used to establish a TCP connection if required (peer-to-peer type use).

Since a TCP socket is reset when its connection closes it is typical to set a server back to listening mode on each close so that it can subsequently accept further connections.

Example

The following example shows a HTTP socket being set back to listening mode after a previous connection has closed.
static int fnHTTPListener(USOCKET Socket, unsigned char ucEvent, unsigned char *ucIp_Data, unsigned short usPortLen)
{
    ....
            case TCP_EVENT_CLOSED:
                ...
                fnTCP_Listen(Socket, HTTP_SERVERPORT, 0);            // go back to listening state
                break;
	....
}


See the following forum thread for additional details about working with TCP sockets: µTasker forum TCP discussion.

Related functions

fnGetTCP_Socket();
fnReleaseTCP_Socket();
fnGetTCP_state();
fnTCP_Activity();
fnTCP_Connect();
fnTCP_close();




Please use the µTasker forum to ask specific questions.