C/E + + network programming sets the socket to blocking and non-blocking in Windows and Linux

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.