非阻塞套接字 [英] NON BLOCKING Socket
问题描述
一个非阻塞套接字是一个我们称之为的fcntl()方法和O_NONBLOCK标志与它相关联。任何一个可以告诉我,其他人需要什么来CONVER正常TCP_IP插座成非阻塞套接字?
A non blocking socket is the one where we call fcntl() method and associate the O_NONBLOCK flag with it. Can any one tell me what else is required to conver a normal TCP_IP socket into a non blocking socket?
如果不堵,插座均采用与Windows服务器很好地工作可能会出现什么问题?
What problems may arise if non-blocking sockets are made to work very well with Windows servers ?
萨钦
推荐答案
例初始化为Linux可能是这样的:
Example init for linux may look like this:
int flags;
s = socket(PF_INET, SOCK_STREAM, IPPROTO_IP) // ret 5
setsockopt(s, SOL_TCP, TCP_NODELAY, [1], 4)
setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, [1], 4)
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, [1], 4)
flags = fcntl(s,F_GETFL,0);
assert(flags != -1);
fcntl(s, F_SETFL, flags | O_NONBLOCK);
connect(s, {sa_family=AF_INET, sin_port=htons(5001), sin_addr=inet_addr("192.168.0.68")}, 16)
从太阳基本白皮书:
Basic white paper from sun:
在窗户使用重叠的IO插槽来获得非阻塞网络。
On windows You use Overlapped IO sockets to get non blocking networking.
在Linux上使用epoll的()。
On Linux use epoll().
在Solaris上插座()。
On solaris socket().
注意读取或写入套接字时,它没有准备好。
(选择输出),因为你可能会收到EAGAIN错误。
Be aware to read or write to a socket when it's not ready. (select-output) Because You may receive EAGAIN error.
大跨平台库(但C ++和新的C ++标准的候选人)是提高:: ASIO。
Great cross platform library (but C++ and new c++ standard candidate) is boost::asio.
它采用原生系统的异步方法。
It uses native system asynchronous methods.
这篇关于非阻塞套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!