网络编程2023-3-15作业

线程实现

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

#define IP "192.168.31.54"
#define PORT 5555

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

struct cli_msg
{
	struct sockaddr_in cin;
	int newfd;
};
void* deal_cli_msg(void* arg);
int main(int argc, const char *argv[])
{
	//创建套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}

	//允许端口快速被重复使用
	int resue = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	
	//填充服务器地址信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);

	//绑定
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}

	//监听
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}

	//连接套接字
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int newfd = -1;
	pthread_t tid;
	struct cli_msg info;

	while(1)
	{
		//主线程负责连接客户端
		newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("客户端连接成功\n");

		info.cin = cin;
		info.newfd = newfd;
		
		//子线程负责传输数据
		if(pthread_create(&tid,NULL,deal_cli_msg,&info)!=0)
		{
			fprintf(stderr,"failed __%d__\n",__LINE__);
			return -1;
		}

		//分离线程 自己回收 避免出现僵尸线程
		pthread_detach(tid);
	}

	close(sfd);
	return 0;
}

void* deal_cli_msg(void* arg)
{
	//需要重新另存,不能间接访问
	int newfd = ((struct cli_msg*)arg)->newfd;
	struct sockaddr_in cin = ((struct cli_msg*)arg)->cin;

	char buf[128]="";
	ssize_t rev = -1;
	while(1)
	{
		bzero(buf,sizeof(buf));
		rev = recv(newfd,buf,sizeof(buf),0);
		if(rev<0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0==rev)
		{
			fprintf(stderr,"[%s : %d] newfd=%d 客户端下线\n",\
					inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
			break;
		}
		printf("[%s : %d] newfd=%d : %s\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);

		strcat(buf,"*_*");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			break;
		}
	}

	close(newfd);
	pthread_exit(NULL);
}


进程实现

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

#define IP "192.168.31.54"
#define PORT 5555

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

int deal_cld_msg(int newfd,struct sockaddr_in cin);

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

int main(int argc, const char *argv[])
{
	//捕获17号信号
	__sighandler_t s = signal(17,callBack);
	if(SIG_ERR==s)
	{
		ERR_MSG("signal");
		return -1;
	}
	
	//创建套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}

	//允许快速重复使用
	int resue = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&resue,sizeof(resue))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}

	//填充服务器信息结构体
	struct sockaddr_in sin;
	sin.sin_family = AF_INET;
	sin.sin_port = htons(PORT);
	sin.sin_addr.s_addr = inet_addr(IP);

	//连接
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}

	//监听
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}

	//连接套接字
	struct sockaddr_in cin;
	socklen_t addrlen = sizeof(cin);
	int newfd = -1;
	pid_t cpid=0;

	while(1)
	{
		//父进程负责连接
		newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
		if(newfd < 0)
		{
			ERR_MSG("accept");
			return -1;
		}
		printf("客户端连接成功\n");

		cpid = fork();
		if(cpid > 0)
		{
			close(newfd);
		}
		//子进程负责传输
		else if(0 == cpid)
		{
			close(sfd);
			deal_cld_msg(newfd,cin);
			close(newfd);
			exit(0);
		}
		else
		{
			ERR_MSG("fork");
			return -1;
		}
	}

	close(sfd);
	return 0;
}

int deal_cld_msg(int newfd,struct sockaddr_in cin)
{
	ssize_t rcv = -1;
	char buf[128]="";
	while(1)
	{
		bzero(buf,sizeof(buf));
		rcv = recv(newfd,buf,sizeof(buf),0);
		if(rcv<0)
		{
			ERR_MSG("recv");
			return -1;
		}
		else if(0 == rcv)
		{
			printf("客户端下线\n");
			break;
		}
		printf("[%s : %d] newfd=%d : %s\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);

		strcat(buf,"*_*");
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return -1;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值