掌握多进程服务器的编写模版;
掌握多进程程序的编写方法
客户端:连接服务器,接收用户输入客户名字,并传递给服务器,接着接收用户输入的字符串,传给由服务器,返回服务器处理后的字符串显示,用户输入Ctrl+D时终止连接并退出;
服务器:接收客户连接、成功返回欢迎信息,接收客户名字并显示,接收客户的字符串反转传回客户,要求服务器具有同时处理多个客户的能力;
client.c
#include //基本输入输出
#include //sockaddr_in结构体所在头文件
#include //socket函数所在头文件
#include //hostent结构体所在头文件
#include //memset等函数所在头文件
#include //close等函数所在头文件
#include //exit等函数所在头文件
#define PORT 3333
int main(int argc,char *args[]){
int sockfd,num; //定义套接字描述符
struct sockaddr_in server;
struct hostent *he;
socklen_t server_len;
char data[100];
if(argc != 2){
printf("Usage:%s \n" ,args[0]);
exit(0);
}
//创建套接字
if((sockfd = socket(AF_INET,SOCK_STREAM,0)) == -1){
perror("Socket error!\n");
exit(0);
}
//获取服务器信息
if((he = gethostbyname(args[1])) == NULL){
perror("Gethostbyname error!\n");
exit(0);
}
memset(&server,'\0',sizeof(server));
//配置服务器地址结构
server.sin_family = AF_INET; //配置协议族为IPv4
server.sin_port = htons(PORT); //配置服务器端口
server.sin_addr = *((struct in_addr *)he->h_addr); //服务器地址
server_len = sizeof(server);
if((connect(sockfd,(struct sockaddr *)&server,sizeof(server))) ==-1){
perror("Connect error!\n");
exit(0);
}
printf("input your Username:");
memset(&data,'\0',sizeof(data));
scanf("%s",&data);
if(send(sockfd,data,strlen(data),0) ==-1){
printf("Send error!\n");
exit(1);
}
while(1){
memset(&data,'\0',sizeof(data));
printf("input the Strings:\n");
scanf("%s",&data);
send(sockfd,data,sizeof(data),0);
if(data==EOF){
break;}
else{
memset(&data,'\0',sizeof(data));
if((recv(sockfd,data,100,0) ==-1)){
//recv函数原型recv(int sockfd,char *datafer,int dataferlen,int flags)
printf("Recv error!\n");
exit(1);}
printf("The code message:%s\n",data); }
}
close(sockfd);
}
server.c
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 1234
#define BACKLOG 2
#define MAXDATASIZE 1000
void process_cli(int connectfd,struct sockaddr_in client);
void revstring(char * str, int num){
int start = 0;
int end = num - 1;
char temp;
while(start != end && start <= end){
temp = str[start];
str[start] = str[end];
str[end] = temp;
start++;
end--;
}
}
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];
char * str_rev;
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);
str_rev=(char*)malloc(sizeof(char) * strlen(recvbuf) + 1);
strcpy(str_rev,recvbuf);
revstring(str_rev,num);
printf("Revstring message:",str_rev);
send(connectfd,str_rev,19,0);
free(str_rev);
}
close(connectfd);
}