PDA

View Full Version : Extend QTcpServer to handle multiple clients



DiamonDogX
22nd February 2010, 15:24
I have a class that extends QTcpServer and listens on a specified port for an incoming client connection (i.e. using nextPendingConnection() and so on). Was wondering what the most elegant way would be to handle multiple clients connecting and keeping track of them. This implies being able to identify each one, so maybe I'd have to do something like overload "incomingConnection( int socketDescriptor )"?

mcosta
23rd February 2010, 13:48
For each QTcpSocket returned by QTcpServer::nextPendingConnection() you can store information such as remoteAddress, remotePort and so on.

DiamonDogX
23rd February 2010, 17:42
I assume you are referring to peerAddress() and peerPort()?

mcosta
23rd February 2010, 20:50
Yes. you're right. I'm sorry for the mistake

DiamonDogX
24th February 2010, 18:59
So if multiple clients are connected and want to send data to the server, how should the server handle reading the data for each separate client (i.e. readyRead())?

mcosta
24th February 2010, 19:49
there isn't a unique solution but you could use a QSignalMapper to map each readReady signal with a slot with a parameter that allow to distinguish the client who sent data.

Read the documentation for use of QSignalMapper and try to use it.