简体   繁体   中英

Linux C++ TCP Socket - Enable Blocking Mode

Amazingly, I have had a hard time finding an answer to this..

I have a TCP client socket that I can successfully connect with and send data through. However, after sending data, I'm expecting a response to be returned from the server. I checked my socket and it would appear that it is in non blocking mode.

if (fcntl(sc->connect_d, F_GETFL) && O_NONBLOCK)
{
//non blocking
}

What is the macro for enabling blocking mode so I can read the server response a little easier? Can somebody give me a small snippet that can do this? Thanks

if (fcntl(sc->connect_d, F_GETFL) && O_NONBLOCK)

The above code is incorrect. It should be:

if (fcntl(sc->connect_d, F_GETFL) & O_NONBLOCK)

Note that TCP sockets are created in blocking mode by default, so (assuming you created the socket yourself) you shouldn't need to do anything to "put it into" blocking mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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