PDA

View Full Version : Handling all requests from same tcp client in server side



JackHammer
12th January 2011, 18:17
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(socketDescr iptor))
{
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);
}
}

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.

wysota
12th January 2011, 21:24
What exactly do you mean by "the same client"? Do you mean all requests from the same connection or all requests from any connection qualified by some id ("client id") or something else? Currently your code associates clients to socket numbers they are using which is very unlikely to be what you want as socket numbers are assigned randomly to connections.

JackHammer
13th January 2011, 05:30
Thanks for your reply.

I solved the problem. It was my mistake. I was making use of Qt's fortune client example for testing my server.It was making a new connection when ever a new request is send. Actually I wanted all requests from same connection to be handled by a particular client. Thanks once again.