Ok, that was easier then thought:
Apparently dup2() (never heard of it..) can redirect stdout and stderr to another (file or socket)descriptor (sorry, never heard of that either... ).
Whenever the QTcpServer gets an incoming connection, a incomingConnection(int handle) is emitted. This handle can be used in order to redirect all stdout ans stderr over a newly created socket.
void Server::incomingConnection(int handle)
{
Client *client = new Client(this);
client->setSocket(handle);
}
void Server::incomingConnection(int handle)
{
Client *client = new Client(this);
client->setSocket(handle);
}
To copy to clipboard, switch view to plain text mode
In client:
void Client::setSocket(int descriptor)
{
socket->setSocketDescriptor(descriptor);
dup2(descriptor, 1); // stdout
dup2(descriptor, 2); // stderr
qDebug() << "This text ends up at the telnet-session of the connected client";
// connect socket signals to slots
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
qDebug() << "Socket created";
}
void Client::setSocket(int descriptor)
{
socket = new QTcpSocket(this);
socket->setSocketDescriptor(descriptor);
dup2(descriptor, 1); // stdout
dup2(descriptor, 2); // stderr
qDebug() << "This text ends up at the telnet-session of the connected client";
// connect socket signals to slots
connect(socket, SIGNAL(connected()), this, SLOT(socketConnected()));
connect(socket, SIGNAL(readyRead()), this, SLOT(socketReadyRead()));
qDebug() << "Socket created";
}
To copy to clipboard, switch view to plain text mode
Bookmarks