OCaml non-blocking client socket

一笑奈何 提交于 2019-12-25 12:35:05

问题


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 :

  • http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEsocket_bool_option
  • http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEsocket_type
  • http://caml.inria.fr/pub/docs/manual-ocaml/libref/Unix.html#TYPEmsg_flag
  • https://ocaml.github.io/ocamlunix/ocamlunix.html#sec119

But I could not find any informations on this.


回答1:


Use Unix.set_nonblock and Unix.clear_nonblock.



来源:https://stackoverflow.com/questions/39613902/ocaml-non-blocking-client-socket

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!