30#ifndef __CLAW_SOCKET_TRAITS_WIN32_HPP__
31#define __CLAW_SOCKET_TRAITS_WIN32_HPP__
67 version = MAKEWORD( 2, 2 );
69 return WSAStartup( version, &data ) == 0;
79 return WSACleanup() == 0;
91 fd = socket(AF_INET, SOCK_STREAM, 0);
104 return ::closesocket(d) == 0;
120 struct hostent* hp = gethostbyname(address.c_str());
124 struct sockaddr_in sa;
126 memset (&sa,
'\0',
sizeof(sa));
127 sa.sin_family = hp->h_addrtype;
128 sa.sin_port = htons(port);
129 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
131 if (
::connect(d, (
struct sockaddr*)&sa,
sizeof(sa)) != SOCKET_ERROR )
150 struct sockaddr_in addr;
152 memset (&addr,
'\0',
sizeof(addr));
153 addr.sin_family = AF_INET;
154 addr.sin_port = htons(port);
155 addr.sin_addr.s_addr = htonl(INADDR_ANY);
157 if ( bind(d, (
struct sockaddr*)&addr,
sizeof(addr)) != SOCKET_ERROR )
158 return ::listen(d, queue_size) != SOCKET_ERROR;
176 struct timeval tv, *ptv;
179 if ( time_limit < 0 )
183 tv.tv_sec = time_limit;
192 select( d+1, &fds, NULL, NULL, ptv );
194 return FD_ISSET( d, &fds );
205 return ::accept( d, NULL, NULL );
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Common interface for platform specific methods needed for using sockets.
Win32 interface for using sockets.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
static const descriptor invalid_socket
Invalid socket descriptor.
SOCKET descriptor
Type of the system description of the socket.
static bool close(descriptor d)
Close a socket.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
static descriptor accept(descriptor d)
Accept an incoming connexion.
static bool init()
Initialize the use of the socket library.
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
static descriptor open()
Open a socket.
static bool release()
Close the socket library.
This is the main namespace.