Enabling O_NONBLOCKING in C on LINUX

*** background **** I am new to TCP, so I have a question that might be a little basic. I am trying to enable O_NONBLOCK on a socket that is receiving data. So far I've tried to set O_NONBLOCK, itcl () and fcntl () differently. I am currently trying to get fcntl () to work.

My question is: should you set O_NONBLOCK before or after socket connection?

** my current implementation of fcntl () is based on disabling the link before code:

How do I reset the socket back to blocking mode (after I set it to non-blocking 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()

      

Thanks for taking the time to look at this.

+3


source to share


2 answers


You have to install O_NONBLOCK

whenever you want. If you do this before connect

, then connect

it will also nonblock (return EINPROGRESS

; you can select

either poll

wait for the writeable state to complete).



+6


source


And to answer part two, use the same code, but turn off the O_NONBLOCK bit, not on.



0


source







All Articles