socket编程服务器和多客户端【多客户端多进程访问】

socket编程服务器和多客户端【多客户端多进程访问】

上一篇socket服务器与客户端写了单个客户端与服务器的交互,只是为了实现基本功能而已,这一篇加了些内容,多个客户端同时访问服务器,采用多进程进行处理。抛砖引玉,诸多问题点,还望看官指正,谢谢~

进程并发服务器:
该服务器弥补了上一个服务器的不足,可以同时处理多个客户端,只要有客户端来连接它,他就能响应。在我们这个服务器中,父进程主要负责监听,所以在父进程一开始就要把父进程的接收函数关闭掉,防止父进程在接收函数处阻塞,导致子进程不能创建成功。同理,子进程主要负责接收客户端,并做相关处理,所以子进程在一创建就要把监听函数关闭,不然会导致服务器功能的紊乱。这个服务器有一个特别要注意的是,子进程在退出时会产生僵尸进程,所以我们一定要对子进程退出后进行处理。

不多说,开整!

服务器:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <signal.h>

#define SOCKET_PORT 8888

/*global*/	
char buffer[1024] = {
     0};
char new_buffer[1024] = {
     0};
	
int create_socket_and_listen(void)
{
     
	int iRet = -1;
	int socket_fd = -1;
	struct sockaddr_in addr;
	
	/*1.create socket fd based on TCP*/
	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(socket_fd == -1)
	{
     
		printf("socket create faild!\n");
		exit(-1);	
	}
	
	/*2.init the port and IP address*/
	memset(&addr, 0, sizeof(addr));
	addr.sin_family = AF_INET;  //Internet address family
	addr.sin_port = htons(SOCKET_PORT);
	addr.sin_addr.s_addr = htonl(INADDR_ANY);

	/*3.bind socket with addr*/
	iRet = bind(socket_fd, (struct sockaddr *)&addr, sizeof(addr));
	if(iRet == -1)
	{
     
		printf("bind faild!\n");
		return 0;
	}

	/*4.listen the new client from socketfd*/
	iRet = listen(socket_fd, 5);
	if(iRet == -1)
	{
     
		printf("listen faild!\n");
		return 0;
	}
	return socket_fd;
}

int wait_client_accept(int socket_fd)
{
     
	int new_socket_fd = -1;
	struct sockaddr_in new_addr;
	int new_addr_len = sizeof(new_addr);
	
	/*5.create a new socket fd(different from the socket_fd before) and use it to  accept the client*/
	printf("Waiting for client connect:\n");
	new_socket_fd = accept(socket_fd, (struct sockaddr *)&new_addr, &new_addr_len);
	if(new_socket_fd < 0)
	{
     
		printf("accept faild!\n");
		return 0;
	}
	printf("accept new_socket_fd : [%d]\n", new_socket_fd);
	return new_socket_fd;
}

int client_handler(int new_socket_fd, int pid)
{
     
	int iRet = -1;
	/*6.communciate with client*/
	while(1)
	{
     
		iRet = read(new_socket_fd, buffer, sizeof(buffer)); /*if nothing to read, it will block*/
		printf("[PID:%d]receive data:%s\n", pid, buffer);
		if(strcmp(buffer, "quit") == 0) /*best to use strncmp*/
		{
     
			return 0;
		}
		strcpy(new_buffer, "The server has received your data:"); /*best to use strncpy*/
		strncat(new_buffer, buffer, sizeof(buffer));
		iRet = write(new_socket_fd, new_buffer, sizeof(new_buffer));
		if(iRet <= 0)
		{
     
			printf("write faild!\n");
			return 0;
		}
		memset(buffer, 0, sizeof(buffer));
		memset(new_buffer, 0, sizeof(new_buffer));
	}
	return 0;
}

/*When child process exit, father process will receive the signal, then go to the handler to release the source*/
int pid_handler(void)
{
     
	pid_t pw;
#if 0
	if((pw = wait(NULL)) != 0)   
#else
	if((pw = waitpid(-1, NULL, WNOHANG)) > 0)  /*wait and waitpid are all OK*/
#endif
	{
     
		printf(" A client (pid : %d) quit!\n", pw);	
	}
	
}

int main(void)
{
     
	int socketfd = -1;
	int new_socketfd = -1;
	pid_t pid;
	
	socketfd = create_socket_and_listen();

	signal(SIGCHLD, (__sighandler_t)pid_handler); /*In order to deal with Zombie process, if no trans here, will be warning!*/
	
	while(1)
	{
     
		new_socketfd = wait_client_accept(socketfd);
		pid = fork();
		if(pid < 0)
		{
     
			printf("fork error!\n");
			break;
		}
		else if(pid == 0) //child
		{
     
			printf("[Notice!] A new client connect, pid:%d, ppid:%d\n", getpid(), getppid());
			close(socketfd); 
			client_handler(new_socketfd, getpid());
			break;
		}
		else //father
		{
     
			close(new_socketfd);
			continue;
		}
		
	}
	return 0;
}

备注:
服务器里面关于signal的知识可点击传送门查看,写得很详细,wait和waitpid网上也有很多内容,写得都超详细,我也可以理解了再写上来(其实就是复制粘贴,苦笑~),没有意义,我是觉得我如果没有新的理解或者自己实践出来的东西,我就不往上面写了,浪费时间。

客户端:

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>

#define SOCKET_PORT 8888

int main(void)
{
     	
	int iRet = -1;
	int socket_fd = -1;
	int addr_len = 0;
	struct sockaddr_in addr;
	char buffer[1024] = {
     0};

	/*1.create socket fd*/	
	socket_fd = socket(AF_INET, SOCK_STREAM, 0);
	if(socket_fd == -1)
	{
     
		printf("scoket create faild!\n");
		exit(-1);
	}

	/*2.init the port and IP address which it will visit*/
	addr.sin_family = AF_INET;
	addr.sin_port = htons(SOCKET_PORT);
#if 0
	addr.sin_addr.s_addr = inet_addr("127.0.0.1");                //The two ways are all OK,usually we use the second method
#else
	inet_aton("127.0.0.1", &(addr.sin_addr));
#endif
	
	/*3.connect it*/
	addr_len = sizeof(addr);
	iRet = connect(socket_fd, (struct sockaddr *)&addr, addr_len);
	if(iRet == -1)
	{
     	
		printf("connect faild!\n");
		return 0;
	}

	/*4.communicate with the server*/
	printf("Please input what you want to server:\n");
	while(1)
	{
     
		scanf("%s", (char *)buffer);
		iRet = write(socket_fd, buffer, sizeof(buffer));
		if(!strcmp(buffer, "quit"))
		{
     
			break;
		}
		memset(&buffer, 0, sizeof(buffer));
		iRet = read(socket_fd, buffer, sizeof(buffer)); /*when buffer is NULL, the read will block*/
		printf("%s\n", buffer);
		printf("Communicate OK!\n");
		memset(&buffer, 0, sizeof(buffer));
	}
	return 0;
}

代码亲测可用!!!!图片不想贴了,自己试一下吧!

题外话:
接触嵌入式不久,两年多,多多少少知道些,感觉会写点,又有很多盲点。网上的资源很是强大,源码注释应有尽有,确实方便,但有些时候,我想找的东西网上没那么“”精确“”,或许是作者没有准确get到我的那个点,作者:“嗯???怨我咯??”
所以我想以后我觉得有价值有意义的东西都会保存起来,以备不时之需~

你可能感兴趣的:(嵌入式C)