Io non-blocking operations
Sock methods do not have to be congested or non-blocking operations. There are two ways to set fcntl and the parameters of the corresponding function.
Server:
# Include <sys/socket. h> # include <stdio. h> # include <string. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <stdlib. h> # include <errno. h> # define bufsize 128int main (INT argc, char * argv []) {int server_sockfd, client_sockfd; int server_len, client_len; struct sockaddr_in server_address; struct sockaddr_in client_address; int I, byte; char char_send [bufsize]; server_sockfd = socket (af_inet, sock_stream, 0); bzero (& server_address, sizeof (server_address); server_address.sin_family = af_inet; server_address.sin_port = htons (7838); region = inaddr_any; server_len = sizeof (server_address); If (BIND (server_sockfd, (struct sockaddr *) & server_address, server_len )) =-1) {perror ("bind"); exit (exit_failure);} Listen (server_sockfd, 5); printf ("server waiting for connect \ n "); client_len = sizeof (client_address); client_sockfd = accept (server_sockfd, (struct sockaddr *) & client_address, (socklen_t *) & client_len); for (I = 0; I <5; I ++) {memset (char_send, '\ 0', bufsize); printf ("input message to send:"); fgets (char_send, bufsize, stdin ); if (byte = Send (client_sockfd, char_send, strlen (char_send), 0) =-1) {perror ("send"); exit (exit_failure );} memset (char_send, '\ 0', bufsize); // set byte = Recv (client_sockfd, char_send, bufsize, msg_dontwait) for non-blocking parameters; If (byte> 0) {printf ("Get % d message: % s", byte, char_send); byte = 0;} else if (byte <0) {If (errno = eagain) {errno = 0; continue;} else {perror ("Recv"); exit (exit_failure) ;}} Shutdown (client_sockfd, 2); Shutdown (server_sockfd, 2 );}
Client:
# Include <stdio. h> # include <string. h> # include <errno. h> # include <sys/socket. h> # include <resolv. h> # include <stdlib. h> # include <netinet/in. h> # include <ARPA/inet. h> # include <unistd. h> # include <fcntl. h> # define maxbuf 128int main (INT argc, char ** argv) {int sockfd, RET, I; struct sockaddr_in DEST, mine; char buffer [maxbuf + 1]; if (sockfd = socket (af_inet, sock_stream, 0) <0) {perror ("socket"); exit (Exit_failure);} bzero (& DEST, sizeof (DEST); DeST. sin_family = af_inet; DeST. sin_port = htons (7838); If (argc <2) {printf ("Usage: % S <DEST ip> <SRC ip>", argv [0]); exit (1);} If (inet_aton (argv [1], (struct in_addr *) & DeST. sin_addr.s_addr) = 0) {perror (argv [1]); exit (1);} bzero (& mine, sizeof (mine); mine. sin_family = af_inet; mine. sin_port = htons (7839); If (inet_aton (argv [2], (struct in_addr *)& Mine. sin_addr.s_addr) = 0) {perror (argv [2]); exit (exit_failure);} If (BIND (sockfd, (struct sockaddr *) & mine, sizeof (struct sockaddr) =-1) {perror (argv [3]); exit (exit_failure);} printf ("will connect! \ N "); If (connect (sockfd, (struct sockaddr *) & DEST, sizeof (DEST ))! = 0) {perror ("Connect"); exit (exit_failure);} // set the non-blocking if (fcntl (sockfd, f_setfl, o_nonblock) of the sock connection =-1) {perror ("fcntl"); exit (exit_failure);} while (1) {bzero (buffer, maxbuf + 1); // because it has been set in the socket, therefore, you do not need to set ret = Recv (sockfd, buffer, maxbuf, 0); If (Ret> 0) {printf ("Get % d message: % s", RET, buffer); ret = 0;} else if (Ret <0) {If (errno = eagain) {errno = 0; continue;} else {perror ("Recv "); exit (exit_failure) ;}} memset (buffer, '\ 0', maxbuf + 1); printf ("input message to send:"); fgets (buffer, maxbuf, stdin); If (ret = Send (sockfd, buffer, strlen (buffer), 0) =-1) {perror ("send "); exit (exit_failure) ;}close (sockfd); Return 0 ;}
This blog from a shura Road, reproduced please note the Source: http://blog.csdn.net/fansongy/article/details/6898577