系统相关
首页 > 系统相关> > 【网络编程实验三】并发多进程服务器编程

【网络编程实验三】并发多进程服务器编程

作者:互联网

1. 实验目的

2. 实验要求

3. 实验内容

采用多进程并发服务器技术,服务器可以同时接受多个客户的请求。具体要求如下:

客户端:

服务器端:

4. 实验代码和结果

1. 实验代码

服务端代码:

#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 1234
#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("socket() error.\n");
		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() error.\n");
		exit(1);
	}
	if(listen(listenfd,BACKLOG)==-1)
	{
		perror("listen() error.\n");
		exit(1);
	}
	len = sizeof(client);
	while(1)
	{
		if((connfd = accept(listenfd, (struct sockaddr *)&client,&len)) == -1)
		{
			perror("accept() error.\n");
			exit(1);
		}
		if((pid = fork()) > 0)
		{
			close(connfd);
			continue;
		}
		else if(pid == 0)
		{
			close(listenfd);
			process_cli(connfd,client);
			exit(1);
		}
		else
		{
			printf("fork() error.\n");
			exit(1);
		}
	}
	close(listenfd);
	return 0;
}

void process_cli(int connfd, struct sockaddr_in client)
{
	int num, i = 0;
	char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];
	printf("[!] You got a connection from IP:%s, PORT:%d\n", inet_ntoa(client.sin_addr), ntohs(client.sin_port));
	num = recv(connfd, cli_name, MAXDATASIZE,0);
	if(num == 0)
	{
		close(connfd);
		printf("Client disconnected.\n");
		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);
		for(i = 0;i < num-1; i++)
		{
			if((recvbuf[i] >= 'a' && recvbuf[i] <= 'z') || (recvbuf[i] >= 'A' && recvbuf[i] <= 'Z'))
			{
				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);
}

客户端代码:

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

#define PORT 1234
#define MAXDATASIZE 1000

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("socket() 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(connect(sockfd, (struct sockaddr *)&server, sizeof(server)) == -1)
	{
		printf("connect() error.\n");
		exit(1);
	}
	process(stdin, sockfd);
	close(sockfd);
	return 0;
}

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("\n[!] Exit.\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("[!] Server terminated.\n");
			return;
		}
		recvline[num] = '\0';
		printf("[>] Server Message:%s\n", recvline);
		if(!strcmp(recvline, "txlw"))
        	{
            		close(sockfd);
            		break;
        	}
	}
	printf("\n[!] Exit.\n");
}

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

2. 实验结果

服务端运行结果:

ser.c_1

ser.c_2

客户端运行结果:

进程1:

cli.c_1

进程2:

cli.c_2

5. 实验心得

6. 附件

下面给出客户端和服务端的c文件
链接:https://pan.baidu.com/s/128gckoPZf4fO2xPkjOHiVQ 密码:zmcd

标签:recvbuf,int,编程,server,并发,printf,服务器,include,MAXDATASIZE
来源: https://blog.csdn.net/ssjjtt1997/article/details/82750267