0

Is there a way to use a client socket in a non blocking way.

For example, if I create a socket for a client to connect on a server and that I do recursive recv on that socket, the last call of Unix.recv will block when no data is send and if the connection is not closed by the server.

In C, you can specify flags for both :

  • socket() and use the SOCK_NONBLOCK flag ORed with the socket type
  • or receiv() with the MSG_DONTWAIT flags.

I have looked here :

But I could not find any informations on this.

cedlemo
  • 3,205
  • 3
  • 32
  • 50
  • 'Recursive `recv()`' doesn't mean anything. – user207421 Sep 21 '16 at 11:51
  • Yes, you are right, sorry, I wanted to say when you call `recv` in a recursive function until all the data are read. Maybe I should have say multiple call to `recv` until all the data are read. – cedlemo Sep 21 '16 at 11:56
  • 1
    Have you looked at Lwt (http://ocsigen.org/lwt/2.5.2/manual/)? It provides lots of tools for non-blocking operations. For example, `Lwt_unix.recv` (http://ocsigen.org/lwt/2.5.2/api/Lwt_unix#VALrecv) is a non-blocking equivalent to `Unix.recv`. – hcarty Sep 21 '16 at 16:25
  • @hcarty, no I have not looked at Lwt for this particular matter, but yes I will. – cedlemo Sep 21 '16 at 17:31

1 Answers1

1

Use Unix.set_nonblock and Unix.clear_nonblock.

Daniel Bünzli
  • 5,119
  • 20
  • 21
  • I guess, I will have to deal with the Unix error EINPROGRESS and EAGAIN. Any link to a code example maybe ? – cedlemo Sep 21 '16 at 12:30