多进程并发服务器实例

时间:2022-01-11 17:56:00

    该程序为多进程并发服务器实例。包括服务器程序和客户端程序。编译及运行的相关信息如下:

    操作系统:CentOS 7

    编译工具:GCC

    调试工具:GDB

    程序实现的功能如下:

    1、服务器等候客户连接,一旦连接成功则显示客户的地址,接着接收该客户的名字并显示到屏幕。然后接收来自该客户的信息(字符串)。每当接收到一个字符串,则对其进行显示,并向客户端发送“sent successfully”("发送成功")的信息。之后,继续等待该客户端的信息直至该客户关闭连接。服务器具有同时处理多个客户的能力。

    2、客户端首先与服务器连接。连接成功后,显示成功连接的信息。接着接收用户输入的客户名字,并将名字发送给服务器程序。然后接收用户输入的字符串,再将字符串发送给服务器,并接收服务器程序发回的发送成功信息。之后,继续等待用户输入直至用户输入Ctrl+C。当收到用户输入Ctrl+C之后,客户关闭连接并退出。

 

    服务器程序如下:

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

#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000

void process_cli(int connectfd,struct sockaddr_in client);

int main()
{
int listenfd,connectfd;
pid_t pid;
struct sockaddr_in server;
struct sockaddr_in client;
int sin_size;

if((listenfd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket() error");
exit(1);
}

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(struct sockaddr))==-1)
{
perror("bind() error");
exit(1);
}

if(listen(listenfd,BACKLOG)==-1)
{
perror("listen() error");
exit(1);
}

sin_size=sizeof(struct sockaddr_in);

while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((connectfd=accept(listenfd,(struct sockaddr *)&client,&sin_size))==-1)
{
perror("accept() error");
exit(1);
}

if((pid=fork())<0)
{
printf("fork error\n");
exit(1);

}
else if(pid==0)
{
close(listenfd);
process_cli(connectfd,client);
exit(0);
}
else
{
close(connectfd);
continue;

}
}
close(listenfd);
}

void process_cli(int connectfd, struct sockaddr_in client)
{
int num,i;
char recvbuf[MAXDATASIZE],sendbuf[MAXDATASIZE],cli_name[MAXDATASIZE];

printf("You got a connection from:%s.",inet_ntoa(client.sin_addr));
num=recv(connectfd,cli_name,MAXDATASIZE,0);
if(num==0)
{
close(connectfd);
printf("client disconnected.\n");
return;
}

cli_name[num-1]='\0';
printf("Client's name is %s.\n",cli_name);

while((num=recv(connectfd,recvbuf,MAXDATASIZE,0))>0)
{
recvbuf[num]='\0';
printf("Received client (%s) message:%s",cli_name,recvbuf);

send(connectfd,"sent successfully.\n",19,0);
}
close(connectfd);
}


客户端程序如下:

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

#define PORT 1234
#define MAXDATASIZE 100

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

int main(int argc,char *argv[])
{
int fd;
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((fd=socket(AF_INET,SOCK_STREAM,0))==-1)
{
perror("socket() error");
exit(1);
}

bzero(&server,sizeof(server));
server.sin_port=htons(PORT);
server.sin_family=AF_INET;
server.sin_addr=*((struct in_addr *)he->h_addr);

if(connect(fd,(struct sockaddr *)&server,sizeof(struct sockaddr))==-1)
{
perror("connect() error");
exit(1);
}

process(stdin,fd);

close(fd);
}

void process(FILE *fp,int sockfd)
{
int numbytes;
char recvline[MAXDATASIZE],sendline[MAXDATASIZE];
printf("Connected to server.\n");
printf("Input 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((numbytes=recv(sockfd,recvline,MAXDATASIZE,0))==0)
{
printf("Server terminated.\n");
return;
}
recvline[numbytes]='\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));
}

服务器程序运行结果如下:

[root@mylinux 20160920]# ./srv
You got a connection from:127.0.0.1.Client's name is client1.
Received client (client1) message:1234
Received client (client1) message:abc
Received client (client1) message:efg
You got a connection from:127.0.0.1.Client's name is client2.
Received client (client2) message:12345667
Received client (client2) message:qazwsx
Received client (client1) message:123abcd

客户端1运行结果如下:

[root@mylinux 20160920]# ./cli 127.0.0.1
Connected to server.
Input name :client1
Input string to server:1234
Server Message :sent successfully.

Input string to server:abc
Server Message :sent successfully.

Input string to server:efg
Server Message :sent successfully.

Input string to server:123abcd
Server Message :sent successfully.

Input string to server:
Exit.


客户端2运行结果如下:

[root@mylinux 20160920]# ./cli 127.0.0.1
Connected to server.
Input name :client2
Input string to server:12345667
Server Message :sent successfully.

Input string to server:qazwsx
Server Message :sent successfully.

Input string to server:
Exit.