forked from a5012847/ZeroNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtcpsocket.cpp
More file actions
144 lines (117 loc) · 3.57 KB
/
tcpsocket.cpp
File metadata and controls
144 lines (117 loc) · 3.57 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "tcpsocket.h"
TcpSocket::TcpSocket() : mSock(SOCKET_ERROR)
{
}
std::string TcpSocket::fromDomainToIP(std::string domain)
{
// 获取主机信息
struct hostent *ht = gethostbyname(domain.data());
if(!ht) {
std::cout << "Failed to get host" << std::endl;
std::fflush(stdout);
return std::string();
}
// 分解IPV4地址
std::string ip;
for(int i = 0;i < ht->h_length;i ++) {
char szTmp[10];
std::sprintf(szTmp, "%d.",(unsigned char)ht->h_addr_list[0][i]);
ip.append(szTmp);
}
ip.pop_back();
return ip;
}
bool TcpSocket::connectTo(std::string domain, int port)
{
if ((int)mSock != SOCKET_ERROR) {
std::cout << "Socket is using" << std::endl;\
std::fflush(stdout);;
return false;
}
mPort = port;
// 把域名转换成ip
mIp = fromDomainToIP(domain);
// 创建socket
if ((int)(mSock = socket(AF_INET,SOCK_STREAM,0)) == SOCKET_ERROR) {
std::cout << "Failed to create socket " << std::endl;
std::fflush(stdout);
return false;
}
mAddr.sin_family = AF_INET;
mAddr.sin_addr.S_un.S_addr = inet_addr(mIp.data());
mAddr.sin_port = htons(mPort);
// 连接至服务端
if (connect(mSock, (SOCKADDR *)&mAddr, sizeof(mAddr)) == SOCKET_ERROR) {
std::cout << "Failed to connect to " << mIp << std::endl;
std::fflush(stdout);
dissconnect();
return false;
}
std::cout << "Connect to success " << mIp << std::endl;
std::fflush(stdout);;
return true;
}
void TcpSocket::dissconnect()
{
if ((int)mSock != SOCKET_ERROR) {
closesocket(mSock);
mSock = SOCKET_ERROR;
std::cout << "Closed socket from " << mIp << std::endl;
std::fflush(stdout);;
}
}
bool TcpSocket::sendData(const char *data,unsigned int size)
{
if ((int)mSock == SOCKET_ERROR) {
std::cout << "Socket do not allowed to send data without connected " << std::endl;
std::fflush(stdout);;
return false;
}
int ret = SOCKET_ERROR;
const unsigned int packetLen = 800;
// 小于pakcetLen的话,就直接发送,不然就分包发送
//
//对发送数据进行加密
//
if (size <= packetLen) {
ret = send(mSock, data, size, 0);
// 出现错误,断开连接
if (ret == SOCKET_ERROR) {
std::cout << "Failed to send data to " << mIp << std::endl;
std::fflush(stdout);;
dissconnect();
}
} else {
unsigned int pos = 0;
while (pos < size) {
unsigned int sendSize = pos+packetLen > size ? size-pos : packetLen;
// 发送
ret = send(mSock, data+pos, sendSize, 0);
// 出现错误,断开连接
if (ret == SOCKET_ERROR) {
std::cout << "Failed to send data to " << mIp << std::endl;
std::fflush(stdout);;
dissconnect();
break;
}
pos += packetLen;
}
}
return ret != SOCKET_ERROR ? true : false;
}
int TcpSocket::recvData(char *data, int size)
{
if ((int)mSock == SOCKET_ERROR) {
std::cout << "Socket do not allowed to reveive data without connected " << std::endl;
std::fflush(stdout);;
return -1;
}
int ret = recv(mSock, data, size, 0);
// 出现错误,断开连接
if (ret == SOCKET_ERROR || ret == 0) {
std::cout << "Failed to receive data from " << mIp << std::endl;
std::fflush(stdout);;
dissconnect();
}
return ret;
}