I am wrigin a small server in Qt4. The "protocol" is quite simple:

client sends "cmd1" server responds "ok"
client sends "cmd2" server responds "ok"
client sends "cmd3" server responds "fail"

I have a few doubts about the best approach to this:
each time a new connection is made, and new callback will be called from which I `get tcpServer->nextPendingConnection()` and. The problem I face is that if I have several connections, I am not sure how to save that socket. My first implementation saved the socket in the class - but this kills the concurancy and only one client can connect.

My second implementation is doing this trick:

Qt Code:
  1. void LogicCommunication::on_data_read()
  2. {
  3. QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
  4. QString s = socket->readAll();
  5.  
  6. QStringList args = s.split(',');
  7. QString command = args[0];
  8. if (command == QLatin1String("layout"))
  9. parseNewLayout(s,args,socket);
  10. else if (command == QLatin1String("play"))
  11. parsePlayFile(s,args,socket);
  12. else if (command == QLatin1String("stop"))
  13. parseStop(s,args,socket);
  14. else
  15. emit errorInProtocol(s,"No valid command sent",socket);
  16. }
To copy to clipboard, switch view to plain text mode 

In each command I emit a signal, I also pass the socket and the remote side (the GUI in my case) can then use that socket to send information back. I am not really happy about this solution, since this means that the GUI needs to know about the connection media (QTcpSocket).

The other alternative I am thinking of is saving the socket in a separate thead, but I am not happy about it. Who needs threads for this code?

Do I have another alternatives? What would you guys do?