Network Programming (2) Socket synchronous non-blocking Working Mode

Source: Internet
Author: User
The non-blocking mode of synchronization is higher than the previously mentioned synchronous blocking mode, although it is limited. in this mode, the send or recv function is set to MSG_DONTWAIT, that is, non-blocking. Even if there is no such function, it will skip the subsequent steps. For example, if multiple sockets exist, you can check whether other sockets can receive data. Then check the previous execution repeatedly.

The non-blocking mode of synchronization is higher than the previously mentioned synchronous blocking mode, although it is limited. in this mode, the send or recv function is set to MSG_DONTWAIT, that is, non-blocking. Even if there is no such function, it will skip the subsequent steps. For example, if multiple sockets exist, you can check whether other sockets can receive data. Then check the previous execution repeatedly.

The non-blocking mode of synchronization is higher than the previously mentioned synchronous blocking mode, although it is limited. In this mode, the send or recv function is set to MSG_DONTWAIT,

That is, it is non-blocking. Even if there is no value, it will skip the next step. For example, if multiple sockets exist, you can check whether other sockets can receive data. Check the preceding execution status repeatedly until there is data.

The advantage of this mode is that it does not block, but it consumes too many system resources because it constantly checks and does a lot of unnecessary calls.

I. Socket Client example
The header files used are in network programming (1) cross-platform Socket synchronous blocking working mode.

The Code is as follows:

/*************************************** * ********* Author: xiongchuanliangDescription: Synchronous non-blocking working mode example _ client code compilation command: Linux: g ++-o tcpclientnoblk. cpp-m64-I. /common ************************************** * *********** // client code # include
 
  
# Include
  
   
# Include
   
    
# Include "initsock. h "# include" common. h "int main (int argc, char * argv []) {int sclient = 0; // socket to connect to the server int flags = 0; // fcntl return identifier int recvbytes = 0; // The length of data returned by the server char recvData [MAXDATASIZE] = {0 }; // Save the data returned by the server // retrieve the input parameter const size_t MaxLen = 500; char testMsg [MaxLen] = {0}; if (argc <3) {printf ("Usage: % s [ip address] [any string] \ n ", argv [0]); exit (EXIT_FAILURE);} else {strncpy (testMsg, argv [2], strlen (argv [2]); printf ("Message: % s \ n", testMsg);} // create a socket sclient = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP ); if (sclient = INVALID_SOCKET) {PrintError ("invalid () failed"); exit (EXIT_FAILURE);} // specify the server address to connect to and the port struct sockaddr_in server_addr; server_addr.sin_family = AF_INET; servers = htons (SERVPORT); inet_ton (AF_INET, argv [1], & volumes); // the ip address parameter memset (& (server_addr.sin_zero) entered by the user ), 0, 8); // set to non-blocking mode // Method 1: flags = fcntl (sclient, F_GETFL, 0); // obtain the socket descriptor flag if (flags <0) {PrintError ("fcntl () failed"); exit (EXIT_FAILURE);} fcntl (sclient, flags | O_NONBLOCK); // change the socket descriptor identifier. // Method 2: // # include
    
     
// # Include
     
      
// Ioctl (sclient, FIONBIO, & flags); // connect the socket to the server if (connect (sclient, (struct sockaddr *) & server_addr, sizeof (struct sockaddr )) =-1) {PrintError ("connect () failed"); exit (EXIT_FAILURE);} // synchronous non-blocking mode -- send data to the server while (send (sclient, testMsg, strlen (testMsg), MSG_DONTWAIT) =-1) {sleep (10); printf ("send () sleep (10) \ n ");} printf ("send success! \ N "); // synchronous non-blocking mode -- receives the returned data while (recvbytes = recv (sclient, recvData, MAXDATASIZE, MSG_DONTWAIT) =-1) {sleep (10); printf ("recv () sleep (10) \ n");} printf ("recv success! \ N "); if (recvbytes = 0) {printf (" recv () no data! \ N ");} else if (recvbytes <0) {PrintError (" recv () failed ");} else {recvData [recvbytes] = '\ 0 '; printf ("recv: % s \ n", recvData);} // close the socket and end the TCP session close (sclient); exit (EXIT_SUCCESS );}
     
    
   
  
 

II. Summary
It is set to non-blocking mode, mainly to change the properties of the Socket. And set MSG_DONTWAIT.
There are two methods to change the Socket property:
Method 1:
Flags = fcntl (sclient, F_GETFL, 0); // obtain the socket descriptor Identifier if (flags <0) {PrintError ("fcntl () failed"); exit (EXIT_FAILURE );} fcntl (sclient, flags | O_NONBLOCK); // change the socket descriptor identifier

Method 2:
#include 
 
    #include 
  
     ioctl(sclient,FIONBIO,&flags);
  
 

At the same time, in the code, we can see that both send and recv are placed in while, and the returned values are continuously checked. Efficiency is not high.


MAIL: xcl_168@aliyun.com

BLOG: http://blog.csdn.net/xcl168

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.