NetLink Sockets C++  1.0.0
Networking C++ Library
 All Classes Namespaces Files Functions Enumerations Enumerator Pages
Public Member Functions | List of all members
NL::Socket Class Reference

#include <netlink/socket.h>

Public Member Functions

 Socket (const string &hostTo, unsigned portTo, Protocol protocol=TCP, IPVer ipVer=ANY)
 
 Socket (unsigned portFrom, Protocol protocol=TCP, IPVer ipVer=IP4, const string &hostFrom="", unsigned listenQueue=DEFAULT_LISTEN_QUEUE)
 
 Socket (const string &hostTo, unsigned portTo, unsigned portFrom, IPVer ipVer=ANY)
 
 ~Socket ()
 
Socketaccept ()
 
int read (void *buffer, size_t bufferSize)
 
void send (const void *buffer, size_t size)
 
int readFrom (void *buffer, size_t bufferSize, string *HostFrom, unsigned *portFrom=NULL)
 
void sendTo (const void *buffer, size_t size, const string &hostTo, unsigned portTo)
 
int nextReadSize () const
 
void disconnect ()
 
const string & hostTo () const
 
const string & hostFrom () const
 
unsigned portTo () const
 
unsigned portFrom () const
 
Protocol protocol () const
 
IPVer ipVer () const
 
SocketType type () const
 
bool blocking () const
 
unsigned listenQueue () const
 
int socketHandler () const
 
void blocking (bool blocking)
 

Detailed Description

Socket class

Note
The Exceptions with asterisk(*) includes the native error code (Exception::nativeErrorCode()) when thrown
Warning
Remember to call init() before using Socket
Examples:
chatClient.cc, chatServer.cc, clientEcho.cc, serverEcho.cc, udpDirectChat.cc, and webGet.cc.

Constructor & Destructor Documentation

NL::Socket::Socket ( const string &  hostTo,
unsigned  portTo,
Protocol  protocol = TCP,
IPVer  ipVer = ANY 
)

CLIENT Socket constructor

Creates a socket, connect it (if TCP) and sets it ready to send to hostTo:portTo. The local port of the socket is choosen by OS.

Parameters
hostTothe target/remote host
portTothe target/remote port
protocolthe protocol to be used (TCP or UDP). TCP by default.
ipVerthe IP version to be used (IP4, IP6 or ANY). ANY by default.
Exceptions
ExceptionBAD_PROTOCOL, BAD_IP_VER, ERROR_SET_ADDR_INFO*, ERROR_CONNECT_SOCKET*, ERROR_GET_ADDR_INFO*
NL::Socket::Socket ( unsigned  portFrom,
Protocol  protocol = TCP,
IPVer  ipVer = IP4,
const string &  hostFrom = "",
unsigned  listenQueue = DEFAULT_LISTEN_QUEUE 
)

SERVER Socket constructor

Creates a socket, binds it to portFrom port and listens for connections (if TCP).

Parameters
portFromthe local port the socket will be bound to
protocolthe protocol to be used (TCP or UDP). TCP by default.
ipVerthe IP version to be used (IP4, IP6 or ANY). IP4 by default.
hostFromthe local address to be binded to (example: "localhost" or "127.0.0.1"). Empty (by default) or "*" means all avariable addresses.
listenQueuethe size of the internal buffer of the SERVER TCP socket where the connection requests are stored until accepted
Exceptions
ExceptionBAD_PROTOCOL, BAD_IP_VER, ERROR_SET_ADDR_INFO*, ERROR_SET_SOCK_OPT*, ERROR_CAN_NOT_LISTEN*, ERROR_CONNECT_SOCKET*
NL::Socket::Socket ( const string &  hostTo,
unsigned  portTo,
unsigned  portFrom,
IPVer  ipVer = ANY 
)

UDP CLIENT Socket Constructor

This client constructor for UDP Sockets allows to expecify the local port the socket will be bound to. It sets the socket ready to send data to hostTo:portTo.

Parameters
hostTothe target/remote host
portTothe target/remote port
portFromthe local port the socket will be bound to
ipVerthe IP version to be used (IP4, IP6 or ANY). ANY by default.
Exceptions
ExceptionBAD_PROTOCOL, BAD_IP_VER, ERROR_SET_ADDR_INFO*, ERROR_SET_SOCK_OPT*, ERROR_CAN_NOT_LISTEN*, ERROR_CONNECT_SOCKET*
NL::Socket::~Socket ( )

Socket Destructor

Closes (disconnects) the socket

Member Function Documentation

Socket * NL::Socket::accept ( )

Accepts a new incoming connection (SERVER Socket).

Creates a new CLIENT socket to handle the communication (send/recieve data) of this accepted connection. Requires the socket to be a SERVER TCP socket. Throws an exception otherwise.

