Linux:多进程和多线程回环socket服务器和客户端

多进程socket服务器代码:

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

void catch_child(int signo) {
pid_t wpid;
int status;
while((wpid = waitpid(-1, &status,WNOHANG)) > 0) {
// if(WIFEXITED(status))
printf(“catch child id %d, catch signal=%d\n”, wpid, signo);
}

}

int main()
{
pid_t pid;
/* sigset_t set;
sigemptyset(&set);
sigaddset(&set, SIGCHLD);
sigprocmask(SIG_BLOCK, &set, NULL);

struct sigaction act;
act.sa_handler = catch_child;
sigemptyset(&act.sa_mask);
act.sa_flags = 0;
sigaction(SIGCHLD, &act, NULL);
sigprocmask(SIG_UNBLOCK, &set, getpid());

*/
int lfd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr_ser;
addr_ser.sin_family = AF_INET;
addr_ser.sin_port = htons(8888);
addr_ser.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(lfd,(struct sockaddr *)&addr_ser,sizeof(addr_ser));
listen(lfd,256);
struct sockaddr_in addr_client;
socklen_t addrlen = sizeof(addr_client);
while(1) {
int cfd = accept(lfd,(struct sockaddr )&addr_client,&addrlen);
/
if (cfd = -1) {
perror(“accept error”);
exit(1);
} else */
if(cfd > 0) {
pid = fork();
}

	if(pid > 0) { //parent
		struct sigaction act;
		act.sa_handler = catch_child;
		act.sa_flags = 0;
		sigemptyset(&act.sa_mask);
		sigaction(SIGCHLD,&act,NULL);
		close(cfd);
		char str_ip[INET_ADDRSTRLEN];
		char *cli_ip = inet_ntop(AF_INET,&(addr_client.sin_addr.s_addr),str_ip,sizeof(str_ip));
		int cli_pot = ntohs(addr_client.sin_port);
		printf("get client ip:%s port:%d\n",cli_ip,cli_pot);
		char *ptr = inet_ntop(AF_INET,&(addr_ser.sin_addr.s_addr),str_ip,sizeof(str_ip));
		printf("server ip:%s\n",str_ip);
	} else if(pid == 0) {
		close(lfd);
		char buf[BUFSIZ];
		while(1) {
			memset(buf,0,sizeof(buf));
			int ret = read(cfd, buf, sizeof(buf));
			if(ret == 0) return 0;
			printf("receive from client:%s\n",buf);
			int i = 0;
			for(i=0; i

}

多线程回环socket服务器代码:

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

void * client_handle(void * arg) {
char buf[BUFSIZ];
int cfd = *(int *)arg;
while(1) {
memset(buf,0,sizeof(buf));
int ret = read(cfd, buf, sizeof(buf));
if(ret == 0)
{
close(cfd);
return 0;
}
printf(“cfd = %d, receive from client:%s\n”,cfd,buf);
int i = 0;
for(i=0; i buf[i]=toupper(buf[i]);
}
sleep(1);
write(cfd,buf,ret);
printf(“send to client: %s\n”,buf);

		}	

}

int main()
{
pthread_t thread;
int lfd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr_ser;
addr_ser.sin_family = AF_INET;
addr_ser.sin_port = htons(8888);
addr_ser.sin_addr.s_addr = htonl(INADDR_ANY);
int ret = bind(lfd,(struct sockaddr *)&addr_ser,sizeof(addr_ser));
listen(lfd,256);
struct sockaddr_in addr_client;
socklen_t addrlen = sizeof(addr_client);
while(1) {
int cfd = accept(lfd,(struct sockaddr *)&addr_client,&addrlen);
if(cfd > 0) {
printf(“cfd = %d \n”,cfd);
pthread_create(&thread,NULL,client_handle,(void *)&cfd);
pthread_detach(thread);
}

		char str_ip[INET_ADDRSTRLEN];
		char *cli_ip = inet_ntop(AF_INET,&(addr_client.sin_addr.s_addr),str_ip,sizeof(str_ip));
		int cli_pot = ntohs(addr_client.sin_port);
		printf("get client ip:%s port:%d\n",cli_ip,cli_pot);
		char *ptr = inet_ntop(AF_INET,&(addr_ser.sin_addr.s_addr),str_ip,sizeof(str_ip));
		printf("server ip:%s\n",str_ip);
	}
close(lfd);
return 0;

}

客户端代码:

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

int main()
{
char * data = “hello”;
int cfd = socket(AF_INET,SOCK_STREAM,0);
struct sockaddr_in addr_ser;
addr_ser.sin_family = AF_INET;
addr_ser.sin_port = htons(8888);

//addr_ser.sin_addr.s_addr = htonl(INADDR_ANY);

const char * src = "127.0.0.1";
inet_pton(AF_INET,src,(void *)&(addr_ser.sin_addr.s_addr));

connect(cfd,(struct sockaddr *)&addr_ser,sizeof(addr_ser));


socklen_t addrlen;
char str[INET_ADDRSTRLEN];
char *ptr = inet_ntop(AF_INET,&(addr_ser.sin_addr.s_addr),str,sizeof(str));

// printf(“connect to server ip:%s\n”,ptr);
char rebuf[256];
while(1) {
printf(“send: %s\n”,data);
write(cfd,data,sizeof(data));
//printf(“send: %s\n”,buf);
// sleep(1);
int ret = read(cfd,rebuf,sizeof(rebuf));
if(ret > 0) {
printf(“recev: %s\n”,rebuf);
}
sleep(1);
}
close(cfd);
return 0;
}

结果:

多进程服务器端:
michael@SD-20220518ZLPG:~/test_server$ ./server_fork.out
get client ip:127.0.0.1 port:63918
receive from client:hello
server ip:0.0.0.0
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
get client ip:127.0.0.1 port:63919
receive from client:hello
server ip:0.0.0.0
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
catch child id 2178, catch signal=17
get client ip:127.0.0.1 port:63919
server ip:0.0.0.0
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO
receive from client:hello
send to client: HELLO

查看客户端和进程:

Linux:多进程和多线程回环socket服务器和客户端_第1张图片
查看多线程服务器和客户端:
Linux:多进程和多线程回环socket服务器和客户端_第2张图片

你可能感兴趣的:(linux)