PageRenderTime 49ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/doc/t-error.htm

https://bitbucket.org/marto1/udtwrap
HTML | 48 lines | 42 code | 6 blank | 0 comment | 0 complexity | 871d044ea9a5c6945f750000fcd1a7bf MD5 | raw file
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Introduction</title>
  6. <link rel="stylesheet" href="udtdoc.css" type="text/css" />
  7. </head>
  8. <body>
  9. <div class="ref_head">&nbsp;UDT Tutorial</div>
  10. <h3><font color="#000080">Error Handling</font></h3>
  11. <p>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
  12. value against these two constants (several routine return false as error value).</p>
  13. <p>On error, <a href="error.htm"><strong>getlasterror</strong></a> 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. <a href="error.htm"><strong>getlasterror</strong></a> returns an <a href="structure.htm"><strong>ERRORINFO</strong></a> structure, it contains both the error code and special text error message. Two helper functions of <a href="structure.htm"><strong>getErrorCode</strong></a> and <a href="structure.htm"><strong>getErrorMessage</strong></a> can be used to read these
  14. information.</p>
  15. <p>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.</p>
  16. <p>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. <a href="error.htm"><strong>getlasterror</strong></a> only provides detailed information when necessary. However, application can use <strong>getlasterror().<a href="structure.htm">clear()</a></strong> to clear the previously logged error if needed. </p>
  17. <p><strong>Example</strong>: check UDT::bind error.</p>
  18. <div class="code">
  19. sockaddr_in my_addr;<br>
  20. my_addr.sin_family = AF_INET;<br>
  21. my_addr.sin_port = htons(21); //invalid port number<br>
  22. my_addr.sin_addr.s_addr = INADDR_ANY;<br>
  23. memset(&(my_addr.sin_zero), '\0', 8);<br>
  24. <br>
  25. UDTSOCKET serv = UDT::socket(AF_INET, SOCK_STREAM, 0);<br>
  26. if (UDT::ERROR == UDT::bind(serv, (sockaddr*)&my_addr, sizeof(my_addr)))<br>
  27. {<br>
  28. &nbsp;&nbsp;cout << "bind: " << UDT::getlasterror().getErrorMessage();<br>
  29. &nbsp;&nbsp;// further action may depends on UDT::getlasterror().getErrorCode().<br>
  30. &nbsp;&nbsp;// system level error can be accessed through "errno"<br>
  31. &nbsp;&nbsp;return 0;<br>
  32. }
  33. </div>
  34. <p>In the example above, the output will be:</p>
  35. <div class="code">
  36. error message: Couldn't set up network connection: Permission denied.
  37. </div>
  38. <p>The UDT error code only reflects the operation error of the UDT socket level. Applications can still read the system level error (e.g., <u>errno</u> in Linux, <u>GetLastError</u> in Windows) to read
  39. more specific error information. However, the error message obtained by getErrorMessage contains information of both the UDT level error and the system level error.</p>
  40. <p>&nbsp;</p>
  41. </body>
  42. </html>