UDT Tutorial

### Transfering Data using UDT

This section describes using UDT to transfer data in streaming mode. This is exactly the same as using traditional BSD socket.

In streaming mode, neither a send or a recv call can guarantee that all data are sent or received in one call, because there is no boundary information in the data stream. Application should use loops for both sending and receiving.

Example: send a data block (buf, size) using UDT.

int ssize = 0;
int ss;
while (ssize < size)
{
if (UDT::ERROR == (ss = UDT::send(usock, buf + ssize, size - ssize, 0)))
{
cout << "send:" << UDT::getlasterror().getErrorMessage() << endl;
break;
}
ssize += ss;
}

Similarily, to receive data stream, the following example code can be used.

Example: receive "size" of data into buffer "buf"

int rsize = 0;
int rs;
while (rsize < size)
{
if (UDT::ERROR == (rs = UDT::recv(usock, buf + rsize, size - rsize, 0)))
cout << "recv:" << UDT::getlasterror().getErrorMessage() << endl;
break;
}
rsize += rs;
}

##### Blocking vs. Non-blocking

UDT supports both blocking and non-blocking mode. The above example demonstrated the blocking mode. In non-blocking mode, UDT::send and UDT::recv will return immediately if there is no buffer available. Usually, non-blocking calls are used together with accept.

UDT also supports timed blocking IO with UDT_SNDTIMEO and UDT_RCVTIMEO. This is in the middle between complete blocking and complete non-blocking calls. Timed IO will block the sending or receiving call for a limited period. This is sometimes useful if the application does not know if and when the peer side will send a message.