linux编程实例--简单多进程服务器

主要利用fork事先创建若干个进程,并发处理多个客户端的连接,返回当前系统时间。具体代码如下:

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 3

static void handle_connect(int serv_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)
        {
        perror("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 3333

int 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;

}


linux编程实例--简单多进程服务器,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。