Linux socket程式設計學習初步(5)--伺服器多執行緒

pengfoo發表於2012-03-16
伺服器端: 
#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 <pthread.h>

#define PORT 12345
#define BACKLOG 5
#define MAXDATASIZE 1000

void process_cli(int connfd,struct sockaddr_in client);
void savedata(char* recvbuf,int len,char* cli_data);
void* function(void* arg);
struct ARG
{
 	int connfd;
	struct sockaddr_in client;
};


int main()
{
    int listenfd,connfd;
    pthread_t tid;
    struct ARG *arg;
    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("Bin() 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);
        }
        arg=(struct ARG *)malloc(sizeof(struct ARG));
        arg->connfd=connfd;
        memcpy((void*)&arg->client,&client,sizeof(client));
        if(pthread_create(&tid,NULL,function,(void*)arg))
        {
            perror("Pthread_creat() error.");
            exit(1);
        }
    }
    close(listenfd);
    return 0;
}


void process_cli(int connfd,struct sockaddr_in client)
{
    int num;
    char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE], cli_data[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);
        savedata(recvbuf,num,cli_data);
        int i;
        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]-26;
            }
        sendbuf[i]=recvbuf[i];
        }
        sendbuf[num-1]='\0';
        send(connfd,sendbuf,strlen(sendbuf),0);
    }
    close(connfd);
    printf("Client (%s) closed connection.\n%s's data :\n%s\n",cli_name,cli_name,cli_data);
}

void* function(void* arg)
{
    struct ARG *info;
    info=(struct ARG *)arg;
    process_cli(info->connfd,info->client);
    free(arg);
    pthread_exit(NULL);
}

void savedata(char* recvbuf,int len,char* cli_data)
{
	static int index=0;//用於表明當前資料的儲存位置
	int i=0;
	while(i<len-1)
	{
        cli_data[index++]=recvbuf[i++];
        cli_data[index]='\n';
	}
	index=index+1;
	cli_data[index]='\0';
}


客戶端:

#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));
}

 

這裡讀者因該瞭解執行緒建立的一些函式及其使用,其中伺服器端源代中的void savedata(char* recvbuf,int len,char* cli_data)函式用於儲存客戶端傳送的資訊。當客戶端結束後能把客戶端傳送過的資訊顯示出來。(當然它可以用於在伺服器上儲存使用者通訊記錄,但需要完善改進。)


 

相關文章