Skip to content

Commit 4856e36

Browse files
committed
Add Windows networking support
1 parent 8b3c1db commit 4856e36

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

connector.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
#include "connector.hh"
22
#include <iostream>
3-
#include <unistd.h>
43
#include <ctime>
54
#include <sstream>
5+
#include <cstdint>
66

77
#include <cstdio>
88
#include <cstring>
99

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+
1120
#include <errno.h>
1221
#include <netdb.h>
1322
#include <sys/types.h>
1423
#include <netinet/in.h>
1524
#include <sys/socket.h>
16-
1725
#include <arpa/inet.h>
1826

27+
#endif
28+
1929
template <typename T>
2030
std::string NumberToString ( T Number )
2131
{
@@ -50,14 +60,13 @@ namespace Profiling {
5060

5161
namespace Profiling {
5262

53-
// get sockaddr, IPv4 or IPv6:
63+
// get sockaddr, IPv4 only!
5464
void *get_in_addr(struct sockaddr *sa)
5565
{
5666
if (sa->sa_family == AF_INET) {
5767
return &(((struct sockaddr_in*)sa)->sin_addr);
5868
}
59-
60-
return &(((struct sockaddr_in6*)sa)->sin6_addr);
69+
return NULL;
6170
}
6271

6372
Connector::Connector(unsigned int port, unsigned int tid)
@@ -166,7 +175,15 @@ namespace Profiling {
166175
void Connector::connect() {
167176
struct addrinfo hints, *servinfo, *p;
168177
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
170187

171188
memset(&hints, 0, sizeof hints);
172189
hints.ai_family = AF_UNSPEC;
@@ -186,7 +203,11 @@ namespace Profiling {
186203
}
187204

188205
if (::connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
206+
#ifdef WIN32
207+
closesocket(sockfd);
208+
#else
189209
close(sockfd);
210+
#endif
190211
perror("client: connect");
191212
continue;
192213
}
@@ -199,9 +220,6 @@ namespace Profiling {
199220
goto giveup;
200221
}
201222

202-
inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr),
203-
s, sizeof s);
204-
205223
freeaddrinfo(servinfo); // all done with this structure
206224

207225
_connected = true;

0 commit comments

Comments
 (0)