Linux socket程式設計學習初步(4)--伺服器端多程式

pengfoo發表於2012-03-15

伺服器端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define PORT 12345
#define BACKLOG 5 //請求佇列的最大長度
#define MAXDATASIZE 1000
void process_cli(int connfd,struct sockaddr_in client);


int main()
{
    int listenfd,connfd;
    pid_t pid;
    struct sockaddr_in server;
    struct sockaddr_in client;
    int len;
    if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        perror("Creating socket failed");
        exit(1);
    }
    int opt=SO_REUSEADDR;
    setsockopt(listenfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
    server.sin_addr.s_addr=htonl(INADDR_ANY);
    if(bind(listenfd,(struct sockaddr *)&server,sizeof(server))==-1)
    {
        perror("Bind() failed");
        exit(1);
    }
    if(listen(listenfd,BACKLOG)==-1)
    {
        perror("listen failed");
        exit(1);
    }
    len=sizeof(client);
    while(1)
    {
        if((connfd=accept(listenfd,(struct sockaddr *)&client,(socklen_t *)&len))==-1)
        {
            perror("accept() failed");
            exit(1);
        }
        if((pid=fork())>0)
        {
            close(connfd);
            continue;
        }
        else if(pid==0)
        {
            close(listenfd);
            process_cli(connfd,client);
            exit(0);
        }
        else
        {
            printf("fork() failed");
            exit(0);
        }
	}
    close(listenfd);
    return 0;
}

void process_cli(int connfd,struct sockaddr_in client)
{
    int num;
    char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];
    printf("You get a connection from %s.",inet_ntoa(client.sin_addr));
    num=recv(connfd,cli_name,MAXDATASIZE,0);
    if(num==0)
    {
        close(connfd);
        printf("Clinet disconnect.");
        return;
    }
    cli_name[num-1]='\0';
    printf("Client's name is %s.\n",cli_name);
    while(num=recv(connfd,recvbuf,MAXDATASIZE,0))
    {
        recvbuf[num]='\0';
        printf("Received client( %s ) message: %s",cli_name,recvbuf);
        int i=0;
        for(i=0;i<num-1;i++)
        {
            if((recvbuf[i]>='a'&&recvbuf[i]<='z')||(recvbuf[i]>='A'&&recvbuf[i]<='Z'))
            {
                recvbuf[i]=recvbuf[i]+3;
                if((recvbuf[i]>='Z'&&recvbuf[i]<='z'+3)||(recvbuf[i]>='z'))
                    recvbuf[i]=recvbuf[i];
            }
            sendbuf[i]=recvbuf[i];
        }
        sendbuf[num-1]='\0';
        send(connfd,sendbuf,strlen(sendbuf),0);//added by fupeng
    }
    close(connfd);
}

客戶端:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>

#define PORT 12345
#define BACKLOG 5
#define MAXDATASIZE 1000
#define DEST_IP "127.0.0.1"

void process(FILE *fp,int sockfd);
char* getMessage(char* sendline,int len,FILE* fp);

int main(/*int argc,char *argv[]*/)
{
    int sockfd;
    struct hostent *he;
    struct sockaddr_in server;
//    if (argc!=2)
//    {
//        printf("Usage:%s <ip address>\n",argv[0]);
//        exit(1);
//    }
//    if((he=gethostbyname(argv[1]))==NULL)
//    {
//        printf("gethostbyname () error\n");
//        exit(1);
//    }
    if((sockfd=socket(AF_INET,SOCK_STREAM,0))==-1)
    {
        printf("sock() error\n");
        exit(1);
    }
    bzero(&server,sizeof(server));
    server.sin_family=AF_INET;
    server.sin_port=htons(PORT);
//    server.sin_addr=*((struct in_addr *)he->h_addr);

    if(inet_aton(DEST_IP,&server.sin_addr)==0)
    {
        fprintf(stderr,"Inet_aton erro\n");
        exit(1);
    }  //by fupeng


    if(connect(sockfd,(struct sockaddr *)&server,sizeof(server))==-1)
    {
        printf("connect() failed\n");
        exit(1);
    }
    process(stdin,sockfd);
    close(sockfd);
}



void process(FILE *fp,int sockfd)
{
	char sendline[MAXDATASIZE],recvline[MAXDATASIZE];
	int num;
	printf("connected to server.\n");
	printf("Input client's name:");
	if(fgets(sendline,MAXDATASIZE,fp)==NULL)
    {
        printf("\nExit.\n");
        return;
    }
	send(sockfd,sendline,strlen(sendline),0);
	while(getMessage(sendline,MAXDATASIZE,fp)!=NULL)
	{
        send(sockfd,sendline,strlen(sendline),0);
        if((num=recv(sockfd,recvline,MAXDATASIZE,0))==0)
        {
            printf("\nServer terminated.\n");
            return;
        }
        recvline[num]='\0';
        printf("Server Message:%s\n",recvline);
	}
	printf("\nExit\n");
}

char *getMessage(char* sendline,int len,FILE* fp)
{
    printf("Input string to server:");
    return(fgets(sendline,MAXDATASIZE,fp));
}

關於多程式併發伺服器的例子。

相關文章