You can only read from the socket if there's something to read.
You try to read a nanosecond after you've send the welcom message. In that time, the client didn't have time to respond.
void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
Connections connections; // This too
while (server->hasPendingConnections ()) {
QTcpSocket *client
= server
->nextPendingConnection
();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
signalmapper->setMapping(client, client);
client->write("Welcome!");
}
connect(signalmapper,
SIGNAL(mapped
(QObject *)),
this,
SLOT(clientReadyRead
(QObject *)));
}
void Server
::clientReadyRead(QObject *socket
) {
QTcpSocket *clientSocket
= qobject_cast<QTcpSocket
*>
(socket
);
clientSocket->read...()
}
void Server::sendWelcomeMessage()
{
typedef QVector<QTcpSocket*> Connections; // <-- Define this at the class level
Connections connections; // This too
while (server->hasPendingConnections ()) {
QTcpSocket *client = server->nextPendingConnection();
connect(client, SIGNAL(disconnected()), client, SLOT(deleteLater()));
connect(client, SIGNAL(readyRead()), signalmapper, SLOT(map())); // Signal mapper is defined in the class definition and created in the constructor, see QSignalMapper.
signalmapper->setMapping(client, client);
client->write("Welcome!");
}
connect(signalmapper, SIGNAL(mapped(QObject *)), this, SLOT(clientReadyRead(QObject *)));
}
void Server::clientReadyRead(QObject *socket)
{
QTcpSocket *clientSocket = qobject_cast<QTcpSocket *>(socket);
clientSocket->read...()
}
To copy to clipboard, switch view to plain text mode
Bookmarks