Socket

API reference

Usage example

#include <rili/service/Socket.hpp>
#include <rili/Stream.hpp>
#include <string>

void createServerConnectToItAcceptConnectionSendAndReceiveSomeData(){
  auto& socket = rili::service::Socket::instance();
  auto acceptor = socket.createAcceptor("127.0.0.1", 12345); // here we create "server"
  auto connection1 = socket.createConnection("127.0.0.1", 12345); // here we create "client connection" to previousely created server
  auto connection2 = acceptor->accept(); // here we accept incoming connection and create "server side" "connection" endpoint

  *connection1 << "something " << rili::stream::flush; // sending from "client" some data to "server"
  *connection2 << "else " << rili::stream::flush; // sending from "server" some data to "client"

  std::string resp1;
  std::string resp2;

  *connection1 >> resp1; // here we receive some data on "client" from "server"
  *connection2 >> resp2; // here we receive some data on "server" from "client"

  rili::stream::cout() << "Server received: " << resp2 << rili::stream::endl; // should receive "someething"
  rili::stream::cout() << "Client received: " << resp1 << rili::stream::endl; // should receive "else"
} // here acceptor, connection1 and connection2 are destroyed releaseing all resources including closing underlying sockets
Note:

Error handling and advenced usage of socket API are missing in this example to keep things as simple as possible