class Socket

Socket. More...

Full nameCoral::Socket
Definition#include <socket.h>
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Types

Public Methods

Public Static Methods

Public Members


Detailed Description

This class manages a socket. IPv6 support is automatically available, when supported by the system.

enum SocketCommunicationDomain {UndefinedSocketCommunicationDomain = -1, IP = 255, IPv4 = AF_INET, IPv6 = AF_INET6, Unix = AF_UNIX }

enum SocketType {UndefinedSocketType = -1, UDP = SOCK_DGRAM, Datagram = SOCK_DGRAM, TCP = SOCK_STREAM, Stream = SOCK_STREAM, Raw = SOCK_RAW, RDM = SOCK_RDM, SeqPacket = SOCK_SEQPACKET }

enum SocketProtocol {UndefinedSocketProtocol = -1, Default = 0, ICMPv4 = IPPROTO_ICMP, ICMPv6 = IPPROTO_ICMPV6 }

 Socket ()

Constructor.

 Socket (const SocketCommunicationDomain communicationDomain, const SocketType socketType, const SocketProtocol socketProtocol = Default)

Constructor for a new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary! The creation success can be checked using ready() method.

Parameters:
communicationDomainCommunication domain (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).

Socket ()

[virtual]

Destructor.

bool  create (const SocketCommunicationDomain communicationDomain = IP, const SocketType socketType = TCP, const SocketProtocol socketProtocol = Default)

Close existing socket and create new socket. For automatic usage of IPv6 when available, set communication domain to IP. Use IPv4/IPv6 only if a special protocol version is necessary!

Parameters:
communicationDomainCommunication domain (e.g. IP).
socketTypeSocket type (e.g. TCP, UDP).
socketProtocolSocket protocol (e.g. Default).

Returns: true, if creation was sucessful; false otherwise.

void  close ()

Close socket.

void  shutdown (const cardinal shutdownLevel)

Shutdown full-duplex connection partial or completely. SHUT_RD - further receives will be disallowed. SHUT_WR - further sends will be disallowed. SHUT_RDWR - further sends and receives will be disallowed.

Parameters:
shutdownLevelSHUT_RD, SHUT_WR, SHUT_RDWR.

inline card64  getBytesSent ()

[const]

Get number of bytes sent.

Returns: Number of bytes sent.

inline card64  getBytesReceived ()

[const]

Get number of bytes received.

Returns: Number of bytes received.

inline void  resetBytesSent ()

Reset number of bytes sent.

inline void  resetBytesReceived ()

Reset number of bytes received.

inline bool  ready ()

[const]

Check, if socket is ready.

Returns: true, if socket is ready; false otherwise.

bool  bind (const SocketAddress& address = InternetAddress())

Bind socket to given address. If address is null address, then INADDR_ANY and an automatically selected port will be used.

Parameters:
addressSocket address.

Returns: true on success; false otherwise.

bool  listen (const cardinal backlog = 5)

Set socket to listen mode with given backlog (queue length for sockets waiting for acception).

Parameters:
backlogBacklog.

Returns: true on success; false otherwise.

Socket*  accept ()

Accept a connection.

Returns: New socket.

bool  connect (const SocketAddress& address, const card8 trafficClass = 0)

Connect socket to given address. A value for traffic class is supported if the connection is an IPv6 connection; otherwise it is ignored.

Parameters:
addressAddress.
trafficClassTraffic class of the connection (IPv6 only!)

Returns: true on success; false otherwise.

inline integer  getSocketOption (const cardinal level, const cardinal optionNumber, void* optionValue, socklen_t* optionLength)

[const]

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory to store option got from getsockopt().
optionLengthMemory with size of option memory.

Returns: Result from getsockopt().

cardinal  getSoLinger ()

[const]

Get SO_LINGER option of socket.

Returns: SO_LINGER value.

bool  getSoReuseAddress ()

[const]

Get SO_REUSEADDR option of socket.

Returns: SO_REUSEADDR value.

bool  getSoBroadcast ()

[const]

Get SO_BROADCAST option of socket.

Returns: SO_BROADCAST value.

bool  getTCPNoDelay ()

[const]

Get TCP_NODELAY option of socket.

Returns: TCP_NODELAY value.

bool  getBlockingMode ()

Check, if blocking mode is on.

Returns: true, if blocking mode is on; false otherwise.

inline integer  setSocketOption (const cardinal level, const cardinal optionNumber, const void* optionValue, const socklen_t optionLength)

Get socket option (wrapper for getsockopt());

Parameters:
levelLevel (e.g. SOL_SOCKET).
optionNumberOption (e.g. SO_REUSEADDR).
optionValueMemory with option.
optionLengthLength of option memory.

Returns: Result from setsockopt().

void  setSoLinger (const bool on, const cardinal linger)

Set SO_LINGER option of socket.

Parameters:
ontrue to set linger on; false otherwise.
lingerSO_LINGER in milliseconds.

void  setSoReuseAddress (const bool on)

Set SO_REUSEADDR option of socket.

Parameters:
ontrue to set SO_REUSEADDR on; false otherwise.

void  setSoBroadcast (const bool on)

Set SO_BROADCAST option of socket.

Parameters:
ontrue to set SO_BROADCAST on; false otherwise.

void  setTCPNoDelay (const bool on)

Set TCP_NODELAY option of socket.

Parameters:
ontrue to set TCP_NODELAY on; false otherwise.

void  setBlockingMode (const bool on)

Set blocking mode.

Parameters:
onTrue to set blocking mode, false to unset.

inline card32  getSendFlowLabel ()

[const]

Get flow label of the connection.

Returns: Flow label of the connection or 0, if there is no flow label.

inline card8  getSendTrafficClass ()

