In your server create a list for client connections.
QList<QTcpSocket *> clientConnections;
Then, when you receive an new incomming connection, add the socket to the clientConnections list.
Connect the most useful signals of the client socket to slots in your server. For example, disconnected() and readyRead().
Example:
void YourServer::incomingConnection(int socketDescriptor)
{
clientConnection->setSocketDescriptor(socketDescriptor);
clientConnections->append(clientConnection);
connect(clientConnection, SIGNAL(...), this, SLOT(...));
// send some welcome to your socket (example)
sendWelcome(clientConnection);
}
void YourServer::incomingConnection(int socketDescriptor)
{
QTcpSocket *clientConnection = new QTcpSocket;
clientConnection->setSocketDescriptor(socketDescriptor);
clientConnections->append(clientConnection);
connect(clientConnection, SIGNAL(...), this, SLOT(...));
// send some welcome to your socket (example)
sendWelcome(clientConnection);
}
To copy to clipboard, switch view to plain text mode
When you want to send something to all client connections:
void sendToAllConnections(...)
{
foreach
(QTcpSocket *clientConnection, clientConnections
) { clientConnection->write(...);
}
etc...
}
void sendToAllConnections(...)
{
foreach(QTcpSocket *clientConnection, clientConnections) {
clientConnection->write(...);
}
etc...
}
To copy to clipboard, switch view to plain text mode
Bookmarks