#include <netlink/socket.h>
|
| 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 () |
|
Socket * | accept () |
|
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) |
|
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.
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
-
hostTo | the target/remote host |
portTo | the target/remote port |
protocol | the protocol to be used (TCP or UDP). TCP by default. |
ipVer | the IP version to be used (IP4, IP6 or ANY). ANY by default. |
- Exceptions
-
Exception | BAD_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
-
portFrom | the local port the socket will be bound to |
protocol | the protocol to be used (TCP or UDP). TCP by default. |
ipVer | the IP version to be used (IP4, IP6 or ANY). IP4 by default. |
hostFrom | the local address to be binded to (example: "localhost" or "127.0.0.1"). Empty (by default) or "*" means all avariable addresses. |
listenQueue | the size of the internal buffer of the SERVER TCP socket where the connection requests are stored until accepted |
- Exceptions
-
Exception | BAD_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
-
hostTo | the target/remote host |
portTo | the target/remote port |
portFrom | the local port the socket will be bound to |
ipVer | the IP version to be used (IP4, IP6 or ANY). ANY by default. |
- Exceptions
-
Exception | BAD_PROTOCOL, BAD_IP_VER, ERROR_SET_ADDR_INFO*, ERROR_SET_SOCK_OPT*, ERROR_CAN_NOT_LISTEN*, ERROR_CONNECT_SOCKET* |
Socket Destructor
Closes (disconnects) the socket
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
-
Exception | EXPECTED_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
-
blocking | true to set the Socket as blocking; false to set the Socket as non-blocking |
- Exceptions
-
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
-
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.
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
-
buffer | A pointer to a buffer where received data will be stored |
bufferSize | Size of the buffer |
- Returns
- Size of received data or (-1) if Socket is non-blocking and there's no data received.
- Exceptions
-
- 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
-
| buffer | Pointer to a buffer where received data will be stored |
| bufferSize | Size of the buffer |
[out] | hostFrom | Here the function will store the address of the remote host |
[out] | portFrom | Here the function will store the remote port |
- Returns
- the length of the data recieved
- Exceptions
-
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
-
buffer | A pointer to the data we want to send |
size | Length of the data to be sent (bytes) |
- Exceptions
-
- 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
-
buffer | A pointer to the data we want to send |
size | Size of the data to send (bytes) |
hostTo | Target/remote host |
portTo | Target/remote port |
- Exceptions
-
Exception | EXPECTED_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
Returns the socket type
- Returns
- SocketType (SERVER or CLIENT)
The documentation for this class was generated from the following files: