In linux, is network programming non-blocking? Is the server unable to accept data?

Source: Internet
Author: User
Tags socket error
In linux, is network programming non-blocking? Is the server unable to accept data? -- Linux general technology-Linux technology and application information. For details, refer to the following section. /* This is the client code */
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Deprecision S_PORT 3333
# Define BUFFER 100.

Int main (int argc, char ** argv)
{
Struct sockaddr_in server_addr;
Int c_fd, s_fd;
Socklen_t s_len;
Char * buffer = (char *) malloc (BUFFER + 1 );
Char addr_t [BUFFER];
Int s_bits, r_bits =-1;
If (argc! = 2 ){
Printf ("usage: no valid argument! \ N "); exit (-1 );
}
If (! Buffer) {perror ("malloc"); exit (-1 );}
Bzero (buffer, BUFFER + 1 );
C_fd = socket (AF_INET, SOCK_STREAM, 0 );
If (! C_fd) {perror ("socket"); exit (-1 );}
Server_addr.sin_port = htons (S_PORT );
/* Inet_ton (AF_INET, argv [1], addr_t );*/
Server_addr.sin_addr.s_addr = inet_addr (argv [1]);
Server_addr.sin_family = AF_INET;
S_len = sizeof (struct sockaddr_in );
If (s_fd = connect (c_fd, (struct sockaddr *) & server_addr, s_len) =-1 ){
Perror ("connect"); exit (-1 );
}
Gets (buffer );
S_bits = send (c_fd, buffer, sizeof (buffer), 0 );
If (s_bits = sizeof (buffer )){
Printf ("success send to server: % s", buffer );
}
Else {
Printf ("fail send to server! \ N ");
}
R_bits = recv (c_fd, buffer, sizeof (buffer), 0 );
If (r_bits =-1 ){
Printf ("receive from server error! \ N "); exit (-1 );
}
Else printf ("receive from the server: % s \ n", buffer );
If (close (c_fd) =-1 ){
Printf ("close the socket error! \ N "); exit (-1 );
}
Return 1;
}
/* This is the server code */


# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Deprecision S_PORT 3333
# Define BUFFER 100.
Void up_chars (char * buf ){
Int I = 0;
While (buf = Toupper (buf))! = '\ 0') I ++;
}

Int
Main (void)
{
Struct sockaddr_in server_addr;
Struct sockaddr_in client_addr;
Int s_fd;
Int c_fd;
Socklen_t s_len;
Char * buffer;
Char addr_t [BUFFER];
Int temp;
Int flags;
If (buffer = (char *) malloc (BUFFER + 1) = NULL ){
Fprintf (stderr, "% s", strerror (errno); exit (-1 );
}
If (s_fd = socket (AF_INET, SOCK_STREAM, 0) =-1 ){
Perror ("socket"); exit (-1 );
}
Flags = fcntl (s_fd, F_GETFL );
Fcntl (s_fd, F_SETFL, flags | O_NONBLOCK );
S_len = sizeof (struct sockaddr_in );
Memset (& server_addr, '\ 0', s_len );
Server_addr.sin_port = htons (S_PORT );
Server_addr.sin_addr.s_addr = INADDR_ANY;
Server_addr.sin_family = AF_INET;
If (bind (s_fd, (struct sockaddr *) & server_addr, s_len) =-1 ){
Printf ("% s", strerror (errno); exit (-1 );
}
If (listen (s_fd, 10) =-1 ){
Printf ("% s", strerror (errno); exit (-1 );
}
While (1 ){
Int bits;
/* Sleep (1 );*/
C_fd = accept (s_fd, (struct sockaddr *) & client_addr, (socklen_t *) & s_len );
If (c_fd =-1 ){
Continue;
}
Else {
Inet_ntop (AF_INET, & client_addr.sin_addr, addr_t, sizeof (addr_t ));
Printf ("client ip is % s, port is % d \ n", addr_t, ntohs (client_addr.sin_port ));
}
Bits = recv (c_fd, buffer, sizeof (buffer), 0 );
If (bits =-1 & errno! = EAGAIN ){
Printf ("fail to receive from client \ n"); continue;
}
Else if (errno = EAGAIN ){
Printf ("socket is not ready now \ n"); continue;
}
Printf ("content is: % s \ n", buffer );
Up_chars (buffer );
Bits = send (c_fd, buffer, sizeof (buffer), 0 );
If (bits =-1 & errno! = EAGAIN ){
Printf ("fail to write \ n"); exit (-1 );
}
Else if (errno = EAGAIN ){
Printf ("socket is not ready! \ N "); continue;
}
}
If (close (s_fd) =-1 ){
Printf ("fail to close \ n"); exit (-1 );
}
Return 1;
}
Related Article

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.