30#ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
31#define __CLAW_SOCKET_TRAITS_UNIX_HPP__
37#include <netinet/in.h>
38#include <netinet/tcp.h>
39#include <sys/socket.h>
87 fd = socket(AF_INET, SOCK_STREAM, 0);
99 return ::close(d) == 0;
114 struct hostent* hp = gethostbyname(address.c_str());
118 struct sockaddr_in sa;
120 memset(&sa,
'\0',
sizeof(sa));
121 sa.sin_family = hp->h_addrtype;
122 sa.sin_port = htons(port);
123 memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
125 if(
::connect(d, (
struct sockaddr*)&sa, (socklen_t)
sizeof(sa)) != -1)
143 struct sockaddr_in addr;
145 memset(&addr,
'\0',
sizeof(addr));
146 addr.sin_family = AF_INET;
147 addr.sin_port = htons(port);
148 addr.sin_addr.s_addr = htonl(INADDR_ANY);
150 if(bind(d, (
struct sockaddr*)&addr,
sizeof(addr)) != -1)
151 return ::listen(d, queue_size) != -1;
168 struct timeval tv, *ptv;
175 tv.tv_sec = time_limit;
184 select(d + 1, &fds, NULL, NULL, ptv);
186 return FD_ISSET(d, &fds);
196 return ::accept(d, NULL, NULL);
216 return fstat(d, &buf) == 0;
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.
Unix interface for using sockets.
static descriptor accept(descriptor d)
Accept an incoming connexion.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
static descriptor open()
Open a socket.
static bool release()
Close the socket library.
static const descriptor invalid_socket
Invalid socket descriptor.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
int descriptor
Type of the system description of the socket.
static bool close(descriptor d)
Close a socket.
static bool init()
Initialize the use of the socket library.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
This is the main namespace.