Precondition
Socket must be SERVER
Returns
A CLIENT socket that handles the new connection
Exceptions
ExceptionEXPECTED_TCP_SOCKET, EXPECTED_SERVER_SOCKET
Examples:
chatServer.cc, and serverEcho.cc.
bool NL::Socket::blocking ( ) const
inline

Returns whether the socket is blocking (true) or not (false)

Returns
socket blocking status
void NL::Socket::blocking ( bool  blocking)

Sets the blocking nature of the Socket

Sets the Socket as blocking (if blocking is true) or as non-blocking (otherwise)

Parameters
blockingtrue to set the Socket as blocking; false to set the Socket as non-blocking
Exceptions
ExceptionERROR_IOCTL*
void NL::Socket::disconnect ( )

Closes (disconnects) the socket. After this call the socket can not be used.

Warning
Any use of the Socket after disconnection leads to undefined behaviour.
const string & NL::Socket::hostFrom ( ) const
inline

Returns the socket local address

Returns
socket local address
const string & NL::Socket::hostTo ( ) const
inline

Returns the target host of the socket

Returns
the host this socket is connected to (in TCP case) or the host it sends data to (UDP case)
Examples:
chatServer.cc.
IPVer NL::Socket::ipVer ( ) const
inline

Returns the socket IP version

Returns
IPVer
unsigned NL::Socket::listenQueue ( ) const
inline

Returns the size of the listen queue.

Precondition
The Socket must be SERVER, otherwise this call has no sense and returns 0
Returns
the size of the internal buffer of the SERVER TCP socket where the connection requests are stored until accepted
int NL::Socket::nextReadSize ( ) const

Get next read() data size

Get the size of the data (bytes) a call to read() or readFrom() can process

Returns
size of data the next call to read/readFrom will receive
Exceptions
ExceptionERROR_IOCTL*
unsigned NL::Socket::portFrom ( ) const
inline

Returns the local port of the socket

Returns
local port
unsigned NL::Socket::portTo ( ) const
inline

Returns the port this socket is connected/sends to

Returns
target/remote port
Examples:
chatServer.cc.
Protocol NL::Socket::protocol ( ) const
inline

Returns the socket protocol

Returns
Protocol
int NL::Socket::read ( void *  buffer,
size_t  bufferSize 
)

Receives data

Receives data and stores it in buffer until bufferSize reached.

Parameters
bufferA pointer to a buffer where received data will be stored
bufferSizeSize of the buffer
Returns
Size of received data or (-1) if Socket is non-blocking and there's no data received.
Exceptions
ExceptionERROR_READ*
Examples:
chatClient.cc, chatServer.cc, and serverEcho.cc.
int NL::Socket::readFrom ( void *  buffer,
size_t  bufferSize,
string *  hostFrom,
unsigned *  portFrom = NULL 
)

Receive data and get the source host and port

Requires the socket to be UDP. Source host address and port are returned in hostFrom and portFrom parameters. Data recieved is written in buffer address up to bufferSize.

Precondition
Socket must be UDP
Parameters
bufferPointer to a buffer where received data will be stored
bufferSizeSize of the buffer
[out]hostFromHere the function will store the address of the remote host
[out]portFromHere the function will store the remote port
Returns
the length of the data recieved
Exceptions
ExceptionEXPECTED_UDP_SOCKET, ERROR_READ*
void NL::Socket::send ( const void *  buffer,
size_t  size 
)

Sends data

Sends the data contained in buffer. Requires the Socket to be a CLIENT socket.

Precondition
Socket must be CLIENT
Parameters
bufferA pointer to the data we want to send
sizeLength of the data to be sent (bytes)
Exceptions
ExceptionEXPECTED_CLIENT_SOCKET, ERROR_SEND*
Examples:
chatServer.cc, and clientEcho.cc.
void NL::Socket::sendTo ( const void *  buffer,
size_t  size,
const string &  hostTo,
unsigned  portTo 
)

Sends data to an expecific host:port

Sends the data contained in buffer to a given host:port. Requires the socket to be an UDP socket, throws an exception otherwise.

Precondition
Socket must be UDP
Parameters
bufferA pointer to the data we want to send
sizeSize of the data to send (bytes)
hostToTarget/remote host
portToTarget/remote port
Exceptions
ExceptionEXPECTED_UDP_SOCKET, BAD_IP_VER, ERROR_SET_ADDR_INFO*, ERROR_SEND*
int NL::Socket::socketHandler ( ) const
inline

Returns the socket handler (file/socket descriptor)

Returns
File descritor to handle the socket
SocketType NL::Socket::type ( ) const
inline

Returns the socket type

Returns
SocketType (SERVER or CLIENT)

The documentation for this class was generated from the following files: