Base Framework
Public Types | Public Member Functions | Static Public Member Functions | Protected Types | Protected Member Functions | List of all members
Socket Class Reference

Socket. More...

#include <base/net/Socket.h>

Inherits Resource, AsynchronousInputStream, and AsynchronousOutputStream.

Inherited by ServerSocket [protected], and StreamSocket.

Public Types

enum  Domain { IPV4, IPV6, DEFAULT_DOMAIN = IPV4 }
 
enum  Kind { STREAM, DATAGRAM, RAW }
 

Public Member Functions

 Socket () noexcept
 
Domain getDomain () const
 
Kind getKind () const
 
bool accept (Socket &socket)
 
void bind (const InetAddress &address, unsigned short port)
 
void bind (const InetEndPoint &endPoint)
 
void close ()
 
void connect (const InetAddress &address, unsigned short port)
 
void connect (const InetEndPoint &endPoint)
 
void create (Kind kind, Domain domain=DEFAULT_DOMAIN)
 
void getName () noexcept
 
const InetAddressgetAddress () const noexcept
 
unsigned short getPort () const noexcept
 
InetEndPoint getEndPoint () const noexcept
 
const InetAddressgetLocalAddress () const noexcept
 
unsigned short getLocalPort () const noexcept
 
InetEndPoint getLocalEndPoint () const noexcept
 
void listen (unsigned int backlog)
 
void shutdownInputStream ()
 
void shutdownOutputStream ()
 
int getErrorState () const
 
bool getReuseAddress () const
 
void setReuseAddress (bool value)
 
bool getKeepAlive () const
 
void setKeepAlive (bool value)
 
bool getBroadcast () const
 
void setBroadcast (bool value)
 
int getLinger () const
 
void setLinger (int seconds)
 
int getReceiveBufferSize () const
 
void setReceiveBufferSize (int size)
 
int getSendBufferSize () const
 
void setSendBufferSize (int size)
 
bool getDontRoute () const
 
void setDontRoute (bool value)
 
uint64 getReceiveTimeout () const
 
void setReceiveTimeout (uint64 nanoseconds)
 
uint64 getSendTimeout () const
 
void setSendTimeout (uint64 nanoseconds)
 
bool getTcpNoDelay () const
 
void setTcpNoDelay (bool value)
 
uint64 getTcpDeferAccept () const
 
void setTcpDeferAccept (uint64 nanoseconds)
 
unsigned int getTimeToLive () const
 
void setTimeToLive (unsigned int value)
 
uint8 getMulticastHops () const
 
void setMulticastHops (uint8 value)
 
bool getMulticastLoopback () const
 
void setMulticastLoopback (bool value)
 
InetAddress getMulticastInterface () const
 
void setMulticastInterface (const InetAddress &interface)
 
uint8 getUnicastHops () const
 
void setUnicastHops (uint8 value)
 
void joinGroup (const InetAddress &group)
 
void joinGroup (const InetAddress &interface, const InetAddress &group)
 
void leaveGroup (const InetAddress &interface, const InetAddress &group)
 
bool getIPv6Restriction () const
 
void setIPv6Restriction (bool value)
 
void setNonBlocking (bool value)
 
unsigned int available () const
 
void flush ()
 
