Linux Concurrent Server

Source: Internet
Author: User

Original reprinted, please note

 

We know that Linux UDP is connectionless, but TCP is connection-oriented, so the UDP server can process it concurrently. However, because TCP is connection-oriented, one communication can only be connected to one client, to implement concurrent processing, you can use fork () to create sub-processes. The programming mode is as follows:

 

Server:

# Include <stdlib. h>
# Include <stdio. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>

# Define my_port 3333

Int main (INT argc, char ** argv)
{
Int listen_fd, accept_fd;
Struct sockaddr_in client_addr;
Int N;
Int sin_size;
 
If (listen_fd = socket (af_inet, sock_stream, 0) <0)
{
Printf ("socket error: % s/n/a", strerror (errno ));
Exit (1 );
}
 
Bzero (& client_addr, sizeof (struct sockaddr_in ));
Client_addr.sin_family = af_inet;
Client_addr.sin_port = htons (my_port );
Client_addr.sin_addr.s_addr = htonl (inaddr_any );
N = 1;
/* If the server is terminated, the server can be started for the second time without waiting for a period of time */
Setsockopt (listen_fd, sol_socket, so_reuseaddr, & N, sizeof (INT ));
If (BIND (listen_fd, (struct sockaddr *) & client_addr, sizeof (client_addr) <0)
{
Printf ("BIND error: % s/n/a", strerror (errno ));
Exit (1 );
}
Listen (listen_fd, 5 );
Sin_size = sizeof (struct sockaddr_in );
While (1)
{
Accept_fd = accept (listen_fd, (struct sockaddr *) & client_addr, (struct socklen_t *) & sin_size );
If (accept_fd <0) & (errno = eintr ))
Continue;
Else if (accept_fd <0)
{
Printf ("Accept error: % s/n/a", strerror (errno ));
Continue;
}
If (n = fork () = 0)
{
/* The sub-process processes the client connection */
Char buffer [1024];
Printf ("Get connect form % s/n", inet_ntoa (client_addr.sin_addr ));
// Print the IP address of the connected client,
N = read (accept_fd, buffer, 1024 );
// Write (accept_fd, buffer, N );
Buffer [N] = '/0 ';
// Close (listen_fd );
Close (accept_fd );
Printf ("server revieve % s", buffer );
Exit (0 );
}
Else if (n <0)
Printf ("fork error: % s/n/a", strerror (errno ));
Close (accept_fd );
}
}
Client:

# Include <stdlib. h>
# Include <stdio. h>
# Include <errno. h>
# Include <string. h>
# Include <netdb. h>
# Include <sys/types. h>
# Include <netinet/in. h>
# Include <sys/socket. h>

# Define portnumber 3333

Int main (INT argc, char * argv [])
{
Int sockfd;
Char buffer [1024];
Struct sockaddr_in server_addr;
Struct hostent * Host;

/* Use hostname to query host names */
If (argc! = 2)
{
Fprintf (stderr, "Usage: % s Hostname/A/N", argv [0]);
Exit (1 );
}

If (host = gethostbyname (argv [1]) = NULL)
{
Fprintf (stderr, "gethostname error/N ");
Exit (1 );
}

/* Fill in the information of the client program on the server */
Bzero (& server_addr, sizeof (server_addr); // initialization, set 0
Server_addr.sin_family = af_inet; // IPv4
Server_addr.sin_port = htons (portnumber); // (convert the short data on the machine to the short data on the network) Port Number
Server_addr.sin_addr = * (struct in_addr *) Host-> h_addr); // ip address
/* The client program initiates a connection request */

While (1)
{
/* The customer program starts to establish the sockfd descriptor */
If (sockfd = socket (af_inet, sock_stream, 0) =-1) // af_inet: Internet; sock_stream: TCP
{
Fprintf (stderr, "socket error: % S/A/N", strerror (errno ));
Exit (1 );
}
If (connect (sockfd, (struct sockaddr *) (& server_addr), sizeof (struct sockaddr) =-1)
{
Fprintf (stderr, "Connect error: % S/A/N", strerror (errno ));
Exit (1 );
}
/* Connection successful */
Printf ("Please input CHAR:/N ");

/* Send data */
Fgets (buffer, 1024, stdin );
Write (sockfd, buffer, strlen (buffer ));
/* End communication */
 
Close (sockfd );
}
Exit (0 );
}
In this way, the server can listen to multiple clients at the same time, with the same effect as UDP,

This only listens to one port. If you want to listen to multiple ports at the same time, you can use select () For Io reuse.

 

 

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.