[const]

Get traffic class of the connection.

Returns: Traffic class of the connection or 0, if there is no traffic class.

inline card32  getReceivedFlowLabel ()

[const]

Get last received flow label.

Returns: Last received flow label or 0, if there is no flow label.

inline card8  getReceivedTrafficClass ()

[const]

Get last received traffic class.

Returns: Last received traffic class or 0, if there is no traffic class.

ssize_t  sendTo (const void* buffer, const size_t length, const cardinal flags, const SocketAddress& receiver, const card8 trafficClass = 0)

[virtual]

Wrapper for sendto(). sendto() will set the packet's traffic class, if trafficClass is not 0.

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
receiverAddress of receiver.

Returns: Bytes sent or error code < 0.

ssize_t  send (const void* buffer, const size_t length, const cardinal flags = 0, const card8 trafficClass = 0)

[virtual]

Wrapper for send(). send() will set the packet's traffic class, if trafficClass is not 0. In this case, the packet will be sent by sendto() to the destination address, the socket is connected to!

Parameters:
bufferBuffer with data to send.
lengthLength of data to send.
flagsFlags for sendto().
trafficClassTraffic class for packet.

Returns: Bytes sent or error code < 0.

ssize_t  receiveFrom (void* buffer, const size_t length, SocketAddress& sender, const cardinal flags = 0)

[virtual]

Wrapper for recvfrom().

Parameters:
bufferBuffer to receive data to.
lengthMaximum length of data to be received.
senderAddress to store sender's address.
flagsFlags for recvfrom().

Returns: Bytes received or error code < 0.

ssize_t  receive (void* buffer, const size_t length, const cardinal flags = 0)

[virtual]

Wrapper for recv().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.
flagsFlags for recv().

Returns: Bytes read or error code < 0.

ssize_t  read (void* buffer, const size_t length)

[virtual]

Wrapper for read().

Parameters:
bufferBuffer to read data to.
lengthMaximum length of data to be received.

Returns: Bytes read or error code < 0.

ssize_t  write (const void* buffer, const size_t length)

[virtual]

Wrapper for write().

Parameters:
bufferBuffer with data to write
lengthLength of data to write

Returns: Bytes sent or error code < 0.

inline integer  fcntl (const integer cmd, const long arg = 0)

Wrapper for fcntl().

Parameters:
cmdCommand.
argArgument.

Returns: Result of fcntl() call.

inline integer  ioctl (const integer request, const void* argp)

Wrapper for ioctl().

Parameters:
requestRequest.
argpArgument.

Returns: Result of ioctl() call.

bool  getSocketAddress (SocketAddress& address)

[const]

Get the socket's address. Note: A socket has to be bound to an address and port or connected to a peer first to let the socket have an address!

Parameters:
addressReference to SocketAddress to write address to.

Returns: true, if call was successful; false otherwise.

See also: bind, connect, getPeerAddress

bool  getPeerAddress (SocketAddress& address)

[const]

Get the peer's address. Note: A socket has to be connected to a peer first to get a peer address!

Parameters:
addressReference to SocketAddress to write address to.

Returns: true, if call was successful; false otherwise.

See also: bind, connect, getSocketAddress

InternetFlow  allocFlow (const InternetAddress& address, const card32 flowLabel = 0, const card8 shareLevel = IPV6_FL_S_PROCESS)

Allocate a new flow to a given destination. A InternetFlow object is returned, the value flow.getFlowLabel() will not be 0, if the allocFlow() call was successful.

Parameters:
addressAddress of the destination.
flowLabelFlowlabel; 0 for random value.
shareLevelShare level for flow label.

Returns: InternetFlow.

void  freeFlow (InternetFlow& flow)

Free a flow.

Parameters:
flowFlow to be freed.

bool  renewFlow (InternetFlow& flow, const cardinal expires, const cardinal linger = 6)

Renew a flow label allocation with given expires and linger (default 6) values. The expires value gives the seconds to go until the flow label expires, the linger value gives the timeout in seconds the freed flow label cannot be allocated again.

Parameters:
flowFlow to be renewed.
expiresSeconds until the flow label expires.
lingerLinger (default 6).

Returns: true on success; false otherwise.

bool  renewFlow (const cardinal expires, const cardinal linger = 6)

Renew current flow's flow label allocation with given expires and linger (default 6) values.

Parameters:
expiresSeconds until the flow label expires.
lingerLinger (default 6).

Returns: true on success; false otherwise.

bool  bindInternetSocketPair (Socket& senderSocket, Socket& receiverSocket, const InternetAddress& receiver = InternetAddress())

[static]

Bind a pair of internet sockets to a given address and port number x and x + 1. x will be a random number, if given port number is 0.

Parameters:
senderSocketFirst socket.
receiverSocketSecond socket.
receiverAddress (e.g ipv6-localhost:0) or NULL for Any address.

void  setTrafficConstraint (const cardinal bytesPerSecond, const card64 maxBufferDelay)

[virtual]

Set traffic constraint to given byte rate. Packets exceeding the given constraint will be dropped. Note: This functionality has to be implemented by subclasses!

Parameters:
bytesPerSecondBytes per second.
maxBufferDelayMaximum buffer delay in microseconds.

void  flush ()

[virtual]

Flush traffic shaper buffer. Note: This functionality has to be implemented by subclasses!

inline int  getSystemSocketDescriptor ()

[const]

Get system's socket descriptor. Warning: It is not recommended to manipulate the socket directly. Use Socket's methods instead.

Returns: Socket descriptor.

static const cardinal MinAutoSelectPort

Minimum port number for bind()'s automatic port selection.

static const cardinal MaxAutoSelectPort

Maximum port number for bind()'s automatic port selection.