完成TCP的服务器,客户端,上交。服务器,客户端需要做到随时收发:多线程多进程实现。

服务器代码:

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

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

struct sockaddr_in cin; 	//存储获取到的客户端的地址信息
socklen_t addrlen = sizeof(cin); 	//真实地址信息结构体的大小
pthread_t pth_r;
pthread_t pth_w;

void *pthfunc_r(void *p)
{	
	char buf[128] = "";
	ssize_t res = 0;
	int newfd = *(int *)p;
	while(1)
	{
		//接收
		bzero(buf,sizeof(buf));
		res = recv(newfd,buf,sizeof(buf),0);
		if(res<0)
		{
			ERR_MSG("recv");
			return NULL;
		}
		else if(0 == res)
		{
			fprintf(stderr,"[%s:%d] newfd = %d 客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);
			//请求制定线程退出
			pthread_cancel(pth_w);
			break;
		}
		printf("[%s:%d] newfd = %d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd,buf);

	}
}

void *pthfunc_w(void *p)
{
	char buf[128] = "";
	ssize_t res = 0;
	int newfd = *(int *)p;
	while(1)
	{
		//发送
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(send(newfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return NULL;
		}
		printf("send success\n");
	}
}
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}

	//允许端口快速重用--->允许程序退出后,端口号能被新的进程快速覆盖
	//必须放在bind之前
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用成功\n");

	//填充地址信息结构体
	//根据地址族的不同,地址信息结构体不一致,AF_INET:man 7 ip
	struct sockaddr_in sin;
	sin.sin_family 		= AF_INET; 	//必须填AF_INET;
	sin.sin_port 		= htons(6666); 	//端口号的网络字节序1024~49151
	sin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序
	//绑定服务器的IP和端口
	if(bind(sfd,(struct sockaddr*)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");
	//将套接字设置为被动监听状态
	if(listen(sfd,128)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	//从已经完成连接的队列头中获取一个客户端的信息,生成一个新的文件描述符
	//生成的新的文件描述符才是与客户端通信的文件描述符
	int newfd = 0;
	newfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(newfd<0)
	{
		ERR_MSG("accept");
		return -1;
	}
	printf("[%s:%d] newfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),newfd);

	//创建接收线程
	if(pthread_create(&pth_r,NULL,pthfunc_r,&newfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	//创建发送线程
	if(pthread_create(&pth_w,NULL,pthfunc_w,&newfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	
	//阻塞线程
	pthread_join(pth_r,NULL);
	pthread_join(pth_w,NULL);

	//关闭文件描述符
	close(newfd);
	close(sfd);
	return 0;
}

客户端代码

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

#define ERR_MSG(msg) do{\
	fprintf(stderr,"line:%d\n",__LINE__);\
	perror(msg);\
}while(0)
#define IP "192.168.8.90"
struct sockaddr_in cin;

pthread_t pth_r;
pthread_t pth_w;

void *pthfunc_r(void *p)
{	
	char buf[128] = "";
	ssize_t res = 0;
	int sfd = *(int *)p;
	while(1)
	{
		//接收
		bzero(buf,sizeof(buf));
		res = recv(sfd,buf,sizeof(buf),0);
		if(res<0)
		{
			ERR_MSG("recv");
			return NULL;
		}
		else if(0 == res)
		{
			fprintf(stderr,"[%s:%d] sfd = %d 服务器下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sfd);
			//请求制定线程退出
			pthread_cancel(pth_w);
			break;
		}
		printf("[%s:%d] sfd = %d : %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),sfd,buf);

	}
}

void *pthfunc_w(void *p)
{
	char buf[128] = "";
	ssize_t res = 0;
	int sfd = *(int *)p;
	while(1)
	{
		//发送
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);

		buf[strlen(buf)-1] = 0;
		if(0 == strcmp(buf,"quit"))
		{	
			//请求制定线程退出
			pthread_cancel(pth_r);
			break;
		}
		if(send(sfd,buf,sizeof(buf),0)<0)
		{
			ERR_MSG("send");
			return NULL;
		}
		printf("send success\n");
	}
}
int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}


	//填充地址信息结构体
	//根据地址族的不同,地址信息结构体不一致,AF_INET:man 7 ip
	cin.sin_family 		= AF_INET; 	//必须填AF_INET;
	cin.sin_port 		= htons(6666); 	//端口号的网络字节序1024~49151
	cin.sin_addr.s_addr = inet_addr(IP); //本机IP地址的网络字节序
	
	if(connect(sfd,(struct sockaddr *)&cin,sizeof(cin))<0)
	{
		ERR_MSG("connect");
		return -1;
	}

	//创建接收线程
	if(pthread_create(&pth_r,NULL,pthfunc_r,&sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	//创建发送线程
	if(pthread_create(&pth_w,NULL,pthfunc_w,&sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	
	//阻塞线程
	pthread_join(pth_r,NULL);
	pthread_join(pth_w,NULL);

	//关闭文件描述符
	close(sfd);
	return 0;
}

终端执行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大鱼YY

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值