-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathsocket.cpp
More file actions
47 lines (38 loc) · 1.09 KB
/
socket.cpp
File metadata and controls
47 lines (38 loc) · 1.09 KB
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
#include "winapi.h"
using namespace WinAPI;
namespace {
class WindowsSockets {
bool b_init_ok;
WindowsSockets(): b_init_ok( false ) {
WSADATA wsa;
b_init_ok = WSAStartup( 0x0202, &wsa ) == 0;
}
public:
~WindowsSockets() { WSACleanup(); } // made public for VS6 to compile
static bool IsInitialized() {
static WindowsSockets sockets;
return sockets.b_init_ok;
}
};
}
Socket::Socket( bool b_udp )
: socket( empty )
{
const int type = b_udp ? SOCK_DGRAM : SOCK_STREAM;
if ( WindowsSockets::IsInitialized() )
socket = static_cast<int>( ::socket( AF_INET, type, 0 ) );
if ( socket >= 0 ) {
int bdcast = 1;
setsockopt( socket, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>( &bdcast ), sizeof( bdcast ) );
}
}
Socket::Socket( int type, int protocol )
: socket( empty )
{
if ( WindowsSockets::IsInitialized() )
socket = static_cast<int>( ::socket( AF_INET, type, protocol ) );
if ( socket >= 0 ) {
int bdcast = 1;
setsockopt( socket, SOL_SOCKET, SO_BROADCAST, reinterpret_cast<char*>( &bdcast ), sizeof( bdcast ) );
}
}