OCaml非阻塞客户端套接字 [英] OCaml non-blocking client socket
本文介绍了OCaml非阻塞客户端套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有一种以非阻塞方式使用客户端套接字的方法.
Is there a way to use a client socket in a non blocking way.
例如,如果我为客户端创建了一个套接字以在服务器上进行连接,并且在该套接字上进行了递归recv
,则在没有数据发送且连接正常时,Unix.recv
的最后一次调用将被阻止没有被服务器关闭.
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.
在C语言中,您可以为两个都指定标志:
In C, you can specify flags for both :
-
socket()
并使用SOCK_NONBLOCK
标志与套接字类型进行或"操作
带有 - 或
receiv()
.
MSG_DONTWAIT
标志的socket()
and use theSOCK_NONBLOCK
flag ORed with the socket type- or
receiv()
with theMSG_DONTWAIT
flags.
我看过这里:
- 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
- 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.
推荐答案
使用 Unix.set_nonblock
和 Unix.clear_nonblock
.
这篇关于OCaml非阻塞客户端套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文