tcpclithr.cpp
来自「是socket的功能包」· C++ 代码 · 共 130 行
CPP
130 行
#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 + =
减小字号Ctrl + -
显示快捷键?