服务器、客户端随时收发:多线程实现

服务器、客户端需要做到随时收发:多进程多线程实现。

server .c

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

#define PORT 7777                                  
#define IP "192.168.8.68"
#define ERR_MSG(msg) do{\
	fprintf(stderr,"line:__%d__\n",__LINE__);\
	perror(msg);\
}while(0)
//接收
char buf[128]="";
ssize_t res=0;
struct sockaddr_in cin;          //存储获取到的客户端的地址信息
//创建双线程用于收发
pthread_t tid1,tid2;

//收
void *callback1(void *arg)
{
	while(1)
	{
		bzero(buf,sizeof(buf));
		res = recv(*(int *)arg, buf,sizeof(buf) , 0);
		if(-1 == res)
		{
			ERR_MSG("recv");
			return NULL;
		}
		else if(0 == res)
		{
			fprintf(stderr,"[%s:%d] newfd=%d\n 客户端下线\n",inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),*(int *)arg);
			break;
		}
		if(!strcmp(buf,"quit"))
		{
			printf("客户端主动下线\n");
			pthread_cancel(tid2);
			break;
		}
		printf("[%s:%d] newfd=%d: %s\n",\
				inet_ntoa(cin.sin_addr),ntohs(cin.sin_port),*(int *)arg,buf);
	}	
	pthread_exit(NULL);
}
//发
void *callback2(void *arg) //void *arg=&newfd
{ 
	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");
			return NULL;
		}
		if(!strcmp(buf,"quit"))
		{
			printf("服务器退出成功\n");
			pthread_cancel(tid1);
			break;
		}
		printf("send success\n");
	}
	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;
	}

	printf("socket success\n");
	//允许端口快速重用
	int reuse=1;
	if(setsockopt(sfd,SOL_SOCKET,SO_REUSEADDR,&reuse,sizeof(reuse))<0)
	{
		ERR_MSG("setsockopt");
		return -1;
	}
	printf("允许端口快速重用\n");

	//填充地址信息结构体
	struct sockaddr_in sin; //定义结构体变量
	sin.sin_family=AF_INET; 
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);

	//绑定服务器的IP和端口
	if(bind(sfd,(struct sockaddr *)&sin,sizeof(sin))<0)
	{
		ERR_MSG("bind");
		return -1;
	}
	printf("bind success\n");

	//将套接字设置为监听状态
	if(listen(sfd,5)<0)
	{
		ERR_MSG("listen");
		return -1;
	}
	printf("listen success\n");

	//由于自动获取,只需要定义几个结构体变量进行接受,需要时直接访问里面成员即可
	socklen_t addrlen=sizeof(cin);
	//从已经完成链接的队列头获取一个客户端的信息,
	//生成一个新的文件描述符,新的才是用于和客户端通信的文件描述符
	
	int 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);

	//创建callback1线程用于服务器收
	if(pthread_create(&tid1,NULL,callback1,&newfd)!=0)
	{
		fprintf(stderr,"pthread_create error\n");
		return -1;
	}
	pthread_detach(tid1);
	//创建callback2线程用于服务器发                          
	if(pthread_create(&tid2,NULL,callback2,&newfd)!=0)
	{
		fprintf(stderr,"pthread_create error\n");
		return -1;
	}

	//阻塞线程
	pthread_join(tid2,NULL);
	//关闭文件描述符
	close(sfd);
	close(newfd);
	return 0;
}

 client.c

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

#define PORT 7777         
#define IP "192.168.8.68"

#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;
//发
void *callback1(void *arg)//void *arg=&sfd
{
	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");
			return NULL;
		}
		if(!strcmp(buf,"quit"))
		{
			printf("客户端主动下线\n");
			pthread_cancel(tid2);
			break;
		}

		printf("send success\n");
	}
	pthread_exit(NULL);
}
//收
void *callback2(void *arg) //void *arg=&newfd
{
	while(1)
	{
		//接收服务器的信息
		res = recv(*(int *)arg, buf , sizeof(buf) , 0);
		if(-1 == res)
		{
			ERR_MSG("recv");
			return NULL;
		}
		else if(0 == res)
		{
			fprintf(stderr,"sfd=%d\n 客户端下线\n",*(int *)arg);
			break;
		}
		if(!strcmp(buf,"quit"))
		{
			printf("服务器退出成功\n");
			pthread_cancel(tid1);
			break;
		}

		printf("sfd=%d: %s\n",*(int *)arg,buf);   
	}
}


int main(int argc, const char *argv[])
{
	//创建套接字
	int sfd=socket(AF_INET,SOCK_STREAM,0);
	if(sfd<0)
	{
		ERR_MSG("socket");
		return -1;
	}
	struct sockaddr_in sin; //绑定服务器信息
	socklen_t addrlen=sizeof(sin);
	sin.sin_family=AF_INET;
	sin.sin_port=htons(PORT);
	sin.sin_addr.s_addr=inet_addr(IP);
	//连接服务器
	if(connect(sfd,(struct sockaddr*)&sin,addrlen)<0)
	{
		ERR_MSG("connect");
		return -1;
	}
	printf("connect success\n");
	//创建callback1线程用于服务器发
	if(pthread_create(&tid1,NULL,callback1,&sfd)!=0)
	{
		fprintf(stderr,"pthread_create error\n");
		return -1;
	}
	pthread_detach(tid1);
	//创建callback2线程用于服务器收                      
	if(pthread_create(&tid2,NULL,callback2,&sfd)!=0)
	{
		fprintf(stderr,"pthread_create error\n");
		return -1;
	}


	//阻塞线程
	pthread_join(tid2,NULL);

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

	return 0;
}

 实现结果:

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值