The sockets library can be used to easily create socket connections in an efficient way
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:
do {
std::string message;
message += " RESPONSE!";
delete s;
});
And the client side of this example looks like this:
socket.
connect(
"127.0.0.1", 12345, 2000);
std::string message = "REQUEST?";
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:
do {
std::string message;
std::string ip;
uint16_t port;
message += " RESPONSE!";
s->
writePacket(ip, port, message.c_str(), message.length());
And the client side of this example looks like this:
std::string message = "REQUEST?";
socket.
writePacket(
"127.0.0.1", 12345, message.c_str(), message.length());
std::string ip;
uint16_t port;