30 #ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
31 #define __CLAW_SOCKET_TRAITS_UNIX_HPP__
33 #include <sys/types.h>
34 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <netinet/tcp.h>
90 fd = socket(AF_INET, SOCK_STREAM, 0);
103 return ::close(d) == 0;
119 struct hostent* hp = gethostbyname(address.c_str());
123 struct sockaddr_in sa;
125 memset (&sa,
'\0',
sizeof(sa));
126 sa.sin_family = hp->h_addrtype;
127 sa.sin_port = htons(port);
128 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
130 if (::
connect(d, (
struct sockaddr*)&sa, (socklen_t)
sizeof(sa)) != -1)
149 struct sockaddr_in addr;
151 memset (&addr,
'\0',
sizeof(addr));
152 addr.sin_family = AF_INET;
153 addr.sin_port = htons(port);
154 addr.sin_addr.s_addr = htonl(INADDR_ANY);
156 if ( bind(d, (
struct sockaddr*)&addr,
sizeof(addr)) != -1 )
157 return ::listen(d, queue_size) != -1;
175 struct timeval tv, *ptv;
178 if ( time_limit < 0 )
182 tv.tv_sec = time_limit;
191 select( d+1, &fds, NULL, NULL, ptv );
193 return FD_ISSET( d, &fds );
204 return ::accept( d, NULL, NULL );
226 return fstat(d, &buf) == 0;
231 typedef socket_traits_unix socket_traits;
234 #endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__
static descriptor accept(descriptor d)
Accept an incoming connexion.
static const descriptor invalid_socket
Invalid socket descriptor.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
int descriptor
Type of the system description of the socket.
Unix interface for using sockets.
static bool close(descriptor d)
Close a socket.
static bool init()
Initialize the use of the socket library.
static descriptor open()
Open a socket.
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.
Some assert macros to strengthen you code.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
static bool release()
Close the socket library.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.