⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tcpclithr.cpp

📁 是socket的功能包
💻 CPP
字号:
#include<stdio.h>#include<vector>#include<pthread.h>#include"../include/mysocket.h"#include"../include/mythread.h"#include"../include/TcpCliThr.h"TcpCliThr::TcpCliThr(){	ThrSet = new vector<MyThread *>();}TcpCliThr::TcpCliThr(int port, char *server):MySocket(AF_INET,		                             SOCK_STREAM, 0, port, server){	ThrSet = new vector<MyThread *>();}TcpCliThr::~TcpCliThr(){	WaitAllThr();	vector<MyThread *>::iterator it = ThrSet->begin();	while (it != ThrSet->end())	{		MyThread *thr = (MyThread *)(*it);		delete thr;		it++;	}	delete ThrSet;}int TcpCliThr::ConnectServ(){	if (Connect() == -1)	{		return -1;	}	MyThread *Rthread, *Wthread;	if (CreateThr(&Rthread, &Wthread) == -1)	{		return -1;	}	AddThread(Rthread);	AddThread(Wthread);	if (Rthread->Start())	{		return -1;	}	if (Wthread->Start())	{		return -1;	}	return 0;}int TcpCliThr::CreateThr(MyThread **Rthread, MyThread **Wthread){	printf("Accept conn\n");		*Rthread = new Receiver(Mysocket, this);	*Wthread = new Sender(Mysocket, this);	return 0;}void TcpCliThr::DealRecv(MyThread *thread){	printf("Receiver is running\n");}void TcpCliThr::DealSend(MyThread *thread){	printf("Sender is running!\n");}void TcpCliThr::AddThread(MyThread *thread){	if (thread == NULL)	{		return;	}	ThrSet->push_back(thread);}void TcpCliThr::DelThread(MyThread *thread){	vector<MyThread *>::iterator it = ThrSet->begin();	while (it != ThrSet->end())	{		if (((MyThread *)*it) == thread)		{			ThrSet->erase(it);			break;		}		it++;	}}int TcpCliThr::WaitAllThr(){	vector<MyThread *>::iterator it = ThrSet->begin();	while (it != ThrSet->end())	{		MyThread *thr = (MyThread *)(*it);		pthread_join(thr->GetId(), NULL);		it++;	}	return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -