#include <sockbase.h>
Public Types | |
enum | format { pf_unspec, pf_unix, pf_inet, pf_inet6, pf_max } |
Address format specifications, for the create() member. More... | |
enum | type { sock_stream, sock_dgram, sock_raw, sock_rdm, sock_seqpacket } |
Type specification for the create() method. More... | |
enum | protocol { proto_ip, proto_icmp, proto_tcp, proto_pup, proto_udp, proto_idp, proto_raw, proto_max } |
Protocol to be used on this socket. More... | |
enum | { invalid_socket } |
enum | msgflag { msg_none, msg_oob, msg_peek, msg_dontroute } |
Flags to use with recv/send methods. More... | |
enum | shuthow { shut_recv, shut_send, shut_both } |
Flags for the shutdown() method. More... | |
enum | errors { socket_error, eintr, ebadf, eacces, efault, einval, emfile, ewouldblock, einprogress, ealready, enotsock, edestaddrreq, emsgsize, eprototype, enoprotoopt, eprotonosupport, esocktnotsupport, eopnotsupp, epfnosupport, afnosupport, eaddrinuse, eaddrnotavail, enetdown, enetunreach, enetreset, econnaborted, econnreset, enobufs, eisconn, enotconn, eshutdown, etoomanyrefs, etimedout, econnrefused, eloop, enametoolong, ehostdown, ehostunreach, host_not_found, try_again, no_recovery, no_data } |
Errors that may be set by the different socklibpp::sock operations. More... | |
enum | ioctl_cmd { fionbio, fionread, siocatmark } |
Flags to use in the sock::ioctl method. More... | |
enum | mode { passive, active } |
Flags to use in the convenience constructor. More... | |
enum | optlevel { sol_socket, ipproto_tcp } |
Levels of socket options for the getsockopt/setsockopt methods. | |
enum | optname { so_acceptconn, so_broadcast, so_debug, so_dontroute, so_error, so_keepalive, so_linger, so_oobinline, so_rcvbuf, so_reuseaddr, so_sndbuf, so_type, tcp_nodelay } |
Option names for the getsockopt/setsockopt methods. More... | |
enum | selectstate { readable, writable, exceptable } |
select() results More... | |
Public Member Functions | |
sock () throw () | |
Default constructor. | |
sock (socket_type fd_) throw () | |
Constructor that uses an already created socket descriptor. | |
sock (format, type, protocol=proto_ip) throw () | |
Constructs and allocates a new socket. | |
sock | accept () throw () |
Accept incomming connections in a listening socket. | |
sock | accept (addr &) throw () |
Accepts incomming connections in a listening socket and stores the accepted address. | |
bool | bind (const addr &) throw () |
Bind the socket to a specific address. | |
bool | close () throw () |
Close the socket, releasing the resources used by it. | |
bool | connect (const addr &) throw () |
Create a connection to the specified address. | |
bool | peername (addr &) const throw () |
Get the address of the peer at which the socket is connected. | |
bool | sockname (addr &) throw () |
Get the local address of the socket. | |
bool | getsockopt (optlevel, optname, int &) const throw () |
Retrieve a socket option. | |
bool | getsockopt (optlevel, optname, bool &) const throw () |
Retrieve a socket option. | |
bool | ioctl (ioctl_cmd, unsigned long &) throw () |
Control the mode of a socket. | |
bool | listen (int=3) throw () |
Put the socket in passive (listen) mode for incomming connections. | |
int | recv (char *, int, msgflag=msg_none) throw () |
Receive data from a socket. | |
int | recvfrom (addr &, char *, int, msgflag=msg_none) throw () |
Receive a datagram and store the source address. | |
bool | can_read (long=0, long=0) const throw () |
Check if the socket has data to be read. | |
bool | wait_read (long &, long &) const throw () |
Wait until data can be read. | |
bool | wait_read () const throw () |
Wait until data can be read. | |
bool | can_write (int=0, int=0) throw () |
Check if the socket can be written to. | |
bool | can_except (int=0, int=0) throw () |
Checks for the presence of out-of-band data or if any exceptional error conditions. | |
unsigned | select (int=0, int=0) throw () |
Do a normal select(), returning the state of the socket. | |
int | send (const char *, int, msgflag=msg_none) throw () |
Send data on a connected socket. | |
int | sendto (const addr &, const char *, int, msgflag=msg_none) throw () |
Send data to a specific destination. | |
bool | setsockopt (optlevel, optname, int) throw () |
Set a socket option. | |
bool | shutdown (shuthow) throw () |
Disable sends and/or receives on a socket. | |
bool | create (format, type, protocol=proto_ip) throw () |
Create a new socket. | |
bool | socket (format fmt, type tp, protocol pr=proto_ip) throw () |
Synonym for create(). | |
bool | sendall (const char *, int, msgflag=msg_none) throw () |
Send all data. | |
bool | recvall (char *, int, msgflag=msg_none) throw () |
Recv all data. | |
bool | block (bool) throw () |
Set the blocking mode. | |
bool | reuseaddr () const throw () |
Query if it can reuse the addr. | |
bool | reuseaddr (bool) throw () |
Set the reuse addr option. | |
int | sendbuf () const throw () |
Get the size of the internal send buffer. | |
void | sendbuf (int) throw () |
Set the size of the internal send buffer. | |
int | recvbuf () const throw () |
Get the size of the internal recv buffer. | |
void | recvbuf (int) throw () |
Set the size of the internal recv buffer. | |
Public Attributes | |
socket_type | fd |
The descriptor of this socket. | |
int | error |
The error code of the last operation performed. |
This is a portability low-level wrapper class; it only stores the socket descriptor and the error code of the last call, to maintain a small footprint.
No exceptions are thrown. Most of the methods return "true" if the operation succeeded. All methodds set the sock::error member to indicate the error code of the last call; if it's zero, the operation succeeded.
Its instance doesn't own the socket descriptor (i.e. the destructor doesn't close the socket, and the default constructor doesn't create a socket). So you can copy sock objects how many times you want to. Remember to actualy create the socket when needed, and to close it before destoying it.
Address format specifications, for the create() member.
Type specification for the create() method.
Flags for the shutdown() method.
Errors that may be set by the different socklibpp::sock operations.
Flags to use in the sock::ioctl method.
Option names for the getsockopt/setsockopt methods.
so_acceptconn | (bool) Socket is listen()ing. |
so_broadcast | (bool) Socket is configured for the transmission of broadcast messages. |
so_debug | (bool) Debugging is enabled. |
so_dontroute | (bool) Routing is disabled. |
so_error | (int) Retrieve error status and clear. |
so_keepalive | (bool Keepalives are being sent. |
so_linger | (struct linger *) Returns the current linger options. |
so_oobinline | (bool) Out-of-band data is being received in the normal data stream. |
so_rcvbuf | (int) Buffer size for receives |
so_reuseaddr | (bool) The socket may be bound to an address which is already in use. |
so_sndbuf | (int) Buffer size for sends |
so_type | (int) The type of the socket (e.g. sock_stream). |
tcp_nodelay | (bool) Disables the Nagle algorithm for send coalescing. |
select() results
socklibpp::sock::sock | ( | ) | throw () [inline] |
socklibpp::sock::sock | ( | socket_type | fd_ | ) | throw () [inline] |
Constructor that uses an already created socket descriptor.
_fd | a socket_type descriptor. |
socklibpp::sock::sock | ( | sock::format | fmt, | |
sock::type | type, | |||
sock::protocol | proto = proto_ip | |||
) | throw () |
sock socklibpp::sock::accept | ( | ) | throw () |
Accept incomming connections in a listening socket.
Before calling this method the socket should be in listening mode, by calling listen().
Accepts incomming connections in a listening socket and stores the accepted address.
Before calling this method the socket should be in listening mode, by calling listen().
sa | a addr object to store the address of the accepted connection. For internet sockets this should be an addr_in object. |
bool socklibpp::sock::bind | ( | const addr & | sa | ) | throw () |
Bind the socket to a specific address.
sa | an addr object with the address to bind the socket. For internet sockets this should be an addr_in object. |
bool socklibpp::sock::close | ( | ) | throw () |
Close the socket, releasing the resources used by it.
bool socklibpp::sock::connect | ( | const addr & | sa | ) | throw () |
Create a connection to the specified address.
sa | a valid addr to make a connection to. For internet sockets it should be an addr_in object. |
bool socklibpp::sock::peername | ( | addr & | sa | ) | const throw () |
Get the address of the peer at which the socket is connected.
It is used on a connected datagram or stream socket.
sa | an addr object to where store the address. For internet sockets it should be an addr_in object. |
bool socklibpp::sock::sockname | ( | addr & | sa | ) | throw () |
Get the local address of the socket.
This call is especially useful when a listen() call has been made without doing a bind() first.
sa | an addr object to where store the address. For internet sockets it should be a addr_in object. |
bool socklibpp::sock::getsockopt | ( | sock::optlevel | level, | |
sock::optname | name, | |||
int & | val | |||
) | const throw () |
Retrieve a socket option.
Use this for options so_error, so_rcvbuf, so_sndbuf and so_type.
level | the optlevel at which the option is defined. Must be sol_socket or ipproto_tcp. | |
name | an optname option | |
val | int where to store the value for the specified option |
bool socklibpp::sock::getsockopt | ( | sock::optlevel | level, | |
sock::optname | name, | |||
bool & | val | |||
) | const throw () |
Retrieve a socket option.
Use this for so_acceptconn, so_broadcast, so_debug, so_dontlinger, so_dontroute, so_keepalive, so_oobinline, so_reuseaddr and tcp_nodelay.
level | the optlevel at which the option is defined. Must be sol_socket or ipproto_tcp. | |
name | an optname option | |
val | bool where to store the value for the specified option |
bool socklibpp::sock::ioctl | ( | sock::ioctl_cmd | cmd, | |
unsigned long & | val | |||
) | throw () |
Control the mode of a socket.
cmd | fionbio, fionread or siocatmark | |
val | where to obtain or place the parameter |
bool socklibpp::sock::listen | ( | int | backlog = 3 |
) | throw () |
Put the socket in passive (listen) mode for incomming connections.
backlog | The maximum length to which the queue of pending connections may grow. On some systems it may be limited to small values (like 5). Illegal values are replaced by the nearest legal value. |
This is typically used by servers that could have more than one connection request at a time: if a connection request arrives with the queue full, the client will receive an error with an indication of econnrefused.
int socklibpp::sock::recv | ( | char * | buf, | |
int | bs, | |||
sock::msgflag | flags = msg_none | |||
) | throw () |
Receive data from a socket.
This is used on connected datagram or sockstream sockets.
For sockets of type sock_stream, as much information as is currently available up to the size of the buffer supplied is returned. If the socket has been configured for in-line reception of out-of-band data (socket option so_oobinline) and out-of-band data is unread, only out-of-band data will be returned. The application may use the ioctl() siocatmark to determine whether any more out-of-band data remains to be read.
For datagram sockets (sock_dgram), data is extracted from the first enqueued datagram, up to the size of the size of the buffer supplied. If the datagram is larger than the buffer supplied, the excess data is lost, and the error member becomes emsgsize.
If no incoming data is available at the socket, the recv() call waits for data to arrive unless the socket is non-blocking. In this case a value of socket_error is returned with the error member set to ewouldblock. The can_read() method may be used to determine when more data arrives.
If the socket is of type sock_stream and the remote side has shut down the connection gracefully, a recv() will complete immediately with 0 bytes received. If the connection has been abortively disconnected, a recv() will fail with the error member set to econnreset.
buf | A buffer for the incoming data. | |
bs | The buffer size. The buffer may be first tested for writing permission, so never pass a size bigger than the real buffer's size. | |
flags | Specifies the way in which the call is made. Can be any or-combination of msg_none, msg_peek or msg_oob. |
number | of bytes received, if no error occurs | |
0 | if connection was closed | |
socket_error | if an error ocurred |
int socklibpp::sock::recvfrom | ( | addr & | from, | |
char * | buf, | |||
int | bs, | |||
sock::msgflag | flags = msg_none | |||
) | throw () |
Receive a datagram and store the source address.
This function is used to read incoming data on a (possibly connected) socket and capture the address from which the data was sent.
For sockets of type sock_stream, as much information as is currently available up to the size of the buffer supplied is returned. If the socket has been configured for in-line reception of out-of-band data (socket option so-oobinline) and out-of-band data is unread, only out-of-band data will be returned. The application may use the ioctl() siocatmark to determine whether any more out-of-band data remains to be read. The from parameter is ignored for sock_stream sockets.
For datagram sockets, data is extracted from the first enqueued datagram, up to the size of the size of the buffer supplied. If the datagram is larger than the buffer supplied, the buffer is filled with the first part of the message, the excess data is lost, and recvfrom() sets the error member to emsgsize.
The network address of the peer which sent the data is copied to the corresponding addr object.
If no incoming data is available at the socket, the recvfrom() call waits for data to arrive unless the socket is non-blocking. In this case a value of socket_error is returned with the error member set to ewouldblock. The can_read() method may be used to determine when more data arrives.
If the socket is of type sock_stream and the remote side has shut down the connection gracefully, a recvfrom() will complete immediately with 0 bytes received. If the connection has been abortively disconnected, a recvfrom() will fail with the error member set to econnreset.
Flags may be used to influence the behavior of the function invocation beyond the options specified for the associated socket. That is, the semantics of this function are determined by the socket options and the flags parameter.
from | An addr object. For internet sockets it should be an addr_in object. | |
buf | A buffer for the incoming data. | |
bs | The buffer size. The buffer may be first tested for writing permission, so never pass a size bigger than the real buffer's size. | |
flags | Specifies the way in which the call is made. Can be any or-combination of msg_none, msg_peek or msg_oob. |
number | of bytes received, if no error occurs | |
0 | if connection was closed | |
socket_error | if an error ocurred |
bool socklibpp::sock::can_read | ( | long | usec = 0 , |
|
long | sec = 0 | |||
) | const throw () |
Check if the socket has data to be read.
Equivalent to wait_read(usec, sec), but sec, and usec aren't output parameters.
bool socklibpp::sock::wait_read | ( | long & | usec, | |
long & | sec | |||
) | const throw () |
Wait until data can be read.
usec | microseconds to wait until the socket can be read; | |
sec | seconds to wait until the socket can be read; |
true | For listening sockets, if an incomming request has been received, so accept is guaranteed to complete without blocking. For other sockets, if there's queued data available for reading (recv() or recvfrom()) | |
false | if the timeout given by sec and usec already passed out and no data was available, or an error ocurred. Always check the error member. |
This relies on the underlying select() implementation; some systems do not inform the time spent in the select() call, so the same semantics applies.
bool socklibpp::sock::wait_read | ( | ) | const throw () |
Wait until data can be read.
Equivalent to wait_read(int&, int&), but will wait infinitely.
bool socklibpp::sock::can_write | ( | int | usec = 0 , |
|
int | sec = 0 | |||
) | throw () |
Check if the socket can be written to.
usec | microseconds to wait until the socket can be written. | |
sec | seconds to wait until the socket can be written. Set it to -1 if you want it to block. |
true | if a (non-blocking) socket is connecting the connection establishment is complete. | |
true | for other sockets a send() or sendto() will complete without blocking. (It is not specified how long this guarantee can be assumed to be valid, particularly in a multithreaded environment.) | |
false | if the timeout given by usec and sec already passed out and no data was available, or an error ocurred. Always check the error data member. |
bool socklibpp::sock::can_except | ( | int | usec = 0 , |
|
int | sec = 0 | |||
) | throw () |
Checks for the presence of out-of-band data or if any exceptional error conditions.
usec | microseconds to wait until the socket receives an exceptional event | |
sec | seconds to wait until the socket receives an exceptional event. Set it to -1 if you want it to block. |
true | There's out-of-band data (see below) to be read | |
true | The connection of a sock_stream was broken or a KEEPALIVE failure. | |
true | A connect() attempt on a (non-blocking) socket has failed. | |
false | No exceptional condition found, or an error ocurred. Always check the error member. |
unsigned socklibpp::sock::select | ( | int | usec = 0 , |
|
int | sec = 0 | |||
) | throw () |
Do a normal select(), returning the state of the socket.
usec | microseconds to wait until the socket receives an event | |
sec | seconds to wait until the socket receives an event. Set it to -1 if you want it to block. |
int socklibpp::sock::send | ( | const char * | buf, | |
int | len, | |||
sock::msgflag | flags = msg_none | |||
) | throw () |
Send data on a connected socket.
Is used on connected datagram or stream sockets and is used to write outgoing data on a socket.
If no buffer space is available within the transport system to hold the data to be transmitted, send() will block unless the socket has been placed in a non-blocking I/O mode.
The can_write() method may be used to determine when it is possible to send more data.
buf | A buffer containing the data to be transmited. | |
len | The length of the data in buf. | |
flags | Flags to be used to influence the behavior of the send() method. Can be any or-combination of msg_none, msg_dontroute and msg_oob (sock_stream only). |
[1,len] | For sock_stream sockets, indicating how much data was sent. | |
[0,len] | For other sockets, indicating how much data was sent. | |
socket_error | An error ocurred. Check the error code in the error data member. |
The successful completion of a send() does not indicate that the data was successfully delivered.
int socklibpp::sock::sendto | ( | const addr & | to, | |
const char * | buf, | |||
int | len, | |||
sock::msgflag | flags = msg_none | |||
) | throw () |
Send data to a specific destination.
Is used on datagram or stream sockets and is used to write outgoing data on a socket.
This is normally used on a sock_dgram socket to send a datagram to a specific peer socket identified by the to parameter. On a sock_stream socket, the to parameter is ignored; in this case the sendto() is equivalent to send().
If no buffer space is available within the transport system to hold the data to be transmitted, sendto() will block unless the socket has been placed in a non-blocking I/O mode.
The can_write() method may be used to determine when it is possible to send more data.
to | Address of the target socket. For internet sockets this should be an addr_in object | |
buf | A buffer containing the data to be transmited | |
len | Length of the data in buf | |
flags | Flags to be used to influence the behavior of the sendto() method. Can be any or-combination of msg_none, msg_dontroute and msg_oob (sock_stream only). |
[1,len] | For non-blocking sock_stream sockets, indicating how much data was sent. | |
[0,len] | For other sockets, indicating how much data was sent. | |
socket_error | An error ocurred. Check the error code in the error data member. |
To send a broadcast (on a sock_dgram only), the address in the to parameter should be constructed using the special IP address addr_broadcast (defined in sockaddr.h) together with the intended port number. It is generally inadvisable for a broadcast datagram to exceed the size at which fragmentation may occur, which implies that the data portion of the datagram (excluding headers) should not exceed 512 bytes.
The successful completion of a sendto() does not indicate that the data was successfully delivered.
bool socklibpp::sock::setsockopt | ( | sock::optlevel | level, | |
sock::optname | name, | |||
int | value | |||
) | throw () |
Set a socket option.
level | The level at which the option is defined. Must be sol_socket or ipproto_tcp | |
name | Socket option to be set | |
value | The new value to set |
bool socklibpp::sock::shutdown | ( | sock::shuthow | how | ) | throw () |
Disable sends and/or receives on a socket.
how | A flag that describes what type of operation will no longer be allowed. |
Never reuse a socket after it has been shut down (don't call connect() or accept() on it).
bool socklibpp::sock::create | ( | sock::format | fmt, | |
sock::type | tp, | |||
sock::protocol | proto = proto_ip | |||
) | throw () |
Create a new socket.
Allocates a socket descriptor of the specified address family, data type and protocol, as well as related resources. If a protocol is not specified (i.e. pf_unspec), the default for the specified connection mode is used.
Only a single protocol exists to support a particular socket type using a given address format. However, the address family may be given as pf_unspec (unspecified), in which case the protocol parameter must be specified. The protocol value to use is particular to the "communication domain" in which communication is to take place.
fmt | The address format specification. Use pf_inet if you want the ARPA Internet address format | |
tp | Type specification for the new socket. Common values are sock_stream and sock_dgram. | |
proto | A particular protocol to be used with the socket, or proto_ip by default |
sock_dgram supports datagrams, which are connectionless, unreliable buffers of a fixed (typically small) maximum length. Uses UDP for the Internet address family. sock_dgram sockets allow sending and receiving of datagrams to and from arbitrary peers using sendto() and recvfrom(). If such a socket is connect()ed to a specific peer, datagrams may be send to that peer send() and may be received from (only) this peer using recv().
bool socklibpp::sock::sendall | ( | const char * | buf, | |
int | len, | |||
msgflag | flags = msg_none | |||
) | throw () |
Send all data.
Convenience function that calls send() in a loop and stops only when either all data was sent or an error in the connection ocurred.
buf | The buffer with data to send. | |
len | The length of data in buf. | |
flags | Flags to use in the send() call. |
true | All data was sent. | |
false | The connection is closed or broken. |
bool socklibpp::sock::recvall | ( | char * | buf, | |
int | len, | |||
msgflag | flags = msg_none | |||
) | throw () |
Recv all data.
Convenience function that calls recv() in a loop until all data specified was received, or an error ocurred.
buf | The buffer to store the data | |
len | The length of the buffer | |
flags | The flags to use in the recv() call. |
true | All the requested data was received | |
false | The connection was closed or broken. |
bool socklibpp::sock::block | ( | bool | bl | ) | throw () |
Set the blocking mode.
Convenience function that sets the blocking mode of the socket.
bl | false for enabling non-blocking mode |
bool socklibpp::sock::reuseaddr | ( | ) | const throw () |
Query if it can reuse the addr.
Convenience function that retrives the so_reuseaddr option through getsockopt().
bool socklibpp::sock::reuseaddr | ( | bool | reuse | ) | throw () |
Set the reuse addr option.
Convenience function that sets the so_reuseaddr option through setsockopt().
reuse | Wheter the address should be reused or not. |
int socklibpp::sock::sendbuf | ( | ) | const throw () |
void socklibpp::sock::sendbuf | ( | int | size | ) | throw () |
Set the size of the internal send buffer.
size | The desired new size of the buffer. |
int socklibpp::sock::recvbuf | ( | ) | const throw () |
void socklibpp::sock::recvbuf | ( | int | size | ) | throw () |
Set the size of the internal recv buffer.
size | The desired new size of the buffer. |
The descriptor of this socket.
This usually have the size of an int, but may be signed or unsigned depending on the platform. You should never use < or > to compare it with any other value. The only useful value to compare is socklibpp::sock::invalid_socket, and you should always test using == or !=.
int socklibpp::sock::error [mutable] |
The error code of the last operation performed.
If no error ocurred in the last operation, the value is zero.