UDT Reference: CUDT Methods

open

The open method initializes a UDT entity with an attempt to use the given port number and/or IP address .

int open(
   const int& port = 0
);

 

int open(
   const char* ip,

   const int& port = 0
);

Parameters

port
[in] The port number that the UDT entity is expected to be bound to.
 
ip
[in] The IP address that the UDT entity is expected to be bound to on a multi-homed host.

Return Values

If no error occurs, open returns the actual port number that the UDT entity is using.

Exceptions

A CUDTException exception is threw out under two kinds of situations. One is that the assigned port number is not available but the application has set the UDT_PCH option as false. The other is that the assigned IP address does not exist.

The default port number is 0, which means UDT can choose any available port number.

Description

The open method initializes the system variables related to sequence numbers and the protocol data structures. Especially, the open method binds the UDP socket to an IP address and a port number. If the second format of open is called, UDT tries to bind to address ip, otherwise it uses the first address found by getaddrinfo.

Port number may not always be cared of by applications, especially at the client side. This is left as the default option in UDT. To bind UDT entity to a fixed port number, the application MUST first set the UDT_PCH as false, then uses a loop to open a UDT entity until no exception is threw out. Refer to the example in this page.

The returned port number should be used for a peer side to connect to. Similarly, if the IP address is assigned, the peer side MUST also connect to the ip address also.

Examples

The following codes show how to bind a server UDT entity to given IP address and port number:

CUDT udt;

char ip = "206.220.241.13";

int port = 7001;

bool opened = false;

do

{

   try

   {

       udt.open(ip, port);

       opened = true;

   }

   catch (CUDTException e)

   {

       // Break if the failure is NOT caused by unavailable port number

   }

} while (!opened);

See Also

listen, connect, setOpt, getOpt