-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsockets.cpp
More file actions
406 lines (323 loc) · 8.48 KB
/
sockets.cpp
File metadata and controls
406 lines (323 loc) · 8.48 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#include "sockets.h"
#include <cstring>
#include <vector>
#include <ws2tcpip.h>
#include <thread>
using namespace std;
unsigned int resolveAddress(string addr){
if(addr=="255.255.255.255")
return INADDR_BROADCAST;
unsigned int ip=0;
if((ip=inet_addr(addr.c_str()))!=INADDR_NONE)
return ip;
addrinfo hints;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
addrinfo* result = NULL;
if (getaddrinfo(addr.c_str(), NULL, &hints, &result) == 0)
if (result){
ip = ((sockaddr_in*)result->ai_addr)->sin_addr.s_addr;
freeaddrinfo(result);
return ip;
}
return 0;
}
string recv(SOCKET s, size_t maxChars){
if(s==INVALID_SOCKET) return "";
char buff[maxChars];
int n = recv(s,buff,maxChars,0);
if(n<=0)
return "";
string t(n,0);
for(int i=0; i<n; i++)
t[i]=buff[i];
return t;
}
bool send(SOCKET s, string msg){
if(s==INVALID_SOCKET) return false;
size_t sentBytes = 0;
while(sentBytes<msg.size()){
size_t sentNow = send(s,msg.c_str()+sentBytes,msg.size()-sentBytes,0);
if(sentNow==SOCKET_ERROR) return false;
sentBytes+=sentNow;
}
return true;
}
void setBlocking(SOCKET sock, bool blocking){
u_long block = blocking ? 0 : 1;
ioctlsocket(sock, FIONBIO, &block);
}
Connection getNewClient(unsigned short port){
TCPRawServer server;
if(!server.start(port))
return Connection();
server.setBlocking(true);
return server.newClient();
}
string getIp(SOCKET s){
sockaddr sa;
int n = sizeof(sa);
if(getpeername(s,&sa,&n)!=SOCKET_ERROR){
return sa.sa_data;
}
}
/** TCPCLIENT **/
/*
SOCKET _socket;
string _ip;
unsigned short _port;
bool _connected;
bool _blocking;
*/
TCPClient::TCPClient():_socket(0),_port(0),_connected(0),_blocking(0){}
TCPClient::TCPClient(string ip, unsigned short port):_socket(0),_port(0),_connected(0),_blocking(0){
connect(ip,port);
}
TCPClient::~TCPClient(){
disconnect();
}
bool TCPClient::connect(string ip, unsigned short port){
disconnect();
SOCKET sock = INVALID_SOCKET;
sockaddr_in sockInfo;
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))==INVALID_SOCKET){
return false;
}
sockInfo.sin_family = AF_INET;
sockInfo.sin_addr.s_addr = resolveAddress(ip);
sockInfo.sin_port=htons(port);
if(::connect(sock,(SOCKADDR*)&sockInfo,sizeof(sockInfo))==SOCKET_ERROR){
closesocket(sock);
return false;
}
_ip = ip;
_port = port;
_socket = sock;
_connected=true;
setBlocking(false);
_blocking = false;
return true;
}
void TCPClient::connect(SOCKET sock, string ip, unsigned short port){
disconnect();
_ip = ip;
_port = port;
_socket = sock;
_connected=true;
setBlocking(false);
_blocking = false;
}
void TCPClient::disconnect(){
if(!_connected) return;
closesocket(_socket);
_ip.clear();
_port = 0;
_socket = 0;
_connected=false;
_blocking = false;
}
string TCPClient::recv(int maxChars){
if(!_connected) return "";
char buff[maxChars];
int n = ::recv(_socket,buff,maxChars,0);
if(n==0){
disconnect();
return "";
}else if(n<0) return "";
string t(n,0);
for(int i=0; i<n; i++)
t[i]=buff[i];
return t;
}
bool TCPClient::send(const string& msg){
if(!_connected) return false;
int n = ::send(_socket, msg);
if(n == SOCKET_ERROR)
if(WSAGetLastError() == WSAENOTCONN)
disconnect();
return n;
}
bool TCPClient::isConnected()const{
return _connected;
}
string TCPClient::getIp()const{
return _ip;
}
unsigned short TCPClient::getPort()const{
return _port;
}
void TCPClient::setBlocking(bool blocking){
if(!_connected) return;
::setBlocking(_socket, blocking);
}
bool TCPClient::isBlocking()const{
return _blocking;
}
/** TCPServer **/
/*
struct _client{
SOCKET socket;
string ip;
bool blocking;
vector<string> data;
};
SOCKET _listener;
vector<_client> _clients;
unsigned short _port;
bool _on;
*/
TCPServer::TCPServer():TCPRawServer(){}
TCPServer::TCPServer(unsigned short port):TCPRawServer(port){
}
TCPServer::~TCPServer(){
finish();
}
bool TCPServer::newClient(){
if(!isOn()) return false;
_client c;
Connection t;
if((t = TCPRawServer::newClient()).sock==INVALID_SOCKET)
return false;
c.socket = t.sock;
c.ip = t.ip;
::setBlocking(c.socket, false);
_clients.push_back(c);
return true;
}
void TCPServer::disconnectClient(size_t clientN){
if(clientN<0 || clientN>=_clients.size()) return;
closesocket(_clients[clientN].socket);
_clients.erase(_clients.begin()+clientN);
}
string TCPServer::recv(size_t clientN, size_t maxChars){
if(clientN>=_clients.size()) return "";
char buff[maxChars];
int n = ::recv(_clients[clientN].socket,buff,maxChars,0);
if(n==0){
disconnectClient(clientN);
return "";
}else if(n<0) return "";
string t(n,0);
for(int i=0; i<n; i++)
t[i]=buff[i];
return t;
}
bool TCPServer::send(size_t clientN, string msg){
if(clientN<0 || clientN>=_clients.size()) return false;
return ::send(_clients[clientN].socket, msg);
}
vector<string>* TCPServer::getData(size_t clientN){
if(clientN<0 || clientN>=_clients.size()) return 0;
return &_clients[clientN].data;
}
string TCPServer::getIp(size_t clientN)const{
if(clientN<0 || clientN>=_clients.size()) return "";
return _clients[clientN].ip;
}
void TCPServer::setBlocking(size_t clientN, bool blocking){
if(clientN<0 || clientN>=_clients.size()) return;
::setBlocking(_clients[clientN].socket, blocking);
}
bool TCPServer::isBlocking(size_t clientN)const{
if(clientN<0 || clientN>=_clients.size()) return false;
return _clients[clientN].blocking;
}
size_t TCPServer::getClientCount()const{
return _clients.size();
}
/**SOCKET _listener;
unsigned short _port;
bool _blocking;
bool _on;**/
TCPRawServer::TCPRawServer():_listener(0),_port(0),_on(0){
}
TCPRawServer::TCPRawServer(unsigned short port):_listener(0),_port(0),_on(0){
start(port);
}
TCPRawServer::~TCPRawServer(){
finish();
}
bool TCPRawServer::start(unsigned short port){
finish();
addrinfo *result = NULL, hints;
ZeroMemory(&hints, sizeof (hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_PASSIVE;
if(getaddrinfo(NULL, to_string(port).c_str(), &hints, &result)!=0)
return false;
SOCKET sock = INVALID_SOCKET;
if((sock = socket(result->ai_family, result->ai_socktype, result->ai_protocol))==INVALID_SOCKET){
freeaddrinfo(result);
return false;
}
if(bind(sock, result->ai_addr, result->ai_addrlen)==SOCKET_ERROR){
freeaddrinfo(result);
closesocket(sock);
return false;
}
freeaddrinfo(result);
if(listen(sock,SOMAXCONN)==SOCKET_ERROR){
closesocket(sock);
return false;
}
_listener = sock;
_port = port;
_on=true;
return true;
}
void TCPRawServer::finish(){
if(!_on) return;
closesocket(_listener);
_listener=0;
_port=0;
_blocking = false;
_on=false;
}
Connection TCPRawServer::newClient(){
Connection t;
if(!_on) return t;
SOCKADDR_IN clientInfo = {0};
int addrsize = sizeof(clientInfo);
if((t.sock=accept(_listener, (sockaddr*)&clientInfo, &addrsize))==INVALID_SOCKET)
return t;
t.ip = inet_ntoa(clientInfo.sin_addr);
return t;
}
bool TCPRawServer::newClient(TCPSocketCallback* callback, bool detach){
Connection t;
if(!_on) return false;
SOCKADDR_IN clientInfo = {0};
int addrsize = sizeof(clientInfo);
if((t.sock=accept(_listener, (sockaddr*)&clientInfo, &addrsize))==INVALID_SOCKET)
return false;
t.ip = inet_ntoa(clientInfo.sin_addr);
thread th(callback, t);
if(detach)
th.detach();
return true;
}
bool TCPRawServer::isOn()const{
return _on;
}
unsigned short TCPRawServer::getPort()const{
return _port;
}
void TCPRawServer::setBlocking(bool blocking){
if(!_on) return;
::setBlocking(_listener,blocking);
_blocking = blocking;
}
bool TCPRawServer::isBlocking()const{
return _blocking;
}
struct WSAInitializer{
WSAInitializer(){
WSAData wsa;
WSAStartup(MAKEWORD(2,2),&wsa);
};
~WSAInitializer(){
WSACleanup();
}
}WSAIni;