elcuco
10th May 2010, 14:22
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:
void LogicCommunication::on_data_read()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
QString s = socket->readAll();
QStringList args = s.split(',');
QString command = args[0];
if (command == QLatin1String("layout"))
parseNewLayout(s,args,socket);
else if (command == QLatin1String("play"))
parsePlayFile(s,args,socket);
else if (command == QLatin1String("stop"))
parseStop(s,args,socket);
else
emit errorInProtocol(s,"No valid command sent",socket);
}
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?
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:
void LogicCommunication::on_data_read()
{
QTcpSocket *socket = qobject_cast<QTcpSocket*>(sender());
QString s = socket->readAll();
QStringList args = s.split(',');
QString command = args[0];
if (command == QLatin1String("layout"))
parseNewLayout(s,args,socket);
else if (command == QLatin1String("play"))
parsePlayFile(s,args,socket);
else if (command == QLatin1String("stop"))
parseStop(s,args,socket);
else
emit errorInProtocol(s,"No valid command sent",socket);
}
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?