Linux Programming Example-Simple multi-process server

Source: Internet
Author: User

The main use of fork in advance to create a number of processes, concurrent processing of multiple client connections, return the current system time. The specific code is as follows:

Server.c

# include <sys/types.h># include <sys/socket.h># include <netinet/in.h># include <time.h># Include <string.h># include <stdio.h># include <signal.h># include <errno.h># include < unistd.h> #define Bufflen 1024#define server_port 3333#define BACKLOG 5#define pidnum 3static void handle_connect (int s    ERV_FD) {int client_fd;    struct sockaddr_in client_addr;        int len=sizeof (CLIENT_ADDR); while (1) {if (Client_fd=accept (serv_fd, (struct sockaddr*) &client_addr,&len) <0) {PE        Rror ("accept");        Exit (errno);           } printf ("Accept done,the process id=%d\n", getpid ());        time_t now;        Char buff[bufflen]={0};        int n=0;        memset (Buff,0,bufflen);        N=RECV (client_fd,buff,bufflen,0);                        if (n>0&&!strncmp (Buff, "time", 4)) {memset (Buff,0,bufflen);            Now=time (NULL); sprintf (Buff, "%24s\r\n", CTime (&noW));        Send (Client_fd,buff,strlen (buff), 0);    } close (CLIENT_FD); }}void sig_int (int num) {exit (1);}    int main (int argc, char* argv[]) {int servfd;    unsigned int lisnum=3;    struct sockaddr_in serv_addr;    Signal (Sigint,sig_int);    Servfd=socket (af_inet,sock_stream,0);    memset (&serv_addr,0,sizeof (SERV_ADDR));    Serv_addr.sin_family=af_inet;    Serv_addr.sin_addr.s_addr=htonl (Inaddr_any);    Serv_addr.sin_port=htons (Server_port);    int Err=bind (servfd, (struct sockaddr*) &serv_addr,sizeof (SERV_ADDR));        if (listen (servfd,lisnum) = =-1) {perror ("listen");        Exit (errno);    }else printf ("The server is listening!\n");    pid_t Pid[pidnum];    int i=0;        for (i=0;i<pidnum;i++) {pid[i]=fork ();        if (pid[i]==0) {handle_connect (SERVFD);    }} while (1);    Close (SERVFD); return 0;}

client.c

#include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <time.h># include<string.h> #include <stdio.h> #include <errno.h> #define Bufflen 1024#define Server_port    3333int Main (int argc,char*argv[]) {int clientfd;    struct sockaddr_in serv_addr;    time_t now;    Char buff[bufflen]={0};    int n=0,err;    Clientfd=socket (af_inet,sock_stream,0);    memset (&serv_addr,0,sizeof (SERV_ADDR));    Serv_addr.sin_family=af_inet;    Serv_addr.sin_addr.s_addr=htonl (Inaddr_any);    Serv_addr.sin_port=htons (Server_port);        if ((Err=connect (CLIENTFD, (struct sockaddr*) &serv_addr,sizeof (SERV_ADDR))) (<0) {perror ("connect");    Exit (errno); } printf ("Connected done!!    \ n ");    memset (Buff,0,bufflen);    strcpy (Buff, "time");    Send (Clientfd,buff,strlen (buff), 0);    memset (Buff,0,bufflen);    N=RECV (clientfd,buff,bufflen,0);    if (n>0) {printf ("time:%s\n", buff);    } close (CLIENTFD); return 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.