Linux Network Programming-non-blocking programming

Source: Internet
Author: User
Tags strcmp

Introduction to the design of non-blocking methods

The most significant difference between the non-blocking operation and the blocking mode is that the function's call returns immediately, regardless of whether the data was successfully read or successfully written. You can do non-blocking programming by using FCNTL () to set the socket file descriptor following code:
Fcntl (S, F_SETFL, O_nonblock);
where S is the socket file descriptor, using the F_SETFL command to set the socket s to non-blocking mode, and then read and write operations can be returned immediately.

Examples of non-blocking programming

The function accept () can use a non-blocking way to poll waiting for the arrival of the client before setting the Non_block mode. The following code uses polling to use the Accept () and recv () functions, when the client sends a "HELLO" string, sends an "OK" response to the client and shuts down the client, and when the client sends the "SHUTDOWN" string to the server, the server sends the "BYE" client, And close the client, and then quit the program.

#define PORT 9999#Define BACKLOG 4intMainintargcChar*argv[]) {structSockaddr_in local,client;intLenints_s =-1, s_c=-1;/ * Initialize the address structure * /Bzero (&local,sizeof(local));/ * Clear 0 * /Bzero (&client,sizeof(client));/ * Clear 0 * /local.sin_family = af_inet;    Local.sin_port = htons (port); LOCAL.SIN_ADDR.S_ADDR = htonl (-1);/ * Create a Socket descriptor * /s_s = socket (af_inet, Sock_stream,0);/ * Set non-blocking mode * /Fcntl (S_S,F_SETFL, O_nonblock);/ * Listen * /Listen (s_s, BACKLOG); for(;;) {/* Poll the receiving client * /         while(S_c <0){/ * Wait for client to arrive * /S_c =accept (s_s, (structsockaddr*) &client, &len); }/ * Polling for receive, exiting while loop when data is received * /         while(Recv (s_c, buffer,1024x768) <=0)            ;/ * Receive the client's data * /        if(strcmp (Buffer,"HELLO")==0){/ * Determine if it is a hello String * /Send (S,"OK",3,0);/ * Send response * /Close (S_c);/ * Close connection * /            Continue;/ * Continue waiting for Client connection * /}if(strcmp (Buffer,"SHUTDOWN")==0){/ * Determine if the string is shutdown * /Send (S,"BYE",3,0);/ * Send bye String * /Close (S_c);/ * Close the Client connection * /             Break;/ * Exit main loop * /}} close (s_s);return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Linux Network Programming-non-blocking programming

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.