socklibpp::sock Class Reference

The socklibpp::sock class represents a socket. All common socket routines are wrapped as member functions. More...

#include <sockbase.h>

List of all members.

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.


Detailed Description

The socklibpp::sock class represents a socket. All common socket routines are wrapped as member functions.

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.

Examples:

daytimed.cpp, echod.cpp, listener.cpp, and streamechod.cpp.


Member Enumeration Documentation

enum socklibpp::sock::format

Address format specifications, for the create() member.

Enumerator:
pf_unspec  Unspecified.
pf_unix  Unix pipes.
pf_inet  IPv4 format.
pf_inet6  IPv6 format.
pf_max  not used

enum socklibpp::sock::type

Type specification for the create() method.

Enumerator:
sock_stream  Stream sockets (TCP).
sock_dgram  Datagram sockets.
sock_raw  Raw sockets.

enum socklibpp::sock::protocol

Protocol to be used on this socket.

Enumerator:
proto_ip  Default protocol for the specified socket type.
proto_icmp  ICMP.
proto_tcp  TCP.
proto_pup  PUP.
proto_udp  UDP.
proto_idp  IDP.
proto_raw  RAW.
proto_max  not used

anonymous enum

Enumerator:
invalid_socket  An invalid socket.

enum socklibpp::sock::msgflag

Flags to use with recv/send methods.

Enumerator:
msg_none  Default.
msg_oob  send/recv out-of-band data (sock_stream only).
msg_peek  Peek at the incoming data.
msg_dontroute  Should not be subject to routing.

enum socklibpp::sock::shuthow

Flags for the shutdown() method.

Enumerator:
shut_recv  Disallows further recv() calls.
shut_send  Disallows further send() calls.
shut_both  Disallows both send() and recv() calls.

enum socklibpp::sock::errors

Errors that may be set by the different socklibpp::sock operations.

Enumerator:
socket_error  Error value used as result for some functions.
ewouldblock  The requested operation would block, try again later.

enum socklibpp::sock::ioctl_cmd

Flags to use in the sock::ioctl method.

Enumerator:
fionbio  Enable or disable non-blocking mode on the socket.
fionread  Determine the amount of data which can be read atomically from socket.
siocatmark  Determine whether or not all out-of-band data has been read.

enum socklibpp::sock::mode

Flags to use in the convenience constructor.

Enumerator:
passive  Create a sock_stream socket and put it in listen mode.
active  Create a sock_stream socket and connect it.

enum socklibpp::sock::optname

Option names for the getsockopt/setsockopt methods.

Enumerator:
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.

enum socklibpp::sock::selectstate

select() results

Enumerator:
readable  there is data waiting to be read
writable  socket can be written to without blocking
exceptable  there is an exceptional event pending


Constructor & Destructor Documentation

socklibpp::sock::sock (  )  throw () [inline]

Default constructor.

It doesn't allocate resources for a socket, you have to call create() to actually allocate a socket.

See also:
create()

socklibpp::sock::sock ( socket_type  fd_  )  throw () [inline]

Constructor that uses an already created socket descriptor.

Parameters:
_fd a socket_type descriptor.

socklibpp::sock::sock ( sock::format  fmt,
sock::type  type,
sock::protocol  proto = proto_ip 
) throw ()

Constructs and allocates a new socket.

Parameters:
fmt an address format
type socket type
proto protocol for this socket
See also:
format, type, protocol


Member Function Documentation

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().

Returns:
A new sock object representing the new connection; if an error occurs, the fd member has the value invalid_socket.
Note:
You should always check the error member to see if the operation succeeded. In case of errors, the sock returned is invalid, and should be ignored.
See also:
listen()
Examples:
daytimed.cpp, echod.cpp, listener.cpp, and streamechod.cpp.

sock socklibpp::sock::accept ( addr sa  )  throw ()

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().

Parameters:
sa a addr object to store the address of the accepted connection. For internet sockets this should be an addr_in object.
Returns:
A new sock object representing the new connection; if an error occurs, the fd member has the value invalid_socket.
Note:
You should always check the error member to see if the operation succeeded. In case of errors, the sock returned is invalid, and should be ignored.
See also:
listen()

bool socklibpp::sock::bind ( const addr sa  )  throw ()

Bind the socket to a specific address.

Parameters:
sa an addr object with the address to bind the socket. For internet sockets this should be an addr_in object.
Returns:
true if no error occurred
Examples:
daytimed.cpp, echod.cpp, and streamechod.cpp.

bool socklibpp::sock::close (  )  throw ()

Close the socket, releasing the resources used by it.

Returns:
true if no error occurred
Examples:
daytimed.cpp, echod.cpp, and listener.cpp.

bool socklibpp::sock::connect ( const addr sa  )  throw ()

Create a connection to the specified address.

Parameters:
sa a valid addr to make a connection to. For internet sockets it should be an addr_in object.
Returns:
true if no error occurred

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.

Parameters:
sa an addr object to where store the address. For internet sockets it should be an addr_in object.
Returns:
true if no error occurred

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.

Parameters:
sa an addr object to where store the address. For internet sockets it should be a addr_in object.
Returns:
true if no error ocurred
Note:
If the socket was bound to addr_any, indicating that any of the host's IP addresses should be used for the socket, sockname() will not necessarily return information about the host IP address, unless the socket has been connected with connect() or accept().

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.

Parameters:
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
Returns:
true if no error ocurred
See also:
optlevel, optname, setsockopt

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.

Parameters:
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
Returns:
true if no error ocurred
See also:
optlevel, optname, setsockopt

bool socklibpp::sock::ioctl ( sock::ioctl_cmd  cmd,
unsigned long &  val 
) throw ()

Control the mode of a socket.

Parameters:
cmd fionbio, fionread or siocatmark
val where to obtain or place the parameter
Returns:
true if no error occurred
It may be used on any socket in any state. It is used to retrieve parameters independent of the protocol and communication subsystem.
See also:
getsockopt(), setsockopt()

bool socklibpp::sock::listen ( int  backlog = 3  )  throw ()

Put the socket in passive (listen) mode for incomming connections.

Parameters:
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.
Returns:
true if no error occurred
To accept connections the socket must first be created with create(), a backlog is specified with listen(), and the connections are accepted with accept(). listen() only applies to sockets that support connections, i.e. sock_stream.

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.

See also:
accept(), bind(), connect()
Examples:
daytimed.cpp, echod.cpp, and streamechod.cpp.

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.

Parameters:
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.
Return values:
number of bytes received, if no error occurs
0 if connection was closed
socket_error if an error ocurred
See also:
recvfrom(), recvall(), can_read(), create(), block()
Examples:
echod.cpp, and listener.cpp.

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.

Parameters:
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.
Return values:
number of bytes received, if no error occurs
0 if connection was closed
socket_error if an error ocurred
See also:
recv(), recvall(), can_read(), create(), block()

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.

See also:
wait_read()

bool socklibpp::sock::wait_read ( long &  usec,
long &  sec 
) const throw ()

Wait until data can be read.

Parameters:
usec microseconds to wait until the socket can be read;
sec seconds to wait until the socket can be read;
Return values:
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.
Note:
After the call, sec and usec may contain the time spent in the call; this may be less, but also more than you specified (select is a system call after all). For sock_stream sockets, wait_read() will also return true if the connection has been closed, so the next recv or recvfrom will complete without blocking. If the connection was closed gracefully, then a recv() will return with 0 bytes read; if the connection was closed abortively, recv() will return socket_error, and the error member will be set to econnreset The presence of out-of-band data will be checked if the socket option SO_OOBINLINE has been enabled.

This relies on the underlying select() implementation; some systems do not inform the time spent in the select() call, so the same semantics applies.

See also:
ioctl(), setsockopt(), accept(), recv(), can_read()
Examples:
echod.cpp.

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.

Parameters:
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.
Return values:
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.
See also:
ioctl(), setsockopt(), connect(), send()

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.

Parameters:
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.
Return values:
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.
Note:
Out-of-band data will only be reported in this way if the option so_oobinline is false.
See also:
ioctl(), setsockopt(), connect()

unsigned socklibpp::sock::select ( int  usec = 0,
int  sec = 0 
) throw ()

Do a normal select(), returning the state of the socket.

Parameters:
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.
Returns:
sock::selectstate values OR'ed together. That is, (sock::readable | sock::writable) if the socket is both readable and writable, and so on.
See also:
ioctl(), setsockopt(), connect(), can_read(), can_write(), can_except()

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.

