ASL
SocketServer Class Reference

Detailed Description

This is a reusable TCP or Unix socket server that listens to incoming connections and answers them concurrently (default) or sequentially.

To make an actual server, derive a class from it and implement the virtual function serve(). That function will be called whenever a new connection arrives.

class HelloServer : public SocketServer
{
public:
void serve(Socket client)
{
String name = client.readLine();
client << "Hello " + name + "\n";
}
};
HelloServer server;
server.bind(9000);
server.start();
virtual void serve(Socket client)
This function is called in a new thread with each new incoming client connection.
Definition: SocketServer.h:106

The server can be bound to more than one local address, incuding a TCP port and a Local Unix socket path.

To add a TLS serving end point, use the bindTLS function, and set the certificate and key.

server.bindTLS(443);
server.useCert(cert, key);

For long-running connections, the recommended way of dealing with possible disconnections is this:

void serve(Socket client)
{
while (client.connected())
{
if (!client.waitData())
continue;
String line = client.readLine(); // or other read operations
...
}
}

Add && !_requestStop to the while condition to let the service be stoppable by SocketServer::stop().

#include <SocketServer.h>

Inheritance diagram for SocketServer:
HttpServer WebSocketServer

Public Member Functions

void start (bool nonblocking=false)
 Makes the server start listening. More...
 
bool bindPath (const String &sname)
 Assigns a Unix local socket to listen to (where supported).
 
bool bind (int port)
 Assigns a TCP port to listen to.
 
bool bind (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen to.
 
bool bindTLS (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen for TLS connections.
 
bool bindTLS (int port)
 Assigns a TCP port for TLS connections.
 
bool useCert (const String &cert, const String &key)
 Sets the certificate and private key for the TLS server in PEM format.
 
virtual void serve (Socket client)
 This function is called in a new thread with each new incoming client connection. More...
 
void stop (bool sync=false)
 Requests the server to stop receiving connections, and waits for all clients to exit if sync is true.
 
void setSequential (bool on)
 Sets the mode of operation: sequential (connections will be handled in sequence) or concurrent ( connections will be handled in parallel by starting a new thread each time); this must be called before calling start() to start the server.
 
bool running () const
 Returns true if this server started and has not yet stopped or still has clients running.
 
String socketError () const
 Returns a string representation of the last socket error (after a failed bind or use cert...)
 

Member Function Documentation

◆ serve()

virtual void serve ( Socket  client)
inlinevirtual

This function is called in a new thread with each new incoming client connection.

Implement it in a subclass to make a specific server.

◆ start()

void start ( bool  nonblocking = false)

Makes the server start listening.

This function blocks by default. If the nonblocking argument is true the server will listen in its own thread and the call will not block.


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