欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

socket通信多线程的服务端与客户端

程序员文章站 2022-06-06 07:58:35
...


前言

本试验是在已实现多进程服务端的基础上(详情可查看我发布的另一个文章),将数据的收发分给交给两个线程完成


一、通信服务端

/*
 * 此程序用于测试线程级的tcp服务端并发
 * 作者:zhy
 * 日期:2021/4/11
 */

#include "_public.h"

CTcpServer TcpServer;

void EXIT(int sig)
{
	printf("程序退出,信号值:%d\n",sig);
	close(TcpServer.m_listenfd);	// 手动关闭,释放资源
	pthread_mutex_destroy(&mutex);

	exit(0);
}

// 与客户端通信的主线程函数
void *pth_main_send(void *arg);
void *pth_main_recv(void *arg);

int main(int argc,char *argv[])
{
	// 忽略全部信号
	for (int ii = 0;ii < 50; ii++) signal(ii,SIG_IGN);

	signal(SIGTERM,EXIT);
	signal(SIGINT ,EXIT);

	if (TcpServer.InitServer(5000) == false)
	{ printf("服务端初始化失败,程序退出。\n"); return -1; }

	while(true)
	{
		if (TcpServer.Accept() == false) continue;

		pthread_t pthid_1,pthid_2;
		if (pthread_create(&pthid_1,NULL,pth_main_send,(void *)((long)TcpServer.m_clientfd)) != 0)
		{ printf("创建线程失败,程序退出。\n"); return -1; }

		if (pthread_create(&pthid_2,NULL,pth_main_recv,(void *)((long)TcpServer.m_clientfd)) != 0)
		{ printf("创建线程失败,程序退出。\n"); return -1; }

		pthread_detach(pthid_1);
		pthread_detach(pthid_2);

		printf("与客户端%d通信的线程已创建\n",TcpServer.m_clientfd);
	}
}

void *pth_main_send(void *arg)
{
	sleep(1);
	int num = (long)arg;
	char strbuffer[1024];

	//与服务端通讯,发送一个报文后等待回复,然后再发下一个报文
	while(1)
	 {
		 memset(strbuffer,0,sizeof(strbuffer));
		 sprintf(strbuffer,"ok");
		 if (TcpServer.Send(strbuffer,strlen(strbuffer)) <= 0) break;
		 printf("发送:%s\n",strbuffer);
		 sleep(1);
	 }

	 pthread_exit(0);
}

void *pth_main_recv(void *arg)
{
	int num = (long)arg;
	char strbuffer[1024];
	while(1)
	{
		memset(strbuffer,0,sizeof(strbuffer));
		if (TcpServer.Recv(strbuffer,sizeof(strbuffer)) <= 0) break;
		printf("接收:%s\n",strbuffer);
		sleep(1);
	}
	pthread_exit(0);
}

二、通信客户端

/*
 * 此程序用于socket通信中客户端发送与接受的并发
 * 作者:zhy 
 * 日期:2021/4/11
 */

#include "_public.h"

CTcpClient TcpClient;

void *pth_main_send(void *arg);
void *pth_main_recv(void *arg);

int main(int argc,char *argv[])
{
	// 向服务器发起连接请求
    if (TcpClient.ConnectToServer("192.168.150.135",5000) == false)
	{ printf("连接服务器失败,程序退出。\n"); return -1; }

	pthread_t pthid_1,pthid_2;

	if (pthread_create(&pthid_1,NULL,pth_main_send,(void *)1) != 0)
	{ printf("创建线程失败,程序退出。\n"); return -1; }

	if (pthread_create(&pthid_2,NULL,pth_main_recv,(void *)2) != 0)
	{ printf("创建线程失败,程序退出。\n"); return -1; }

	 pthread_join(pthid_1,NULL);
	 pthread_join(pthid_2,NULL);
}

void *pth_main_send(void *arg)
{
	int num = (long)arg;
	char strbuffer[1024];

	//与服务端通讯,发送一个报文后等待回复,然后再发下一个报文
	 for (int ii =0;ii<20;ii++)
	 {
		 memset(strbuffer,0,sizeof(strbuffer));
		 sprintf(strbuffer,"这是第%d个报文",ii);
		 if (TcpClient.Send(strbuffer,strlen(strbuffer)) <= 0) break;
		 printf("= %d =发送:%s\n",num,strbuffer);
		 sleep(1);
	 }

	 pthread_exit(0);
}

void *pth_main_recv(void *arg)
{
	sleep(1);
	int num = (long)arg;
	char strbuffer[1024];
	for (int ii =0;ii<20;ii++)
	{
		if (TcpClient.Recv(strbuffer,sizeof(strbuffer)) <= 0) break;
		printf("= %d =接收:%s\n",num,strbuffer);
		sleep(1);
	}
	pthread_exit(0);
}