Experiment 0x2:多进程并发服务器TCP编程

Experiment 0x2:多进程并发服务器TCP编程

文章目录

  • Experiment 0x2:多进程并发服务器TCP编程
    • 0x0 说明
    • 0x1 要求
    • 0x2 实现
    • 0x3 源码
      • 1- TCP服务端源码
      • 2- TCP客户端源码

0x0 说明

实验二:多进程并发服务器TCP编程

记录实验课代码

代码环境:

win10

VS2019 远程连接 ubuntu20

进行linux编程

0x1 要求

要求:实现一个基于TCP协议的多进程并发通信服务器与客户端,要求完成以下功能

1、基于多进程并发服务器编程模板,设计一个基于TCP协议的网络通信程序

2、实现并发服务器的功能,子进程完成客户端和服务器之间的网络通信

  • 多进程编程
    • fork函数的使用;
    • wait,waitpid函数的使用;
  • 基于多进程实现的网络通信程序,含服务器和客户端:
    • 由主进程循环接收客户的连接请求,并显示客户的IP地址和端口号;
    • 由子进程独立负责不同客户端之间的数据通信与信息回显。

0x2 实现

实现一个基于TCP协议的多进程并发通信服务器与客户端,

[外链图片转存失败,源站可能有防盗img](https://img-blog!链机制,建议将来csdnimcn/接上传

0x3 源码

1- TCP服务端源码

#include
#include
#include
#include
#include
#include
#include

#define PORT 2345
#define BACKLOG 5
#define MAXDATASIZE 1000

void process_client(int connfd, sockaddr_in client);

int main()
{
    int listenfd;
    sockaddr_in server;
    
    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if (listenfd == -1) {
        perror("Creating socket failed.\n");
        _exit(1);
    }
    int opt = SO_REUSEADDR;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
    bzero(&server, sizeof(sockaddr_in));
    server.sin_family = AF_INET;
    server.sin_port = htons(PORT);
    server.sin_addr.s_addr = htonl(INADDR_ANY);

    int len;
    len = sizeof(sockaddr_in);
    int flag = bind(listenfd, (sockaddr*)&server, len);
    if (flag == -1) {
        perror("bind() error!\n");
        _exit(1);
    }

    flag = listen(listenfd, BACKLOG);
    if (flag == -1) {
        perror("listen() error!\n");
        _exit(1);
    }

    len = sizeof(sockaddr_in);
    pid_t pid;
    while (1) {
        sockaddr_in client;
        int connfd = accept(listenfd, (sockaddr*)&client,(socklen_t*)&len);
        if (connfd == -1) {
            perror("accept() error!\n");
            _exit(1);
        }
        pid = fork();
        if (pid > 0) {//parent process
            close(connfd);
            continue;
        }
        else if (pid == 0) {//child process
            close(listenfd);
            process_client(connfd, client);
            _exit(0);
        }
        else {
            perror("fork() error!\n");
            _exit(1);
        }

    }
    close(listenfd);    

    return 0;
}

void process_client(int connfd, sockaddr_in client) {

    int num;
    char recvbuf[MAXDATASIZE] = { 0 };
    char sendbuf[MAXDATASIZE] = { 0 };
    char client_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, client_name, MAXDATASIZE,0);
    if (num == 0) {
        close(connfd);
        printf("Client disconnected.\n");
        return;
    }
    client_name[num - 1] = '\0';
    printf("Client's name is %s.\n", client_name);
    while (num = recv(connfd, recvbuf, MAXDATASIZE,0) , num ) {
        recvbuf[num] = '\0';
        printf("Received client(%s) message: %s\n", client_name, recvbuf);
        /*
        int i = 0;
        for (i = 0; i < num; 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] <= 'Z' + 3)) {
                    recvbuf[i] = recvbuf[i] - 26;
                }
            }
            sendbuf[i] = recvbuf[i];
        }
        sendbuf[i] = '\0';
        */
        memcpy(sendbuf, recvbuf, strlen(recvbuf));
        send(connfd, sendbuf, strlen(recvbuf), 0);

        memset(sendbuf, 0, MAXDATASIZE);
        memset(recvbuf, 0, MAXDATASIZE);
        num = 0;
    }
    close(connfd);
}

2- TCP客户端源码

#include
#include
#include
#include
#include
#include
#include

#define PORT 2345
#define MAXDATASIZE 100

void process(FILE* fp, int sockfd);
char* getMessage(char* sendline, int len, FILE* fp);
int main(int argc, char* argv[]) {
	int sockfd;
	hostent* he;
	sockaddr_in server;
	
	if (argc != 2) {
		printf("Usage:%s\n", argv[0]);
		_exit(1);
	}
	he = gethostbyname(argv[1]);
	if (he == NULL) {
		perror("gethostbyname() error\n");
		_exit(1);
	}
	sockfd = socket(AF_INET, SOCK_STREAM, 0);
	if (sockfd == -1) {
		perror("socket() error!\n");
		_exit(1);
	}

	bzero(&server, sizeof(sockaddr_in));
	server.sin_family = AF_INET;
	server.sin_port = htons(PORT);
	server.sin_addr = *(in_addr*)he->h_addr_list[0];

	int flag = connect(sockfd, (sockaddr*)&server, sizeof(sockaddr_in));
	if (flag == -1) {
		perror("connect() error!\n");
		_exit(1);
	}
	process(stdin, sockfd);
	close(sockfd);
	
}

void process(FILE* fp, int sockfd) {
	char sendline[MAXDATASIZE] = { 0 };
	char recvline[MAXDATASIZE] = { 0 };

	printf("Connected to server.\n");
	printf("Input client's name: ");

	char* flag = fgets(sendline, MAXDATASIZE, fp);
	if (flag == NULL) {
		perror("\nfgets() error! Exit()\n");
		_exit(1);
	}

	send(sockfd, sendline, strlen(sendline), 0);
	while (flag = getMessage(sendline, MAXDATASIZE, fp), flag) {
		send(sockfd, sendline, strlen(sendline), 0);
		int num;
		num = recv(sockfd, recvline, MAXDATASIZE, 0);
		if (num == 0) {
			printf("Server terminated.\n");
			return;
		}
		recvline[num] = '\0';
		printf("Server Message: %s\n", recvline);

		memset(sendline, 0, MAXDATASIZE);
		memset(recvline, 0, MAXDATASIZE);
	}
	printf("\nExit.\n");
}

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

你可能感兴趣的:(linux网络编程C语言,C语言,linux,c语言,网络)