Hi,
I have a Server class which inherits from QTcpServer to accept the TCP connections. To handle each request in a custom way I have reimplemented QTcpServer::incomingConnection(). I have a custom class named 'ClientHandler' on server side which inherits from QTcpSocket and I am making the object of 'ClientHandler' class inside QTcpServer::incomingConnection(int socketDescriptor) and setting the socket descriptor to it.
Now I want to handle all the requests from the same TCP client by the same 'ClientHandler' object. To do that I am currently using the following code ...
void Server::incomingConnection ( int socketDescriptor )
{
if(socketDescClientHandlerMap.contains(socketDescriptor))
{
ClientHandler* clientHandler = socketDescHandlerClientMap.value(socketDescriptor);
//It seems we have to set socketDescriptor again to handle the next request
clientHandler->setSocketDescriptor(socketDescriptor);
}
else
{
ClientHandler* clientHandler = new ClientHandler(this);
socketDescClientHandlerMap[socketDescriptor] = clientHandler;
clientHandler->setSocketDescriptor(socketDescriptor);
}
}
void Server::incomingConnection ( int socketDescriptor )
{
if(socketDescClientHandlerMap.contains(socketDescriptor))
{
ClientHandler* clientHandler = socketDescHandlerClientMap.value(socketDescriptor);
//It seems we have to set socketDescriptor again to handle the next request
clientHandler->setSocketDescriptor(socketDescriptor);
}
else
{
ClientHandler* clientHandler = new ClientHandler(this);
socketDescClientHandlerMap[socketDescriptor] = clientHandler;
clientHandler->setSocketDescriptor(socketDescriptor);
}
}
To copy to clipboard, switch view to plain text mode
Is it the right way to do it? If not please suggest me the correct way...
(I checked peerPort() and peerAddress() - both of them seems useless for the current scenario)
Thanks in advance.
Bookmarks