Introduction

UDT Tutorial

### Error Handling

All UDT API will return an error upon a failed operation. Particularly, UDT defines UDT::INVALID_SOCK and UDT::ERROR as error returned values. Application should check the return value against these two constants (several routine return false as error value).

On error, getlasterror can be used to retrieve the error information. In fact, the function returns the latest error occurred in the thread where the function is called. getlasterror returns an ERRORINFO structure, it contains both the error code and special text error message. Two helper functions of getErrorCode and getErrorMessage can be used to read these information.

The UDT error information is thread local (that is, an error in another thread will not affect the error information in the current thread). The returned value is a reference to the UDT internal error structure.

Note that a successful call will NOT clear the error. Therefore, applications should use the return value of a UDT API to check the result of a UDT call. getlasterror only provides detailed information when necessary. However, application can use getlasterror().clear() to clear the previously logged error if needed.

Example: check UDT::bind error.

sockaddr_in my_addr;
my_addr.sin_family = AF_INET;
my_addr.sin_port = htons(21); //invalid port number
my_addr.sin_addr.s_addr = INADDR_ANY;
memset(&(my_addr.sin_zero), '\0', 8);
UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0);
if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr)))
{
cout << "bind: " << UDT::getlasterror().getErrorMessage();
// further action may depends on UDT::getlasterror().getErrorCode().
// system level error can be accessed through "errno"
return 0;
}

In the example above, the output will be:

error message: Couldn't set up network connection: Permission denied.

The UDT error code only reflects the operation error of the UDT socket level. Applications can still read the system level error (e.g., errno in Linux, GetLastError in Windows) to read more specific error information. However, the error message obtained by getErrorMessage contains information of both the UDT level error and the system level error.