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

服务器

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


//封装一个报错的宏函数
#define ERR_MSG(msg) do{\
	fprintf(stderr,"line:%d\n",__LINE__);\
	perror(msg);\
}while(0)


char buf[128] = "";
ssize_t res = 0;
pthread_t tid1,tid2;

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

//线程1接收
void* callback1(void* arg)
{
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recv(*((int*)arg),buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0 == res)
		{
			fprintf(stderr,"客户端已下线\n");
			break;
		}
		printf("[%s:%d] new_sfd=%d: %s\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),*((int*)arg),buf);
		printf("recv success\n");
	}
	pthread_cancel(tid2);
	pthread_exit(NULL);
}


//线程2发送
void* callback2(void* arg)
{
	while(1)
	{
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;

		if(send(*((int*)arg),buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			break;
		}
	}
	pthread_cancel(tid1);
	pthread_exit(NULL);
}



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;
	}


	//填充地址信息结构体,地址信息族不同,地址信息结构体不一致
	struct sockaddr_in sin;
	sin.sin_family      =AF_INET;   //必须填AF_INET
	sin.sin_port        =htons(43210);   //端口号的网络字节序,1024~49151
	sin.sin_addr.s_addr =inet_addr("192.168.8.90");    //本机IP地址网络字节序,ifconfig查看    
	//绑定服务器的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");

	//struct sockaddr_in cin;    //存储获取到的客户端的地址信息结构体
	//socklen_t addrlen = sizeof(cin);   //真实地址信息结构体
	//从已经生成的连接的队列中获取一个客户端的信息,生成一个新的文件
	//生成的新的文件描述符才是与客户端通信的文件描述符
	int new_sfd = 0;
	//new_sfd = accept(sfd,NULL,NULL);  //若不想获取客户端信息则填NULL
	new_sfd = accept(sfd,(struct sockaddr*)&cin,&addrlen);
	if(new_sfd < 0)
	{
		ERR_MSG("accept");
		return -1;
	}
	//printf("new_sfd=%d\n",new_sfd);
	printf("[%s:%d] new_sfd = %d\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),new_sfd);


	//创建两个分支线程
	//线程1接收
	if(pthread_create(&tid1,NULL,callback1,&new_sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	//线程2发送
	if(pthread_create(&tid2,NULL,callback2,&new_sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);


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

客户端

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


//封装一个报错的宏函数
#define ERR_MSG(msg) do{\
	fprintf(stderr,"line:%d\n",__LINE__);\
	perror(msg);\
}while(0)


char buf[128] = "";
ssize_t res = 0;
pthread_t tid1,tid2;


//线程1发送
void* callback1(void* arg)
{
	while(1)
	{
		bzero(buf,sizeof(buf));
		fgets(buf,sizeof(buf),stdin);
		buf[strlen(buf)-1] = 0;
		if(strcmp(buf,"quit") == 0)
		{
			printf("客户端退出\n");
			break;
		}

		if(send(*((int*)arg),buf,sizeof(buf),0) < 0)
		{
			ERR_MSG("send");
			break;
		}
	}
	pthread_cancel(tid2);
	pthread_exit(NULL);
}


//线程2接收
void* callback2(void* arg)
{
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recv(*((int*)arg),buf,sizeof(buf),0);
		if(res < 0)
		{
			ERR_MSG("recv");
			break;
		}
		else if(0 == res)
		{
			fprintf(stderr,"客户端下线\n");
			break;
		}
		printf("收到信息 %s\n",buf);
	}
	pthread_cancel(tid1);
	pthread_exit(NULL);
}



int main(int argc, const char *argv[])
{
	//创建流式套接字
	int sfd = socket(AF_INET,SOCK_STREAM,0);
	if(sfd < 0)
	{
		ERR_MSG("socket");
		return -1;
	}



	//允许端口快速重用———>允许程序退出后,端口号能被新的进程快速覆盖
	int reuse = 1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse)) < 0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}


	//填充地址信息结构体,地址信息族不同,地址信息结构体不一致
	struct sockaddr_in sin;
	sin.sin_family      =AF_INET;   //必须填AF_INET
	sin.sin_port        =htons(43210);   //端口号的网络字节序,1024~49151
	sin.sin_addr.s_addr =inet_addr("192.168.8.90");    //本机IP地址网络字节序,ifconfig查看   


	//与服务器建立联系
	if(connect(sfd,(struct sockaddr*)&sin,sizeof(sin)) < 0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success\n");

	//创建两个分支线程
	//线程1接收
	if(pthread_create(&tid1,NULL,callback1,&sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}
	//线程2发送
	if(pthread_create(&tid2,NULL,callback2,&sfd) != 0)
	{
		ERR_MSG("pthread_create");
		return -1;
	}

	pthread_join(tid1,NULL);
	pthread_join(tid2,NULL);

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

运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值