/
UDPSocket.cpp
62 lines (50 loc) · 1.1 KB
/
UDPSocket.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
#include "UDPSocket.h"
#include <stdexcept>
#ifdef WINDOWS
void inet_aton(const char *a, IN_ADDR *n) {
n->s_addr = inet_addr(a);
}
void print_error(const char *msg) {
/* Dear Microsoft: This is *so* much easier on Unix... */
TCHAR buf[128];
memset(buf, 0, sizeof(buf));
FormatMessage(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError( ),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
buf, sizeof(buf) - 1, NULL
);
fprintf(stderr, "%s: %s\n", msg, buf);
}
#endif
UDPSocket::UDPSocket( ) {
#ifdef WINDOWS
WSADATA wsad;
if (winsock_started == 0) {
if (WSAStartup(0x0002, &wsad) != 0) {
throw std::runtime_error("Failed to initialize Windows sockets API\n");
}
}
++winsock_started;
#endif
socket_fd = socket(AF_INET, SOCK_DGRAM, 0);
if (socket_fd == -1) {
throw std::runtime_error("Failed to create UDP socket\n");
}
}
UDPSocket::~UDPSocket( ) {
#ifdef WINDOWS
closesocket(socket_fd);
--winsock_started;
if (winsock_started == 0) {
WSACleanup( );
}
#else
close(socket_fd);
#endif
}
#ifdef WINDOWS
int UDPSocket::winsock_started = 0;
#endif