简体   繁体   English

在LINUX上打开C中的O_NONBLOCKING

[英]Turning on O_NONBLOCKING in C on LINUX

***Background**** I am new to TCP so I have question that may be a bit basic. ***背景****我是TCP的新手,所以我的问题可能有点基础。 I am trying to turn on O_NONBLOCK on a socket that is receiving data. 我试图在接收数据的套接字上打开O_NONBLOCK。 Up until now I have tried to different ways to set O_NONBLOCK, itcl() and fcntl(). 到目前为止,我尝试了不同的方法来设置O_NONBLOCK,itcl()和fcntl()。 Right now I am trying to get fcntl() to work. 现在我想让fcntl()工作。

My Question: Are you supposed to set O_NONBLOCK before you connect the socket or after? 我的问题:您是否应该在连接套接字之前或之后设置O_NONBLOCK?

**my current implementation of fcntl() is based off off the link before the code: **我当前的fcntl()实现基于代码之前的链接:

How to reset a socket back to blocking mode (after I set it to nonblocking mode)? 如何将套接字重置回阻塞模式(在我将其设置为非阻塞模式后)?

//set socket to NONBlocking
on = fcntl(Socket,F_GETFL);
on = (on | O_NONBLOCK);
if(fcntl(Socket,F_SETFL,on) < 0)
    {
       perror("turning NONBLOCKING on failed\n");
    }

// DO CONNECT
rc = connect()

Thank you for taking the time to look at this 感谢您抽出宝贵时间来看看这个

You are supposed to set O_NONBLOCK whenever you want. 你应该随时设置O_NONBLOCK If you do it before connect , then connect will be non-blocking as well (returning EINPROGRESS ; you can select or poll for writable state to wait for its completion). 如果在connect之前执行此操作,则connect也将是非阻塞的(返回EINPROGRESS ;您可以selectpoll可写状态以等待其完成)。

回答第二部分,使用相同的代码,但关闭O_NONBLOCK位而不是打开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM