lwip - how to implement non-blocking calls

 

Hi,

I am using lwIP on BF527 and I want to know how I can get my socket to be       nonblocking for the recv() function?

Thanks,

Kaushal



    • Hi,


      "By default, TCP sockets are in "blocking" mode. For example, when you call recv() to read from a stream, control isn't returned to your program until at least one byte of data is read from the remote site. This process of waiting for data to appear is referred to as "blocking". The same is true for the write() API, the connect() API, etc. When you run them, the connection "blocks" until the operation is complete.

      Its possible to set a descriptor so that it is placed in "non-blocking" mode. When placed in non-blocking mode, you never wait for an operation to complete. This is an invaluable tool if you need to switch between many different connected sockets, and want to ensure that none of them cause the program to "lock up."

      If you call "recv()" in non-blocking mode, it will return any data that the system has in it's read buffer for that socket. But, it won't wait for that data. If the read buffer is empty, the system will return from recv() immediately saying ``"Operation Would Block!"''. "

      However, the provided Blackin lwIP port does not support non-blocking recv function calls.

      Instead, you can use the select() call on a port to find if the data is ready. The advantage of using select is that you can provide a timeout - so select() will return back to the program call after a given timeout and depending if the data is ready or not, you can issue recv() call. The default timeout for select() call is infinite so be sure to set the timeout. In the event that the data is ready, the recv() will return right away.

      http://forge.novell.com/modules/xfref_library/detail.php?reference_id=180 shows you how to use select() as a non-block alternative to accept().

      From: http://www.developerweb.net/forum/showthread.php?t=3196

      " If you are using a non-blocking socket and then call connect, it should return -1 and errno=EINPROGRESS.

      Then select the socket for writing with the timeout. After select() indicates it is "writteable", it has connected (or an error has occurred) so check with getsockopt() the SO_ERROR at the SOL_SOCKET level... It should be zero if the connection was successful, any other value is what errno should be if it was a blocking connect().

      If the timeout period expires, then you should be getting a 0 from select()... "

      I hope this gets you going.

      Thanks,


      Kaushal