clockUtils  1.1
Sockets

Namespaces

 clockUtils
 
 clockUtils::sockets
 
 std
 

Macros

#define CLOCK_SOCKETS_API
 

Typedefs

typedef uint32_t clockUtils::sockets::IPv4
 
typedef int32_t socklen_t
 
typedef int32_t socklen_t
 

Functions

CLOCK_SOCKETS_API IPv4 clockUtils::sockets::resolveHostname (const std::string &hn)
 returns the IP for a given hostname More...
 
CLOCK_SOCKETS_API IPv4 clockUtils::sockets::convertIP (const std::string &ip)
 converts an IP in the numbers-and-dots notation into an IPv4 integer More...
 
CLOCK_SOCKETS_API std::string clockUtils::sockets::convertIP (const IPv4 &ip)
 converts an IPv4 formatted IP to the numbers-and-dots notation More...
 

Variables

const IPv4 clockUtils::sockets::NO_IP
 

Detailed Description

The sockets library can be used to easily create socket connections in an efficient way

How to use the sockets library

Sockets are used to communicate through networks like a local network or the internet. There are two ways you can do this using this library.

TCP sockets

You need to include this header

To connect two TcpSockets you need two parts. The first part is a host which is waiting for connections, mostly a server. In this example you will see a simple echo server just returning all messages he got sent adding the string " RESPONSE!". A server using TcpSockets can look like this:

socket.listen(12345, 10, true, [](clockUtils::sockets::TcpSocket * s) {
do {
std::string message;
err = s->receivePacket(message);
message += " RESPONSE!";
s->writePacket(message.c_str(), message.length());
delete s;
});

And the client side of this example looks like this:

socket.connect("127.0.0.1", 12345, 2000);
std::string message = "REQUEST?";
socket.writePacket(message.c_str(), message.length());
socket.receivePacket(message);

Every method of TcpSocket will return ClockError::SUCCESS on success or otherwise an error code. The message received in client will now like like "REQUEST? RESPONSE!".

UDP sockets

You need to include this header

A UdpSocket works a bit different than the TcpSocket because it doesn't create a connection between to sockets. But the API is mostly the same. Here is the same example as for the TcpSocket:

socket.bind(12345);
do {
std::string message;
std::string ip;
uint16_t port;
err = s->receivePacket(message, ip, port);
message += " RESPONSE!";
s->writePacket(ip, port, message.c_str(), message.length());

And the client side of this example looks like this:

socket.bind(12346);
std::string message = "REQUEST?";
socket.writePacket("127.0.0.1", 12345, message.c_str(), message.length());
std::string ip;
uint16_t port;
socket.receivePacket(message, ip, port);

Macro Definition Documentation

§ CLOCK_SOCKETS_API

#define CLOCK_SOCKETS_API

Definition at line 40 of file socketsParameters.h.

Typedef Documentation

§ IPv4

typedef uint32_t clockUtils::sockets::IPv4

Definition at line 42 of file Commons.h.

§ socklen_t [1/2]

typedef int32_t socklen_t

Definition at line 45 of file UdpSocket.h.

§ socklen_t [2/2]

typedef int32_t socklen_t

Definition at line 45 of file TcpSocket.h.

Function Documentation

§ convertIP() [1/2]

CLOCK_SOCKETS_API IPv4 clockUtils::sockets::convertIP ( const std::string &  ip)

converts an IP in the numbers-and-dots notation into an IPv4 integer

Parameters
[in]ipto be converted
Returns
the IP or NO_IP if ip was not well-formed
Note
Because of the underlaying function, the ip "255.255.255.255" equals NO_IP, thus converting this ip will appear like an error
Here is the caller graph for this function:

§ convertIP() [2/2]

CLOCK_SOCKETS_API std::string clockUtils::sockets::convertIP ( const IPv4 ip)

converts an IPv4 formatted IP to the numbers-and-dots notation

Parameters
[in]ipIPv4 address
Returns
the readable version as a string

§ resolveHostname()

CLOCK_SOCKETS_API IPv4 clockUtils::sockets::resolveHostname ( const std::string &  hn)

returns the IP for a given hostname

Parameters
[in]hnHostname to resolve
Returns
the IP or NO_IP upon failure
Here is the caller graph for this function:

Variable Documentation

§ NO_IP

const IPv4 clockUtils::sockets::NO_IP

Definition at line 44 of file Commons.h.