Listening to clients

The chatServer will trigger the newConnection() signal whenever a client has connected to the server, so we connect that signal to our custom slot function called newClientConnection(). The slot function looks like this:

void server::newClientConnection() 
{ 
   QTcpSocket* client = chatServer->nextPendingConnection(); 
   QString ipAddress = client->peerAddress().toString(); 
   int port = client->peerPort(); 
 
   connect(client, &QTcpSocket::disconnected, this, &server::socketDisconnected); connect(client, &QTcpSocket::readyRead, this, &server::socketReadyRead); connect(client, &QTcpSocket::stateChanged, this, &server::socketStateChanged); allClients->push_back(client); qDebug() << "Socket connected from " + ipAddress + ":" + QString::number(port); ...

Get Hands-On GUI Programming with C++ and Qt5 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.