fnGetFreeTCP_Port()

unsigned short fnGetFreeTCP_Port(void);

This routine can be used to obtain a TCP port number for use by a socket.

If the project define RANDOM_NUMBER_GENERATOR is active it will return the first number as random value from the dynamic port range 49152 to 65535 and subsequent calls will return the next incremented value until the value overflows from 65535 back to 49152. Without RANDOM_NUMBER_GENERATOR the range is simply stepped through starting at 49152.

The routine is automatically used by TCP clients when they connect using fnTCP_Connect(). It is otherwise not generally required to be called directly by application code. The FTP server uses the call to define a TCP port number to be used by its data connection in passive mode, which is an example of where the function is useful outside of the TCP layer.

Example

This example sets a TCP socket to listening mode on the next free TCP port. In the case of FTP the number of this port is sent to the client via a telegram on the FTP control connection so that the client knows which TCP port it is to connect on. The use of unknown TCP ports to listen on is of little interest in general situations.

    usFTP_DataPort = fnGetFreeTCP_Port();                    // get the next free TCP port
    fnTCP_Listen(FTP_TCP_Data_socket, usFTP_DataPort, 0);    // start listening on the TCP port


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

Related functions

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




Please use the µTasker forum to ask specific questions.