20230315 作业

多线程并发服务器

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <signal.h>


#define ERR_MSG(msg) do{\
	fprintf(stderr,"line=__%d__",__LINE__);\
	perror(msg);\
}while(0)

//服务器地址+端口
#define SER_PORT 2222 //1024~49151
#define SER_IP "192.168.31.200" 

struct newclient{
	int fd;
	struct sockaddr_in cin;
};

void *thread_fun(void *arg){
	struct newclient nc = *(struct newclient *)arg;

	int newfd = nc.fd;
	struct sockaddr_in cin = nc.cin;

	char buf[128]="";
	ssize_t res = 0;
	while(1){
		//接收
		bzero(buf,sizeof(buf));

		//从一个已经连接的socket接收数据
		res = read(newfd,buf,sizeof(buf));
		if(res < 0){
			//=-1 运行失败
			ERR_MSG("recv");
			break;
		}else if(0 == res){

			printf("[%s:%d] 断开连接\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));
			break;
		}
		printf("rcv [%s:%d] %s\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);

		//发送---->谁发给我数据,我就发还给谁
		strcat(buf,"*_*");

		//将数据发送给指定的套接字文件描述符中
		if(send(newfd,buf,sizeof(buf),0) < 0){
			ERR_MSG("send");
			break;
		}
		printf("send success\n");
	}
	close(newfd);
	pthread_exit(NULL);
}


int main(int argc, const char *argv[])
{ 
	//创建一个新的套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd){
		ERR_MSG("socket");
		return -1;
	}
	printf("sfd=%d\n",sfd);

	//设置套接字选项
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("setsockopt success\n");

	//地址信息结构体
	struct sockaddr_in sin;
	//IPV4 地址簇
	sin.sin_family = AF_INET;
	//将端口号从主机字节序转成网络字节序
	sin.sin_port = htons(SER_PORT);
	//将IPV4地址的字符串转换为网络字节序的整数
	sin.sin_addr.s_addr = inet_addr(SER_IP);

	//将IP地址和端口号绑定到指定套接字中
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//将套接字设置为被动监听状态,只负责监听是否有客户端连接成功
	if(listen(sfd,10) < 0){
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");


	while(1){
		struct newclient nc;
		socklen_t addrlen = sizeof(nc.cin);
		nc.fd = accept(sfd,(struct sockaddr *)&nc.cin,&addrlen);
		if(nc.fd < 0){
			ERR_MSG("accept");
			return -1;
		}
		printf("newfd=%d\n",nc.fd);
		printf("[%s:%d]\n",inet_ntoa(nc.cin.sin_addr),ntohs(nc.cin.sin_port));

		pthread_t tid;
		if(pthread_create(&tid,NULL,thread_fun,&nc) != 0){
			close(nc.fd);
			ERR_MSG("pthread_create");
			return -1;
		}
		pthread_detach(tid);

	}

	close(sfd);

	return 0;
}

多进程并发服务器

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

#define ERR_MSG(msg) do{\
	fprintf(stderr,"line=__%d__",__LINE__);\
	perror(msg);\
}while(0)

//服务器地址+端口
#define SER_PORT 2222 //1024~49151
#define SER_IP "192.168.31.200" 

void handlezombiethread(int sig){
	while(waitpid(-1,NULL,WNOHANG) > 0);
}

int rcvmsg(int newfd,struct sockaddr_in cin){
	char buf[128]="";
	ssize_t res = 0;
	while(1){
		//接收
		bzero(buf,sizeof(buf));

		//从一个已经连接的socket接收数据
		res = read(newfd,buf,sizeof(buf));
		if(res < 0){
			//=-1 运行失败
			ERR_MSG("recv");
			return -1;
		}else if(0 == res){
			fprintf(stderr,"[%s:%d] 客户端断开连接\n",\
				inet_ntoa(cin.sin_addr),\
				ntohs(cin.sin_port));
			break;
		}
		printf("rcv [%s:%d] %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),buf);
		//发送---->谁发给我数据,我就发还给谁
		strcat(buf,"*_*");

		//将数据发送给指定的套接字文件描述符中
		if(send(newfd,buf,sizeof(buf),0) < 0){
			ERR_MSG("send");
			return -1;
		}
		printf("send success\n");
	}
	return 0;
}


int main(int argc, const char *argv[])
{ 
	__sighandler_t r = signal(SIGCHLD,handlezombiethread);
	if(SIG_ERR == r){
		ERR_MSG("signal");
		return -1;
	}
	//创建一个新的套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(-1 == sfd){
		ERR_MSG("socket");
		return -1;
	}
	printf("sfd=%d\n",sfd);
	
	//设置套接字选项
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0){
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("setsockopt success\n");
	
	//地址信息结构体
	struct sockaddr_in sin;
	//IPV4 地址簇
	sin.sin_family = AF_INET;
	//将端口号从主机字节序转成网络字节序
	sin.sin_port = htons(SER_PORT);
	//将IPV4地址的字符串转换为网络字节序的整数
	sin.sin_addr.s_addr = inet_addr(SER_IP);

	//将IP地址和端口号绑定到指定套接字中
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0){
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	
	//将套接字设置为被动监听状态,只负责监听是否有客户端连接成功
	if(listen(sfd,10) < 0){
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	while(1){
		struct sockaddr_in cin;
		socklen_t addrlen = sizeof(cin);
		int newfd = accept(sfd,(struct sockaddr *)&cin,&addrlen);
		if(newfd < 0){
			ERR_MSG("accept");
			return -1;
		}
		printf("newfd=%d\n",newfd);
		printf("[%s:%d]\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port));

		pid_t pid = fork();

		if(pid == 0){
			close(sfd);
			rcvmsg(newfd,cin);
			close(newfd);
			exit(0);
		}else if(pid > 0){
			close(newfd);
		}else{
			close(newfd);
			ERR_MSG("fork");
			return -1;
		}
	}
	close(sfd);

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值