基于Tcp协议的Socket-C++封装

C++ Tcp封装

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
// Tcp.h: 基本网络通讯封装类
//
//////////////////////////////////////////////////////////////////////

#ifndef TCP_H
#define TCP_H

#include <winsock.h>
#pragma comment(lib, "wsock32.lib") // search for wsock32 lib at compile time
#define WM_TCP WM_APP+100
class CTcp{
public:
CTcp();
~CTcp();

//extern int sd_connect, sd_bind, sd_accept;
int wsa_ok ;
static void peek_message(void);
static int get_local_ip(char *ip);

int init();
int exit();
int bind_listen(int port,sockaddr_in& addr,long &err);

int status(int sd, char *type, int timeout=5);
int bind(HWND hWnd, int port);
int bind2(HWND hWnd, char *ip, int port);
int accept(int sd, int timeout);
int connect(char *hostname, int port, int timeout, int f_noblock);
int connect2(char *bind_ip, char *hostname, int port, int timeout, int f_noblock);
void disconnect(int sd);
void close(int& sd);

int send(int sd, char *buf, int len, int timeout=10);
int recv(int sd, char *buf, int len, int timeout=10);
int gethostnamebyip(char *ip, char *name);

unsigned short htons(unsigned short);
unsigned short ntohs(unsigned short);
unsigned long int htonl(unsigned long int);
unsigned long int ntohl(unsigned long int);
unsigned __int64 ntohh(unsigned __int64);
unsigned __int64 htonh(unsigned __int64);

float htonf(float f);
float ntohf(float f);
double htond(double d);
double ntohd(double d);

char *get_remote_ip(int sd, char *ip);

};

#endif // TCP_H

下面是tcp.cpp类

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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
// Tcp.cpp: implementation of the CTcp class.
//
//////////////////////////////////////////////////////////////////////

