Linux TCP/IP network programming (multi-process)

Source: Internet
Author: User

 

Multi-process network programming example:

# Include <stdio. h> # include <errno. h> # include <netdb. h> # include <sys/socket. h> // may be use by getpid () and getppid () # include <unistd. h> int main (int argc, char * argv []) {int sockfd, new_fd, mypid; struct sockaddr_in server_addr; struct sockaddr_in client_addr; int sin_size, portnumber; char hello [] = "Hello! Are You Fine? "; Char buf [1024]; int flag; if (argc! = 2) {fprintf (stderr, "Usage: % s portnumber \ a \ n", argv [0]); exit (1 );} if (portnumber = atoi (argv [1]) <0) {fprintf (stderr, "Usage: % s portnumber \ a \ n", argv [0]); exit (1);}/* the server starts to establish the socket descriptor */if (sockfd = socket (AF_INET, SOCK_STREAM, 0) =-1) {fprintf (stderr, "Socket error: % s \ n \ a", strerror (errno); exit (1);}/* fill the sockaddr structure on the server */bzero (& server_addr, sizeof (struct sockaddr_in); server_addr.sin_family = AF _ INET; struct = htonl (INADDR_ANY); server_addr.sin_port = htons (portnumber);/* bind the sockfd descriptor */if (bind (sockfd, (struct sockaddr *) (& server_addr ), sizeof (struct sockaddr) =-1) {fprintf (stderr, "Bind error: % s \ n \ a", strerror (errno); exit (1 );} /* listen to the sockfd descriptor */if (Listen (sockfd, 5) =-1) {fprintf (stderr, "listen error: % s \ n \ ", strerror (errno); exit (1);} // printf my PID num printf ("Sta Rting Server at port: % d Success. my PID is: % d \ nAccepting Request... \ n ", portnumber, getpid (); while (1) {/* server blocking until the Client Program establishes a connection */sin_size = sizeof (struct sockaddr_in ); if (new_fd = accept (sockfd, (struct sockaddr *) (& client_addr), & sin_size) =-1) {fprintf (stderr, "Accept error: % s \ n \ a ", strerror (errno); exit (1) ;}// generate a sub-process to complete the session with the client. The parent process continues to listen to if (! Fork () {// print my PID and my parent PID mypid = getpid (); printf ("Accepting new connection... \ nNow I will server for you. my PID is: % d \ nMy parent PID is: % d \ n ", mypid, getppid (); // subprocess, close the parent process listening socket close (sockfd ); fprintf (stderr, "Server get connection from % s \ n", inet_ntoa (client_addr.sin_addr); if (write (new_fd, hello, strlen (hello) =-1) {fprintf (stderr, "Write Error: % s \ n", strerror (errno); exit (1) ;}while (1) // get data from client {if (flag = read (new_fd, buf, 1024) <0) {printf ("Reading data error"); break ;} if (flag = 0) {printf ("[PID: % d] Ending current connection \ n", mypid); break;} else {printf ("[PID: % d] --> % s \ n ", mypid, buf); if (strstr (buf," exit ") {printf (" I get 'exit 'string, so close client connection \ n "); break ;}}// end while (get data from client) // The connection is lost and the process ends (the customer proactively disconnects, automatic disconnection due to network problems) printf ("Connection lose. end child program, PID: % d \ n ", getpid (); close (new_fd); exit (0) ;}// if fork // The parent process closes the new socket, continue listening for request close (new_fd);} // while close (sockfd); exit (0 );}

:

 

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.