unsigned int read (uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int write (const uint8 *buffer, unsigned int size, bool nonblocking=false)
 
unsigned int sendTo (const uint8 *buffer, unsigned int size, const InetAddress &address, unsigned short port)
 
unsigned int receiveFrom (uint8 *buffer, unsigned int size, InetAddress &address, unsigned short &port)
 
void asyncCancel ()
 
AsynchronousReadOperation read (uint8 *buffer, unsigned int bytesToRead, AsynchronousReadEventListener *listener)
 
AsynchronousWriteOperation write (const uint8 *buffer, unsigned int bytesToWrite, AsynchronousWriteEventListener *listener)
 
void wait () const
 
bool wait (unsigned int microseconds) const
 
 ~Socket ()
 
- Public Member Functions inherited from Resource
String getDescription () const
 
void setDescription (const String &description)
 
unsigned int getResourceId () const
 
unsigned int getCreatedById () const
 
AnyReference getHandle () const noexcept
 
template<class TYPE >
TYPE & getInternalHandle () const
 
 operator bool () const noexcept
 
- Public Member Functions inherited from AsynchronousStream
virtual ~AsynchronousStream () noexcept(false)
 

Static Public Member Functions

static const char * toString (Domain domain) noexcept
 
static const char * toString (Kind kind) noexcept
 
- Static Public Member Functions inherited from AsynchronousStream
static bool asyncTest () noexcept
 

Protected Types

enum  { OPTION_DONT_ROUTE, OPTION_TCP_NO_DELAY, OPTION_TCP_DEFER_ACCEPT }
 

Protected Member Functions

OperatingSystem::Handle getHandle () const noexcept
 
bool getBooleanOption (int option) const
 
void setBooleanOption (int option, bool value)
 

Additional Inherited Members

- Protected Attributes inherited from Resource
AnyReference handle
 

Detailed Description

Socket.

This class implements a socket. A socket is an endpoint for communication between two hosts on a network. MT-level is safe.

See also
StreamSocket ServerSocket
Version
1.2

Member Enumeration Documentation

◆ anonymous enum

anonymous enum
protected

Socket options.

◆ Domain

Domain.

Enumerator
IPV4 

IP version 4.

IPV6 

IP version 6.

DEFAULT_DOMAIN 

The default domain.

◆ Kind

Socket type.

Enumerator
STREAM 

Stream socket.

DATAGRAM 

Datagram socket.

RAW 

Raw socket.

Constructor & Destructor Documentation

◆ Socket()

Socket::Socket ( )
noexcept

Initializes an invalidated socket object (ie. unconnected and unbound).

◆ ~Socket()

Socket::~Socket ( )

Releases the socket.

Member Function Documentation

◆ accept()

bool Socket::accept ( Socket socket)

Accepts the first connection from the queue of pending connections on the specified socket. This function is used with a connection-oriented socket.

Parameters
socketSpecifies the socket to accept a connection from.
Returns
True if connection has been accepted. False, if connection could not be accepted without blocking.
Examples
testsuite/echod.cpp.

◆ available()

unsigned int Socket::available ( ) const

Returns the number of bytes that can be read or skipped over without blocking.

Returns
Available number of bytes in stream.

◆ bind() [1/2]

void Socket::bind ( const InetAddress address,
unsigned short  port 
)

Associates a local name (address and port) with this socket.

Parameters
addressThe IP address the socket should be bound to. If unspecified the assigned address may not be known until the socket has been connected with connect or accept.
portThe port the socket should be bound to. If zero the socket is assigned to a unique port.

◆ bind() [2/2]

void Socket::bind ( const InetEndPoint endPoint)
inline

Associates a local name (address and port) with this socket.

Parameters
endPointThe end point.

◆ close()

void Socket::close ( )

Closes this socket.

◆ connect() [1/2]

void Socket::connect ( const InetAddress address,
unsigned short  port 
)

Connects this socket to the specified address and port.

Parameters
addressThe IP address to connect to.
portThe port to connect to.

◆ connect() [2/2]

void Socket::connect ( const InetEndPoint endPoint)
inline

Connects this socket to the specified address and port.

Parameters
endPointThe end point.

◆ create()

void Socket::create ( Kind  kind,
Domain  domain = DEFAULT_DOMAIN 
)

Creates either a stream or a datagram socket.

Parameters
kindThe socket type (e.g. STREAM).
domainThe domain (the default is DEFAULT_DOMAIN).

◆ flush()

void Socket::flush ( )

Forces any buffered bytes to be written out.

◆ getAddress()

const InetAddress& Socket::getAddress ( ) const
noexcept

Returns the IP address to which the socket is connected.

◆ getBooleanOption()

bool Socket::getBooleanOption ( int  option) const
protected

Get boolean socket option.

◆ getBroadcast()

bool Socket::getBroadcast ( ) const

Returns true if broadcast datagrams allowed on this socket.

◆ getDomain()

Domain Socket::getDomain ( ) const

Returns the domain.

◆ getDontRoute()

bool Socket::getDontRoute ( ) const

Returns true if the standard routing facilities have been disabled.

◆ getEndPoint()

InetEndPoint Socket::getEndPoint ( ) const
inlinenoexcept

Returns the connected end point.

◆ getErrorState()

int Socket::getErrorState ( ) const

Returns the error state of the socket.

◆ getHandle()

OperatingSystem::Handle Socket::getHandle ( ) const
protectednoexcept

Returns the handle of the socket.

◆ getIPv6Restriction()

bool Socket::getIPv6Restriction ( ) const

Returns the IPv6 packet restriction flag.

◆ getKeepAlive()

bool Socket::getKeepAlive ( ) const

Returns true if connection is kept alive.

◆ getKind()

Kind Socket::getKind ( ) const

Returns the kind.

◆ getLinger()

int Socket::getLinger ( ) const

Gets the linger interval.

Returns
-1 if linger is disabled.

◆ getLocalAddress()

const InetAddress& Socket::getLocalAddress ( ) const
noexcept

Returns the local IP address to which the socket is bound.

◆ getLocalEndPoint()

InetEndPoint Socket::getLocalEndPoint ( ) const
inlinenoexcept

Returns the local end point.

◆ getLocalPort()

unsigned short Socket::getLocalPort ( ) const
noexcept

Returns the local port to which the socket is bound.

◆ getMulticastHops()

uint8 Socket::getMulticastHops ( ) const

Returns the maximum number of multicast hops (time to live).

◆ getMulticastInterface()

InetAddress Socket::getMulticastInterface ( ) const

Returns the default interface for outgoing multicast packets.

◆ getMulticastLoopback()

bool Socket::getMulticastLoopback ( ) const

Returns the multicast loop-back value.

◆ getName()

void Socket::getName ( )
noexcept

Caches the locally assigned address and port of the socket. This member function can be used after a succesful accept or connect to determine the locally assigned address and port if unspecified.

◆ getPort()

unsigned short Socket::getPort ( ) const
noexcept

Returns the remote port to which the socket is connected.

◆ getReceiveBufferSize()

int Socket::getReceiveBufferSize ( ) const

Gets the size of the receive buffer.

◆ getReceiveTimeout()

uint64 Socket::getReceiveTimeout ( ) const

Returns the timeout period in nanoseconds for receive.

◆ getReuseAddress()

bool Socket::getReuseAddress ( ) const

Returns true if 'bind' allows local addresses to be reused.

◆ getSendBufferSize()

int Socket::getSendBufferSize ( ) const

Gets the size of the send buffer.

◆ getSendTimeout()

uint64 Socket::getSendTimeout ( ) const

Returns the timeout period in nanoseconds for send.

◆ getTcpDeferAccept()

uint64 Socket::getTcpDeferAccept ( ) const

Returns the timeout period in nanoseconds for send.

◆ getTcpNoDelay()

bool Socket::getTcpNoDelay ( ) const

Returns true if the Nagle's algorithm is disabled.

◆ getTimeToLive()

unsigned int Socket::getTimeToLive ( ) const

Returns the current time to live (TTL) value.

◆ getUnicastHops()

uint8 Socket::getUnicastHops ( ) const

Returns the maximum number of unicast hops (time to live).

◆ joinGroup() [1/2]

void Socket::joinGroup ( const InetAddress group)

Joins the specified multicast group on the default interface.

◆ joinGroup() [2/2]

void Socket::joinGroup ( const InetAddress interface,
const InetAddress group 
)

Joins the specified multicast group on the specified interface.

◆ leaveGroup()

void Socket::leaveGroup ( const InetAddress interface,
const InetAddress group 
)

Leaves the specified multicast group of the specified interface.

◆ listen()

void Socket::listen ( unsigned int  backlog)

This function places the socket in a state where it is listening for incoming connections. It also sets the maximum length of the queue of pending connections. The backlog argument may be silently reduced. The socket must be bound and unconnected.

Parameters
backlogThe maxium length of the queue.

◆ read() [1/2]

AsynchronousReadOperation Socket::read ( uint8 *  buffer,
unsigned int  bytesToRead,
AsynchronousReadEventListener listener 
)
virtual

Requests and asynchronous read operation.

Parameters
bufferThe bytes to be read.
bytesToReadThe number of bytes to be read.
listenerThe listener to be notified on completion.

Implements AsynchronousInputStream.

◆ read() [2/2]

unsigned int Socket::read ( uint8 *  buffer,
unsigned int  size,
bool  nonblocking = false 
)

Fills the buffer with bytes from the socket input stream. Blocks if asked to read more bytes than available. Raises EndOfFile if end of stream has been reached.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
nonblockingSelect nonblocking mode.
Returns
The actual number of bytes read.

◆ receiveFrom()

unsigned int Socket::receiveFrom ( uint8 *  buffer,
unsigned int  size,
InetAddress address,
unsigned short &  port 
)

Receives data from any address using an unconnected socket.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
addressThe address.
portThe port.
Returns
The number of bytes received.

◆ sendTo()

unsigned int Socket::sendTo ( const uint8 *  buffer,
unsigned int  size,
const InetAddress address,
unsigned short  port 
)

Sends the contents of the buffer to the specified address using an unconnected socket.

Parameters
bufferThe buffer.
sizeThe size of the buffer.
addressThe Internet address of the remote host.
portThe port of the remote host.
Returns
The number of bytes sent.

◆ setBooleanOption()

void Socket::setBooleanOption ( int  option,
bool  value 
)
protected

Set boolean socket option.

◆ setBroadcast()

void Socket::setBroadcast ( bool  value)

Sets the broadcast datagram flag of this socket.

◆ setDontRoute()

void Socket::setDontRoute ( bool  value)

Disables/enables the standard routing facilities.

◆ setIPv6Restriction()

void Socket::setIPv6Restriction ( bool  value)

Sets the IPv6 packet restriction flag.

◆ setKeepAlive()

void Socket::setKeepAlive ( bool  value)

Sets the keep alive flag of this socket.

◆ setLinger()

void Socket::setLinger ( int  seconds)

Sets the linger interval. Negative time disables the linger.

◆ setMulticastHops()

void Socket::setMulticastHops ( uint8  value)

Sets the maximum number of multicast hops (time to live).

◆ setMulticastInterface()

void Socket::setMulticastInterface ( const InetAddress interface)

Sets the default interface for outgoing multicast packets.

◆ setMulticastLoopback()

void Socket::setMulticastLoopback ( bool  value)

Sets the multicast loop-back value.

◆ setNonBlocking()

void Socket::setNonBlocking ( bool  value)

Sets the blocking mode of the socket.

◆ setReceiveBufferSize()

void Socket::setReceiveBufferSize ( int  size)

Sets the size of the receive buffer.

◆ setReceiveTimeout()

void Socket::setReceiveTimeout ( uint64  nanoseconds)

Sets the timeout period for receive (clamped to one day).

◆ setReuseAddress()

void Socket::setReuseAddress ( bool  value)

Sets the local address reuse flag of this socket.

◆ setSendBufferSize()

void Socket::setSendBufferSize ( int  size)

Sets the size of the send buffer.

◆ setSendTimeout()

void Socket::setSendTimeout ( uint64  nanoseconds)

Sets the timeout period for send (clamped to one day).

◆ setTcpDeferAccept()

void Socket::setTcpDeferAccept ( uint64  nanoseconds)

Sets the timeout period for send (clamped to one day).

◆ setTcpNoDelay()

void Socket::setTcpNoDelay ( bool  value)

Disables/enables the Nagle's algorithm.

◆ setTimeToLive()

void Socket::setTimeToLive ( unsigned int  value)

Sets the time to live (TTL) value.

◆ setUnicastHops()

void Socket::setUnicastHops ( uint8  value)

Sets the maximum number of unicast hops (time to live).

◆ shutdownInputStream()

void Socket::shutdownInputStream ( )

Disables the input stream for this socket.

◆ shutdownOutputStream()

void Socket::shutdownOutputStream ( )

Disables the output stream for this socket.

◆ wait() [1/2]

void Socket::wait ( ) const

Blocking wait for input to become available.

◆ wait() [2/2]

bool Socket::wait ( unsigned int  microseconds) const

Blocking wait for input to become available.

Parameters
microsecondsThe timeout periode in microseconds.
Returns
True, if data is available. False, if the timeout periode expired.

◆ write() [1/2]

AsynchronousWriteOperation Socket::write ( const uint8 *  buffer,
unsigned int  bytesToWrite,
AsynchronousWriteEventListener listener 
)
virtual

Requests and asynchronous write operation.

Parameters
bufferThe bytes to be read.
bytesToWriteThe number of bytes to be written.
listenerThe listener to be notified on completion.

Implements AsynchronousOutputStream.

◆ write() [2/2]

unsigned int Socket::write ( const uint8 *  buffer,
unsigned int  size,
bool  nonblocking = false 
)

Writes bytes in buffer to stream.

Parameters
bufferThe buffer containing the bytes to be written.
sizeThe number of bytes to be written.
nonblockingSelect nonblocking mode.
Returns
The actual number of bytes written.