Parameters:
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).
Return values:
[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.
Note:
For datagram sockets, care must be taken not to exceed the maximum IP packet size of the underlying subnets If the data is too long to pass atomically through the underlying protocol the error emsgsize is set on the error member, and no data is transmitted.

The successful completion of a send() does not indicate that the data was successfully delivered.

See also:
sendto(), can_write(), ioctl(), setsockopt()
Examples:
daytimed.cpp, and echod.cpp.

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.

Parameters:
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).
Return values:
[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.
Note:
For datagram sockets, care must be taken not to exceed the maximum IP packet size of the underlying subnets If the data is too long to pass atomically through the underlying protocol the error emsgsize is set on the error member, and no data is transmitted.

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.

Parameters:
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
Returns:
true if no error occurred.
See also:
setsockopt(), ioctl()

bool socklibpp::sock::shutdown ( sock::shuthow  how  )  throw ()

Disable sends and/or receives on a socket.

Parameters:
how A flag that describes what type of operation will no longer be allowed.
Returns:
true if no error occurred.
Note:
This does not close the socket, and resources attached to the descriptor will not be freed until close() is invoked.

Never reuse a socket after it has been shut down (don't call connect() or accept() on it).

See also:
close()
Examples:
daytimed.cpp.

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.

Parameters:
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
Note:
sock_stream provides sequenced, reliable, two-way, connection-based byte streams with an out-of-band data transmission mechanism. Uses TCP for the Internet address family. They are full-duplex byte streams. A stream socket must be in a connected state before any data may be sent or received on it. A connection to another socket is created with a connect() call. Once connected, data may be transferred using send() and recv() calls. When a session has been completed, a closesocket() must be performed. Out-of-band data may also be transmitted as described in send() and received as described in recv(). The communications protocols used to implement a sock_stram ensure that data is not lost or duplicated. If data for which the peer protocol has buffer space cannot be successfully transmitted within a reasonable length of time, the connection is considered broken and subsequent calls will fail with the error data member code set to etimedout.

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().

Examples:
daytimed.cpp, echod.cpp, and streamechod.cpp.

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.

Parameters:
buf The buffer with data to send.
len The length of data in buf.
flags Flags to use in the send() call.
Return values:
true All data was sent.
false The connection is closed or broken.
Note:
There's no way to know how much data was sent, and it's not useful anyways, as it only fails when the connection is lost. Non-blocking sockets will obviously block on this function.
See also:
send(), sendto()

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.

Parameters:
buf The buffer to store the data
len The length of the buffer
flags The flags to use in the recv() call.
Return values:
true All the requested data was received
false The connection was closed or broken.
Note:
There's no way to know how much data was received, and it's not useful anyways, as it only fails when the connection is lost. Non-blocking sockets will obviously block on this function.
See also:
recv(), recvfrom()

bool socklibpp::sock::block ( bool  bl  )  throw ()

Set the blocking mode.

Convenience function that sets the blocking mode of the socket.

Parameters:
bl false for enabling non-blocking mode
Returns:
true if no error ocurred.

bool socklibpp::sock::reuseaddr (  )  const throw ()

Query if it can reuse the addr.

Convenience function that retrives the so_reuseaddr option through getsockopt().

Returns:
The value of the so_reuseaddr option; if an error ocurred it returns false. The only way to detect errors is checking the error data member.

bool socklibpp::sock::reuseaddr ( bool  reuse  )  throw ()

Set the reuse addr option.

Convenience function that sets the so_reuseaddr option through setsockopt().

Parameters:
reuse Wheter the address should be reused or not.
Returns:
true if no error ocurred.

int socklibpp::sock::sendbuf (  )  const throw ()

Get the size of the internal send buffer.

Returns:
The size of the internal send() buffer.

void socklibpp::sock::sendbuf ( int  size  )  throw ()

Set the size of the internal send buffer.

Parameters:
size The desired new size of the buffer.
Note:
Avoid changing the buffer size after the socket is connected, or else some information may be lost.

int socklibpp::sock::recvbuf (  )  const throw ()

Get the size of the internal recv buffer.

Returns:
The size of the internal recv() buffer.

void socklibpp::sock::recvbuf ( int  size  )  throw ()

Set the size of the internal recv buffer.

Parameters:
size The desired new size of the buffer.
Note:
Avoid changing the buffer size after the socket is connected, or else some information may be lost.


Member Data Documentation

socket_type socklibpp::sock::fd

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 !=.

Examples:
listener.cpp.

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.

Note:
For some operations this is the only way to detect if an error ocurred. e.g. accept()
Examples:
daytimed.cpp, echod.cpp, listener.cpp, and streamechod.cpp.


The documentation for this class was generated from the following files:
Generated on Thu Jan 18 19:26:35 2007 for socklib++ by  doxygen 1.5.1