#include <iostream>
using namespace std;
class MyClass : public sigc::trackable
{
public:
MyClass(void)
{
server->clientConnected.connect(
mem_fun(*this, &MyClass::onClientConnected));
server->clientDisconnected.connect(
mem_fun(*this, &MyClass::onClientDisconnected));
cout << "Connect using: \"telnet localhost 12345\" from "
"another console\n";
}
~MyClass(void)
{
delete server;
}
private:
{
con->
dataReceived.connect(mem_fun(*
this, &MyClass::onDataReceived));
con->
write(
"Hello, client!\n", 15);
}
{
}
{
char *str = static_cast<char *>(buf);
string data(str, str+count);
cout << data;
string dataOut = string("You said: ") + data;
server->
writeOnly(con, dataOut.c_str(), dataOut.size());
{
dataOut = string("He said : ") + data;
server->
writeExcept(con, dataOut.c_str(), dataOut.size());
dataOut = string("To all : ") + data;
server->
writeAll(dataOut.c_str(), dataOut.size());
}
return count;
}
};
int main(int argc, char **argv)
{
MyClass my_class;
}
The core class for writing asyncronous cpp applications.
A class for creating a TCP server.
An application class for writing non GUI applications.
void exec(void)
Execute the application main loop.
std::string toString(void) const
Return the string representation of the IP address.
A class for handling exiting TCP connections.
const IpAddress & remoteHost(void) const
Return the IP-address of the remote host.
uint16_t remotePort(void) const
Return the remote port used.
DisconnectReason
Reason code for disconnects.
sigc::signal< int, TcpConnection *, void *, int > dataReceived
A signal that is emitted when data has been received on the connection.
virtual int write(const void *buf, int count)
Write data to the TCP connection.
int writeAll(const void *buf, int count)
Write data to all connected clients.
int numberOfClients(void)
Get the number of clients that is connected to the server.
int writeExcept(TcpConnection *con, const void *buf, int count)
Send data to all connected clients except the given client.
int writeOnly(TcpConnection *con, const void *buf, int count)
Send data only to the given client.
A class for creating a TCP server.
Namespace for the asynchronous programming classes.