#include "tcp.h"
//#include "stdafx.h"
#include <winsock.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "tcp.h"
extern WriteLog(char *file_name, char *format, ...);
extern WriteStat(char *format, ...);
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTcp::CTcp()
{
wsa_ok =0;
}
CTcp::~CTcp()
{
}
int CTcp::init()
{
if(!wsa_ok)
{
WSAData wsa;
//sd_bind =sd_connect =sd_accept =-1;
if(WSAStartup(MAKEWORD(1, 1), &wsa) !=0)
return -1;
wsa_ok =1;
}
return 0;
}
int CTcp::exit()
{
if(wsa_ok) WSACleanup();
return 0;
}
int CTcp::status(int sd, char *type, int timeout)
{
fd_set rset, wset, eset;
fd_set FAR *prset =NULL, *pwset =NULL, *peset =NULL;
struct timeval tval;
int i, status, err_no =0;
time_t t1, t2;
MSG msg;
tval.tv_sec =0;
tval.tv_usec =1;
time(&t1);
t2 =t1;
while(t2-t1 < timeout)
{
FD_ZERO(&rset);
FD_ZERO(&wset);
FD_ZERO(&eset);
for(i =0; i<(int)strlen(type); i++)
{
if(type[i] =='r') { FD_SET(sd, &rset); prset =&rset; }
if(type[i] =='w') { FD_SET(sd, &wset); pwset =&wset; }
if(type[i] =='e') { FD_SET(sd, &eset); peset =&eset; }
}
status =select(-1, prset, pwset, peset, &tval);
err_no =WSAGetLastError();
int err=GetLastError();
//WriteStat("select err_no=%d, err=%d", err_no, err);
time(&t2);
if(status ==0)
{
if(PeekMessage(&msg, 0, NULL, NULL, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if(msg.message ==WM_QUIT)
{
//WSASetLastError(WSAETIMEOUT);
return -1;
}
}
if(t2-t1 <timeout) continue;
else
{
if(prset) FD_CLR((UINT)sd,&rset);
if(pwset) FD_CLR((UINT)sd,&wset);
if(peset) FD_CLR((UINT)sd,&eset);
WSASetLastError(WSAEWOULDBLOCK);
return -10;
}
}
if(peset && FD_ISSET(sd, peset))
{
if(prset !=NULL) FD_CLR((UINT)sd,&rset);
if(pwset !=NULL) FD_CLR((UINT)sd,&wset);
if(peset !=NULL) FD_CLR((UINT)sd,&eset);
//err_no =WSAGetLastError();
/*
len =sizeof(errno);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (char *)&errno, &len);
*/
WSASetLastError(err_no);
return -1;
}
if((prset && FD_ISSET(sd, prset)) || (pwset && FD_ISSET(sd, pwset)))
{
//err_no =WSAGetLastError();
/*
len =sizeof(errno);
getsockopt(sd, SOL_SOCKET, SO_ERROR, (char *)&errno, &len);
*/
}
if(prset !=NULL) FD_CLR((UINT)sd,&rset);
if(pwset !=NULL) FD_CLR((UINT)sd,&wset);
if(peset !=NULL) FD_CLR((UINT)sd,&eset);
//if(status <0)
//err_no =WSAGetLastError();
WSASetLastError(err_no);
if(err_no ==WSAEINTR)
{
return WSAEINTR;
}
if(err_no)
{
return -1;
}
else break;
}
return 0;
}
int CTcp::bind(HWND hWnd, int port)
{
struct sockaddr_in addr;
char temp[200];
int sd;
sd =-1;
if((sd =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)
{
sprintf(temp, "socket failed! errno:%d", WSAGetLastError());
return -1;
}
memset(&addr, 0, sizeof(addr));
addr.sin_family =AF_INET;
addr.sin_port =htons((unsigned short)port);
int l =1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&l, sizeof(l));
/*setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, (char *)&l, sizeof(l));*/
if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
{
sprintf(temp, "bind failed! errno:%d", WSAGetLastError());
closesocket(sd);
return -1;
}
if(hWnd && WSAAsyncSelect(sd, hWnd, WM_TCP, FD_ACCEPT) !=0)
{
sprintf(temp, "tcp_bind:WSAAsyncSelect failed!");
closesocket(sd);
return -2;
}
//sd_bind =sd;
listen(sd, 5);
return sd;
}
int CTcp::bind2(HWND hWnd, char *ip, int port)
{
struct sockaddr_in addr;
char temp[200];
int sd;
sd =-1;
if((sd =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)
{
sprintf(temp, "socket failed! errno:%d", WSAGetLastError());
return -1;
}
if(ip && *ip)
{
memset(&addr, 0, sizeof(addr));
ULONG ul =inet_addr(ip);
if(ul ==0xffffffff)
{
closesocket(sd);
return -1;
}
else addr.sin_addr.s_addr=ul;
}

memset(&addr, 0, sizeof(addr));
addr.sin_family =AF_INET;
addr.sin_port =htons((unsigned short)port);
int l =1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, (char *)&l, sizeof(l));
/*setsockopt(sd, SOL_SOCKET, SO_REUSEPORT, (char *)&l, sizeof(l));*/
if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
{
sprintf(temp, "bind failed! errno:%d", WSAGetLastError());
closesocket(sd);
return -1;
}
if(hWnd && WSAAsyncSelect(sd, hWnd, WM_TCP, FD_ACCEPT) !=0)
{
sprintf(temp, "tcp_bind:WSAAsyncSelect failed!");
closesocket(sd);
return -2;
}
//sd_bind =sd;
listen(sd, 5);
return sd;
}
int CTcp::accept(int sd, int timeout)
{
int sd_acc =-1;
struct sockaddr_in sa;
int len;
/*unsigned long l;*/
if(status(sd, "rw", timeout) <0)
return -1;
len =sizeof(sa);
if((sd_acc =::accept(sd, (struct sockaddr *)&sa, &len)) <0)
return -1;
//sd_accept =sd_acc;
/* l =1;
if(ioctlsocket(sd_acc, FIONBIO, &l) <0)
{
closesocket(sd);
return -1;
}*/
return sd_acc;
}
int CTcp::connect(char *hostname, int port, int timeout, int f_noblock)
{
struct hostent *hp;
struct sockaddr_in addr;
char temp[200];
unsigned long ul;
long l;
int sd =-1, ret;
time_t t1, t2;
sd =-1;
if((sd =::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)
{
sprintf(temp, "socket failed! errno:%d", WSAGetLastError());
return -1;
}
memset(&addr, 0, sizeof(addr));
ul =inet_addr(hostname);
if(ul ==0xffffffff)
{
if((hp =gethostbyname(hostname)) ==NULL)
{
sprintf(temp, "gethostbyname and inet_addr failed! errno:%d", WSAGetLastError());
closesocket(sd);
return -1;
}
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
}
else addr.sin_addr.s_addr=ul;
addr.sin_family =AF_INET;
addr.sin_port =htons((unsigned short)port);
l =1;
if(/*f_noblock && */ioctlsocket(sd, FIONBIO, (unsigned long *)&l) <0)
{
closesocket(sd);
return -1;
}
/*if(setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char *)&l, sizeof(l)) <0)
{
closesocket(sd);
return -1;
}*/
time(&t1);
int counter=0;
while((ret =::connect(sd, (struct sockaddr *)&addr, sizeof(addr))) !=0)
{

time(&t2);
if((t2 -t1) > timeout)
{
closesocket(sd);
return -1;
}
peek_message();
int err_no =WSAGetLastError();
if(ret ==SOCKET_ERROR)
{
if(counter++>10)
{
Sleep(100);
counter=0;
}
if(err_no ==WSAEISCONN) break;
}
else if(err_no ==WSAEWOULDBLOCK /*|| err_no ==WSAEINPROGRESS*/ || err_no ==WSAEALREADY)
{
continue;
}
else
{
closesocket(sd);
return -1;
}
}
if(status(sd, "we", timeout) <0)
{
sprintf(temp, "status:Á¬½Ó·þÎñÆ÷ʧ°Ü!\n¼ì²é·þÎñÆ÷¶Ë³ÌÐòÊÇ·ñÔËÐÐ\nÇÒÖ÷»úµØÖ·ÊÇ·ñ%s, ¶Ë¿ÚÊÇ·ñ%d\n"
"errno=%d",
hostname, port, WSAGetLastError());
closesocket(sd);
return -1;
}
//sd_connect =sd;
return sd;
}
int CTcp::connect2(char *bind_ip, char *hostname, int port, int timeout, int f_noblock)
{
struct hostent *hp;
struct sockaddr_in addr;
char temp[200];
unsigned long ul;
long l;
int sd =-1, ret;
time_t t1, t2;
sd =-1;
if((sd =socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) <0)
{
sprintf(temp, "socket failed! errno:%d", WSAGetLastError());
return -1;
}
if(bind_ip && *bind_ip)
{
memset(&addr, 0, sizeof(addr));
addr.sin_family =AF_INET;
ul =inet_addr(bind_ip);
if(ul ==0xffffffff)
{
closesocket(sd);
return -1;
}
else addr.sin_addr.s_addr=ul;
if(::bind(sd, (struct sockaddr *)&addr, sizeof(addr)) <0)
{
sprintf(temp, "tcp_connect: bind failed! errno:%d", WSAGetLastError());
closesocket(sd);
return -1;
}
}
memset(&addr, 0, sizeof(addr));
ul =inet_addr(hostname);
if(ul ==0xffffffff)
{
if((hp =gethostbyname(hostname)) ==NULL)
{
sprintf(temp, "gethostbyname and inet_addr failed! errno:%d", WSAGetLastError());
closesocket(sd);
return -1;
}
memcpy(&addr.sin_addr, hp->h_addr, hp->h_length);
}
else addr.sin_addr.s_addr=ul;
addr.sin_family =AF_INET;
addr.sin_port =htons((unsigned short)port);
l =1;
if(/*f_noblock && */ioctlsocket(sd, FIONBIO, (unsigned long *)&l) <0)
{
closesocket(sd);
return -1;
}
/*if(setsockopt(sd, IPPROTO_TCP, TCP_NODELAY, (char *)&l, sizeof(l)) <0)
{
closesocket(sd);
return -1;
}*/
time(&t1);
int counter=0;
while((ret =::connect(sd, (struct sockaddr *)&addr, sizeof(addr))) !=0)
{
time(&t2);
if((t2 -t1) > timeout)
{
closesocket(sd);
return -1;
}
peek_message();
int err_no =WSAGetLastError();
if(ret ==SOCKET_ERROR)
{
if(counter++>10)
{
Sleep(100);
counter=0;
}
if(err_no ==WSAEISCONN) break;
}
else if(err_no ==WSAEWOULDBLOCK /*|| err_no ==WSAEINPROGRESS*/ || err_no ==WSAEALREADY)
{
continue;
}
else
{
closesocket(sd);
return -1;
}
}
if(status(sd, "we", timeout) <0)
{
sprintf(temp, "status:Á¬½Ó·þÎñÆ÷ʧ°Ü!\n¼ì²é·þÎñÆ÷¶Ë³ÌÐòÊÇ·ñÔËÐÐ\nÇÒÖ÷»úµØÖ·ÊÇ·ñ%s, ¶Ë¿ÚÊÇ·ñ%d\n"
"errno=%d",
hostname, port, WSAGetLastError());
closesocket(sd);
return -1;
}
//sd_connect =sd;
return sd;
}
void CTcp::disconnect(int sd)
{
if(sd >0)
{
closesocket(sd);
//if(sd ==sd_connect) sd_connect =-1;
//if(sd ==sd_accept) sd_accept =-1;
//if(sd ==sd_bind) sd_bind =-1;
}
}
int CTcp::send(int sd, char *buf, int len, int timeout)
{
int len1, len_send =0;
time_t t1, t2;
len_send =0;
time(&t1);
t2 =t1;
int counter=0;
while(len_send <len)
{
if(t2-t1 >timeout)
return len_send;
if(status(sd, "w", timeout-(t2-t1)) <0)
{
return len_send;
}
if((len1 =::send(sd, &buf[len_send], len-len_send, 0)) <=0)
{
if(len1 ==SOCKET_ERROR && GetLastError() ==WSAEWOULDBLOCK)
{
time(&t2);

if(counter++>100)
{
Sleep(100);
counter=0;
}
continue;
}
return len_send;
}
len_send +=len1;
time(&t2);
}
return len_send;
}
int CTcp::recv(int sd, char *buf, int len, int timeout)
{
int len1, len_recv;
time_t t2, t1;
len_recv =0;
time(&t1);
t2 =t1;
int err =0;
int counter=0;
while(len_recv <len)
{
if(t2 -t1 >timeout)
{
WSASetLastError(err);
return len_recv;
}
if(status(sd, "r", timeout-(t2-t1)) <0)
{
err =WSAGetLastError();
//WriteStat("tcp_status err=%d", WSAGetLastError());
WSASetLastError(err);
return len_recv;
}
if((len1 =::recv(sd, &buf[len_recv], len-len_recv, 0)) <=0)
{
err =WSAGetLastError();
//WriteStat("recv err=%d, len1=%d", err, len1);
if(timeout ==0) break;
if(len1 ==SOCKET_ERROR && err ==WSAEWOULDBLOCK)
{
time(&t2);
if(counter++>100)
{
Sleep(100);
counter=0;
}
continue;
}
WSASetLastError(err);
return len_recv;
}
len_recv +=len1;
time(&t2);
}
WSASetLastError(err);
return len_recv;
}
int CTcp::gethostnamebyip(char *ip, char *host)
{
struct hostent *hp;
struct in_addr ul;
host[0] =0;
ul.S_un.S_addr =inet_addr(ip);
if(ul.S_un.S_addr ==0xFFFFFFFF) return -1; // ip error or ip is hostname
hp =gethostbyaddr((char *)&ul, 4, AF_INET);
if(hp ==NULL) return -1; // can not get hostname
strcpy(host, hp->h_name);
return 0;
}
int CTcp::get_local_ip(char *ip)
{
struct hostent *hp;
char host[50], *p;
if(gethostname(host, sizeof(host)) <0) return -1;
hp =gethostbyname(host);
if(hp ==NULL) return -1;
p =(char *)hp->h_addr;
wsprintfA(ip, "%d.%d.%d.%d", (int)p[0]&0xFF, (int)p[1]&0xFF, (int)p[2]&0xFF, (int)p[3]&0xFF);
return 0;
}
char* CTcp::get_remote_ip(int sd, char *ip)
{
struct sockaddr_in addr_in;
int len =sizeof(addr_in);
char *p1;
if(sd <0) return NULL;
if(getpeername(sd, (struct sockaddr *)&addr_in, &len) <0)
return NULL;
p1 =(char *)&addr_in.sin_addr;
sprintf(ip, "%d.%d.%d.%d", ((int)p1[0]) &0xff, ((int)p1[1]) &0xff, (int)p1[2] &0xff, (int)p1[3]&0xff);

return ip;
}
unsigned short CTcp::htons(unsigned short s)
{
return ::htons(s);
}
unsigned short CTcp::ntohs(unsigned short s)
{
return ::ntohs(s);
}
unsigned long int CTcp::htonl(unsigned long int l)
{
return ::htonl(l);
}
unsigned long int CTcp::ntohl(unsigned long int l)
{
return ::ntohl(l);
}
float CTcp::htonf(float f)
{
unsigned char *p, p0, p1;
if(htons(1) ==1) return f;
p =(unsigned char *)&f;
p0 =p[0];
p1 =p[1];
p[0] =p[3];
p[3] =p0;
p[1] =p[2];
p[2] =p1;
return f;
}
float CTcp::ntohf(float f)
{
unsigned char *p, p0, p1;
if(ntohs(1) ==1) return f;
p =(unsigned char *)&f;
p0 =p[0];
p1 =p[1];
p[0] =p[3];
p[3] =p0;
p[1] =p[2];
p[2] =p1;
return f;
}
double CTcp::htond(double d)
{
unsigned char *p, p0, p1, p2, p3;
if(htons(1) ==1) return d;
p =(unsigned char *)&d;
p0 =p[0];
p1 =p[1];
p2 =p[2];
p3 =p[3];
p[0] =p[7];
p[7] =p0;
p[1] =p[6];
p[6] =p1;
p[2] =p[5];
p[5] =p2;
p[3] =p[4];
p[4] =p3;
return d;
}
double CTcp::ntohd(double d)
{
unsigned char *p, p0, p1, p2, p3;
if(ntohs(1) ==1) return d;
p =(unsigned char *)&d;
p0 =p[0];
p1 =p[1];
p2 =p[2];
p3 =p[3];
p[0] =p[7];
p[7] =p0;
p[1] =p[6];
p[6] =p1;
p[2] =p[5];
p[5] =p2;
p[3] =p[4];
p[4] =p3;
return d;
}
unsigned __int64 CTcp::htonh(unsigned __int64 d)
{
unsigned char *p, p0, p1, p2, p3;
if(htons(1) ==1) return d;
p =(unsigned char *)&d;
p0 =p[0];
p1 =p[1];
p2 =p[2];
p3 =p[3];
p[0] =p[7];
p[7] =p0;
p[1] =p[6];
p[6] =p1;
p[2] =p[5];
p[5] =p2;
p[3] =p[4];
p[4] =p3;
return d;
}
unsigned __int64 CTcp::ntohh(unsigned __int64 d)
{
unsigned char *p, p0, p1, p2, p3;
if(ntohs(1) ==1) return d;
p =(unsigned char *)&d;
p0 =p[0];
p1 =p[1];
p2 =p[2];
p3 =p[3];
p[0] =p[7];
p[7] =p0;
p[1] =p[6];
p[6] =p1;
p[2] =p[5];
p[5] =p2;
p[3] =p[4];
p[4] =p3;
return d;
}
void CTcp::peek_message(void)
{
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
int CTcp::bind_listen(int port,sockaddr_in& addr,long &err)
{
int sock;


sock = socket(AF_INET, SOCK_STREAM, 0); // Create socket

addr.sin_family = AF_INET; // Address family Internet
addr.sin_port = htons (port); // Assign port 'port' to this socket
addr.sin_addr.s_addr = htonl (INADDR_ANY); // No destination

if (::bind (sock, (LPSOCKADDR) &addr, sizeof (addr)) == SOCKET_ERROR)
{
closesocket (sock);
err = WSAGetLastError ();
return -1;
}

if (::listen (sock, 10) == SOCKET_ERROR)
{
closesocket (sock);
err = WSAGetLastError ();
return -1;
}
return sock;
}
void CTcp::close(int &sd)
{
shutdown(sd,0);
closesocket(sd);
sd = INVALID_SOCKET;
}

下面是main.cpp

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
#include <iostream>
#include "tcp.h"
#include <WinSock2.h>
#pragma comment(lib, "ws2_32.lib");

#define PORT 9995

using namespace std;

int main(int argc, char *argv[])
{

WSADATA wsaData;
WORD sockVersion = MAKEWORD(2, 2);
::WSAStartup(sockVersion, &wsaData);

char lv_name[50];
gethostname(lv_name, 50);


// hostent * lv_pHostent;
// lv_pHostent = (hostent *)malloc(sizeof(hostent));
// if( NULL == (lv_pHostent = gethostbyname(lv_name)))
// {
// printf("get Hosrname Fail \n");
// return 0;
// }
// SOCKADDR_IN lv_sa;
// lv_sa.sin_family = AF_INET;
// lv_sa.sin_port = htons(6000);
// memcpy(&lv_sa.sin_addr.S_un.S_addr, lv_pHostent->h_addr_list[0], lv_pHostent->h_length);
// printf("%s\n", inet_ntoa(lv_sa.sin_addr));
CTcp tcp_client;
tcp_client.init();
cout << "please input str" << endl;
char buf[1024] = {0};
memset(buf, 0, sizeof(buf));
// gets(buf);

char *hostname = "192.168.1.137";
int sd = tcp_client.connect(hostname, PORT, 3000, 0);
char *ip;

if(sd != -1){
tcp_client.get_remote_ip(sd, ip);
cout << "success connection ip:" << ip << endl;
while(1){
memset(buf, 0, sizeof(buf));
gets(buf);
int re = tcp_client.send(sd, buf, sizeof(buf), 10);
if(strncmp(buf, "exit", 4) == 0)
break;
}

}

cout << "over" << endl;
return 0;
}