-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsocket_type.cpp
More file actions
477 lines (446 loc) · 8.85 KB
/
socket_type.cpp
File metadata and controls
477 lines (446 loc) · 8.85 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#include <stdio.h>
#include "socket_type.h"
#include <thread>
int socketInit()
{
#ifdef WIN32
WORD wVersion;
WSADATA wsaData;
wVersion=MAKEWORD(2,2);
return WSAStartup(wVersion,&wsaData);
#else
return 0;
#endif
}
int socketDestory()
{
#ifdef WIN32
return WSACleanup();
#else
return errno;
#endif
}
int socketError()
{
#ifdef WIN32
return WSAGetLastError();
#else
return errno;
#endif
}
int socketSetNoBlocking(socket_t socketId)
{
int iRet=0;
#ifdef WIN32
int opt=1;
iRet=setsockopt(socketId,SOL_SOCKET,SO_REUSEADDR,(const char*)&opt,sizeof opt);
if (iRet!=0)
{
printf("set socket %d reuseaddr failed;\n",(int)socketId);
return iRet;
}
u_long argp=1;
iRet=ioctlsocket(socketId,FIONBIO,&argp);
if (iRet!=0)
{
printf("set socket %d noblock failed;\n",(int)socketId);
return iRet;
}
#else
int opts;
int opt = 1;
iRet = setsockopt(socketId, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof opt);
opts = fcntl(socketId, F_GETFL,0);
if (opts<0)
{
printf("set socket %d F_GETFL failed;\n",socketId);
return opts;
}
opts |= O_NONBLOCK;
opts = fcntl(socketId, F_SETFL,opts);
if (opts<0)
{
printf("set socket %d F_SETFL failed;\n",socketId);
return opts;
}
#endif
return iRet;
}
socket_t socketCreateTcpClient(const char* hostname,int port)
{
socket_t clientSocket;
do
{
sockaddr_in svrAddr;
memset(&svrAddr,0,sizeof svrAddr);
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=htons(INADDR_ANY);
svrAddr.sin_port=htons(0);
clientSocket=socket(AF_INET,SOCK_STREAM,0);
int socketErr;
if (clientSocket<0)
{
socketErr=socketError();
printf("Create socket failed ,errcode :%d\n",socketErr);
break;
}
int iRet=0;
iRet=bind(clientSocket,(const sockaddr*)&svrAddr,sizeof svrAddr);
if (iRet!=0)
{
break;
}
/*iRet=socketSetNoBlocking(clientSocket);
if (iRet!=0)
{
printf("Set socket %d no blocking failed;");
break;
}*/
//set recv buf size
int bufSize;
#ifdef WIN32
int sizeSize=sizeof bufSize;
#else
unsigned int sizeSize=sizeof bufSize;
#endif
iRet=getsockopt(clientSocket,SOL_SOCKET,SO_RCVBUF,(char*)&bufSize,&sizeSize);
if (iRet<0)
{
printf("get buf size failed;\n");
break;
}
int reqSize=SNDBUF_SIZE;
while (bufSize<SNDBUF_SIZE)
{
if (setsockopt(clientSocket,SOL_SOCKET,SO_RCVBUF,(const char*)&reqSize,sizeSize)>=0)
{
printf("set send buf size:%d success;\n",reqSize);
break;
}
reqSize=((reqSize+bufSize)>>1);
}
svrAddr.sin_port=htons(port);
svrAddr.sin_addr.s_addr=inet_addr(hostname);
iRet=connect(clientSocket,(const sockaddr*)&svrAddr,sizeof svrAddr);
if (iRet!=0)
{
socketErr=socketError();
if (socketErr==EWOULDBLOCK||socketErr==EINPROGRESS)
{
break;
}
printf("socket %d connect failed:%d ;\n",clientSocket,socketErr);
}else
{
printf("connect success;\n");
}
} while (0);
return clientSocket;
}
socket_t socketCreateUdpClient()
{
return socket(AF_INET, SOCK_DGRAM, 0);
}
socket_t socketCreateServer(const char* hostname,int port)
{
socket_t svrSocket;
do
{
sockaddr_in svrAddr;
memset(&svrAddr,0,sizeof svrAddr);
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=htons(INADDR_ANY);
svrAddr.sin_port=htons(port);
svrSocket=socket(AF_INET,SOCK_STREAM,0);
int socketErr;
if (svrSocket<0)
{
socketErr=socketError();
printf("Create socket failed ,errcode :%d\n",socketErr);
break;
}
int iRet;
iRet=socketSetNoBlocking(svrSocket);
if (iRet!=0)
{
printf("Set socket %d no blocking failed;");
break;
}
iRet=bind(svrSocket,(const sockaddr*)&svrAddr,sizeof svrAddr);
if (iRet!=0)
{
printf("bind svr socket failed;%d\n",socketError());
break;
}
//set send buf size
int bufSize;
#ifdef WIN32
int sizeSize=sizeof bufSize;
#else
unsigned int sizeSize=sizeof bufSize;
#endif
iRet=getsockopt(svrSocket,SOL_SOCKET,SO_SNDBUF,(char*)&bufSize,&sizeSize);
if (iRet<0)
{
printf("get buf size failed;\n");
break;
}
int reqSize=SNDBUF_SIZE;
while (bufSize<SNDBUF_SIZE)
{
if (setsockopt(svrSocket,SOL_SOCKET,SO_SNDBUF,(const char*)&reqSize,sizeSize)>=0)
{
printf("set send buf size:%d success;\n",reqSize);
break;
}
reqSize=((reqSize+bufSize)>>1);
}
} while (0);
if (svrSocket==(~0))
{
svrSocket = 0;
}
return svrSocket;
}
int socketServerListen(socket_t socketId)
{
int iRet=0;
iRet=listen(socketId,SOMAXCONN);
if (iRet!=0)
{
int err=socketError();
printf("socket %d listen failed,errcode :%d ;\n",socketId,err);
return iRet;
}
return iRet;
}
int socketSend(socket_t s, const char * buf, int len)
{
if (len == 0)
{
return 0;
}
int iRet;
#ifdef WIN32
//return send(s,buf,len,0);
WSABUF wsa_buf;
wsa_buf.buf = const_cast<char*>(buf);
wsa_buf.len = len;
DWORD num_bytes_send = 0;
iRet = WSASend(s, &wsa_buf, 1, &num_bytes_send, 0, 0, 0);
if (iRet == 0)
{
return len;
}
else if (iRet = SOCKET_ERROR)
{
iRet = socketError();
if (iRet == 111 || iRet == 0 ||
iRet == EWOULDBLOCK || iRet == 113)
{
return len;
}
else
{
return iRet;
}
}
#else
int curPos = 0;
int againTimes = 0;
while (curPos<len)
{
iRet = write(s, buf + curPos, len - curPos);
if (iRet==len-curPos)
{
return len;
}
if (iRet<=0)
{
int error = socketError();
if (error==EINTR||error==EAGAIN)
{
againTimes++;
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (againTimes>2000)
{
return iRet;
}
continue;
}
return iRet;
}
else
{
curPos += iRet;
}
}
return len;
#endif
}
int socketRecv(socket_t s,const char* buf, int len)
{
#ifdef WIN32
return recv(s,const_cast<char*>(buf),len,0);
#else
//return read(s,const_cast<char*>(buf),len);
int err = 4;
int ret = 0;
while (err == 4)
{
ret = recv(s, const_cast<char*>(buf), len, 0);
if (ret <= 0)
{
err = socketError();
if (err== EAGAIN)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
}
else if (err== EINTR)
{
err = 4;
}
}
else
{
break;
}
}
return ret;
#endif
}
void setSocketTimeout(socket_t s, timeval time)
{
#ifdef WIN32
int out = time.tv_sec * 1000 + time.tv_usec / 1000;
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char *)(&out), sizeof(out));
#else
setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, (const char*)&time, sizeof(time));
#endif // WIN32
}
int socketSendto(socket_t s,
const char *buf,
int len,
int flags,
const struct sockaddr *to,
int tolen)
{
return sendto(s, buf, len, flags, to, tolen);
}
int socketRecvfrom(socket_t s,
char *buf,
int len,
int flags,
struct sockaddr *from,
int fromlen)
{
#ifdef WIN32
return recvfrom(s, buf, len, flags, from, &fromlen);
#else
return recvfrom(s, buf, len, flags, from, (socklen_t*)&fromlen);
#endif // WIN32
}
socket_t socketCreateUdpServer(int port)
{
socket_t udpSock=-1;
int socketErr=-1;
do
{
udpSock=socket(AF_INET,SOCK_DGRAM,0);
if (udpSock<0)
{
socketErr=socketError();
printf("create socket failed,error code:%d\n",socketErr);
break;
}
int reuseFlag=1;
if (setsockopt(udpSock,SOL_SOCKET,SO_REUSEADDR,
(const char*)&reuseFlag,sizeof reuseFlag)<0)
{
printf("setsockopt reuseaddr failed;\n");
closeSocket(udpSock);
udpSock=-1;
break;
}
#ifdef WIN32
#else
#ifdef SO_REUSEPORT
if (setsockopt(udpSock, SOL_SOCKET, SO_REUSEPORT,
(const char*)&reuseFlag, sizeof reuseFlag) < 0) {
printf("setsockopt(SO_REUSEPORT) error: ");
closeSocket(udpSock);
udpSock=-1;
break;
}
#endif
#ifdef IP_MULTICAST_LOOP
const u_int8_t loop = 1;
if (setsockopt(udpSock, IPPROTO_IP, IP_MULTICAST_LOOP,
(const char*)&loop, sizeof loop) < 0) {
printf("setsockopt(IP_MULTICAST_LOOP) error: ");
closeSocket(udpSock);
udpSock=-1;
break;
}
#endif
#endif
#ifdef WIN32
#else
//if (port!=0)
{
#endif
sockaddr_in svrAddr;
svrAddr.sin_family=AF_INET;
svrAddr.sin_addr.s_addr=0;
svrAddr.sin_port=htons(port);
if (bind(udpSock,(const sockaddr*)&svrAddr,sizeof svrAddr)!=0)
{
printf("bind() error port:%d\n",port);
closeSocket(udpSock);
udpSock=-1;
break;
}
#ifdef WIN32
#else
}
#endif
} while (0);
return udpSock;
}
const char *get_server_ip()
{
char hostName[32];
gethostname(hostName,sizeof hostName);
hostent *hostInfo=gethostbyname(hostName);
char *pIp=inet_ntoa(*((in_addr*)(*hostInfo->h_addr_list)));
return pIp;
}
int get_source_port(socket_t sock_id, unsigned short &port)
{
sockaddr_in tempAddr;
tempAddr.sin_port = 0;
SOCKLEN_T len = sizeof tempAddr;
if (getsockname(sock_id, (sockaddr*)&tempAddr, (socklen_t*)&len)<0)
{
sockaddr_in svrAddr;
svrAddr.sin_family = AF_INET;
svrAddr.sin_addr.s_addr = 0;
svrAddr.sin_port = htons(port);
bind(sock_id, (const sockaddr*)&svrAddr, sizeof svrAddr);
if (getsockname(sock_id, (sockaddr*)&tempAddr, (socklen_t*)&len)<0)
{
return -1;
}
else
{
port = ntohs(tempAddr.sin_port);
return 0;
}
}
else
{
port = ntohs(tempAddr.sin_port);
return 0;
}
return -1;
}