-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathctcpserver.hpp
More file actions
57 lines (53 loc) · 1.53 KB
/
ctcpserver.hpp
File metadata and controls
57 lines (53 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//08290016
#ifndef CTCP_SERVER
#define CTCP_SERVER
#include<iostream>
#include<pthread.h>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<unistd.h>
#include<netdb.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<arpa/inet.h>
using namespace std;
struct SockInfo{
string addr;
int fd;
};
class ctcpserver {
//TCP通讯的服务端类。
private:
int m_listenfd;
//监听的socket,-1表示未初始化。
unsigned short m_port;
//服务端用于通讯的端口。
public:
int m_clientfd;
// 客户端连上来的socket, -1表示客户端未连接。
string m_clientip;
// 客户端字符串格式的IP。
ctcpserver() :m_listenfd(-1), m_clientfd(-1) {};
//初始化服务端用于监听的socket。
bool initserver(const unsigned short in_port);
bool accept();
const string & clientip( ) const;
//向对端发送报文,成功返回true,失败返回false。
bool send(const string& buffer);
//接收对端的报文,成功返回true,
//失败返回false。
//buffer-存放接收到的报文的内容,maxlen-本次接收报文的最大长度。
bool recv(string& buffer, const size_t maxlen);
//关闭监听的socket。
bool closelisten ();
//关闭客户端连上来的socket。
bool closeclient ();
~ctcpserver(){
closelisten();
closeclient();}
};
int sendString(const string& buffer,int fd);
int sendPackage(const string& buffer,const uint16_t cmd,int fd);
string GetValidData(string recvData, int totalSize);
#endif