C/E + + network programming sets the socket to blocking and non-blocking in Windows and Linux
In the socket programming, the socket read and write default is blocked, but in some cases we need to set it to non-blocking, such as multi-multiplexing, or through select to achieve the connection timeout, such as the socket is set to non-blocking, The interface between Windows and Linux is different, using the Ioctlsocket function in Windows, using the FCNTL function in Linux, let's do a cross-platform setup blocking function Setblock.
BOOL Setblock (int sock,bool isblock) {int re = 0;//is distinguished from Windows and Linux by macro, if the WINDOWS64-bit program determines _WIN64 macro #ifdef win32unsigned Long UL = 0;if (!isblock) ul = 1;re = ioctlsocket (sock, Fionbio, (unsigned long*) &ul); #else//First fetch to the existing descriptor property to ensure that this change does not change the original attribute int fl AGS = Fcntl (sock, F_GETFL, 0); if (Flags < 0) {return false;} if (isblock) {flags = flags & ~o_nonblock;} Else{flags = Flags | O_nonblock;} Re = fcntl (sock, F_SETFL, flags); #endifif (Re! = 0) return False;return true;}
More content can search online "Xia Chaojun" to get my course
More information can also focus on my 51CTO video course
650) this.width=650; "alt=" C++socket network Programming Daquan actual combat HTTP Server (PHP support) Video course "src=" https://s1.51cto.com/images/201702/ 49f7c1b034a686ddc50759bf223c5b88ac08f7_big.png "title=" c++socket network Programming Daquan actual combat HTTP Server (PHP support) Video course "height=" "width=" "/>
http://edu.51cto.com/index.php?do=lesson&id=153268
This article from "Xia Chaojun" blog, declined reprint!
C/E + + network programming sets the socket to blocking and non-blocking in Windows and Linux