-
Notifications
You must be signed in to change notification settings - Fork 0
/
GCSocketPortTest.cpp
96 lines (64 loc) · 2.09 KB
/
GCSocketPortTest.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
//
// Created by gaochong on 17-12-1.
//
#include <cstring>
#include <zconf.h>
#include "GCSocketPortTest.h"
int GCSocketPortTest::TestTCPConnectPort( char* IP ,int port ){
//printf( "TestTCPConnectPort: %s:%d\n" ,IP,port );
int client_sockfd;
int ccc = 0;
struct sockaddr_in remote_addr;
//char buf[BUFSIZ];
memset(&remote_addr,0,sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr( IP );
remote_addr.sin_port = htons( port );
if( ( client_sockfd = socket( PF_INET,SOCK_STREAM,0 ) ) < 0 ) {
perror("socket error");
return -1;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 250;
//SO_SNDTIMEO
setsockopt( client_sockfd , SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
if( connect( client_sockfd,( struct sockaddr * )&remote_addr,sizeof(struct sockaddr) )<0 ) {
perror( "connect error" );
ccc = -2;
}
//printf("connected to server/n");
close(client_sockfd);
return ccc;
}
int GCSocketPortTest::TestUDPSendPort( char* IP ,int port ){
int client_sockfd;
int ccc = 0;
struct sockaddr_in remote_addr;
int sin_size = 0;
char buf[BUFSIZ];
memset(&remote_addr,0,sizeof(remote_addr));
remote_addr.sin_family = AF_INET;
remote_addr.sin_addr.s_addr = inet_addr( IP );
remote_addr.sin_port = htons( port );
if( (client_sockfd = socket(PF_INET,SOCK_DGRAM,0) )<0 ) {
perror("socket");
return -1;
}
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 200; /** 超时*/
setsockopt( client_sockfd , SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
strcpy( buf,"This is a test message" );
int bufsize = strlen(buf);
int len = sendto( client_sockfd,buf,bufsize,0,(struct sockaddr *)&remote_addr,sizeof( struct sockaddr ) );
if( len != bufsize ) {
printf( "************************* UDP sendto error *%s************************************\n" , IP);
perror("recvfrom");
ccc = -2;
}else{
ccc = 0;
}
close(client_sockfd);
return ccc;
}