1
1
#include " connector.hh"
2
2
#include < iostream>
3
- #include < unistd.h>
4
3
#include < ctime>
5
4
#include < sstream>
5
+ #include < cstdint>
6
6
7
7
#include < cstdio>
8
8
#include < cstring>
9
9
10
- #include < unistd.h>
10
+ #ifdef WIN32
11
+
12
+ #include < winsock2.h>
13
+ #include < ws2tcpip.h>
14
+ #pragma comment (lib, "Ws2_32.lib")
15
+ #pragma comment (lib, "Mswsock.lib")
16
+ #pragma comment (lib, "AdvApi32.lib")
17
+
18
+ #else
19
+
11
20
#include < errno.h>
12
21
#include < netdb.h>
13
22
#include < sys/types.h>
14
23
#include < netinet/in.h>
15
24
#include < sys/socket.h>
16
-
17
25
#include < arpa/inet.h>
18
26
27
+ #endif
28
+
19
29
template <typename T>
20
30
std::string NumberToString ( T Number )
21
31
{
@@ -50,14 +60,13 @@ namespace Profiling {
50
60
51
61
namespace Profiling {
52
62
53
- // get sockaddr, IPv4 or IPv6:
63
+ // get sockaddr, IPv4 only!
54
64
void *get_in_addr (struct sockaddr *sa)
55
65
{
56
66
if (sa->sa_family == AF_INET) {
57
67
return &(((struct sockaddr_in *)sa)->sin_addr );
58
68
}
59
-
60
- return &(((struct sockaddr_in6 *)sa)->sin6_addr );
69
+ return NULL ;
61
70
}
62
71
63
72
Connector::Connector (unsigned int port, unsigned int tid)
@@ -166,7 +175,15 @@ namespace Profiling {
166
175
void Connector::connect () {
167
176
struct addrinfo hints, *servinfo, *p;
168
177
int rv;
169
- char s[INET6_ADDRSTRLEN];
178
+
179
+ #ifdef WIN32
180
+ // Initialise Winsock.
181
+ WSADATA wsaData;
182
+ int startupResult = WSAStartup (MAKEWORD (2 ,2 ), &wsaData);
183
+ if (startupResult != 0 ) {
184
+ printf (" WSAStartup failed with error: %d\n " , startupResult);
185
+ }
186
+ #endif
170
187
171
188
memset (&hints, 0 , sizeof hints);
172
189
hints.ai_family = AF_UNSPEC;
@@ -186,7 +203,11 @@ namespace Profiling {
186
203
}
187
204
188
205
if (::connect (sockfd, p->ai_addr , p->ai_addrlen ) == -1 ) {
206
+ #ifdef WIN32
207
+ closesocket (sockfd);
208
+ #else
189
209
close (sockfd);
210
+ #endif
190
211
perror (" client: connect" );
191
212
continue ;
192
213
}
@@ -199,9 +220,6 @@ namespace Profiling {
199
220
goto giveup;
200
221
}
201
222
202
- inet_ntop (p->ai_family , get_in_addr ((struct sockaddr *)p->ai_addr ),
203
- s, sizeof s);
204
-
205
223
freeaddrinfo (servinfo); // all done with this structure
206
224
207
225
_connected = true ;
0 commit comments