Results 1 to 5 of 5

Thread: QTcpServer - get the correct QTcpSocket

  1. #1
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTcpServer - get the correct QTcpSocket

    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?

  2. #2
    Join Date
    Feb 2008
    Posts
    98
    Thanks
    2
    Thanked 24 Times in 24 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - get the correct QTcpSocket

    You may use a QHash<QTcpSocket*,Command>. In order to keep track of the status of each client, use the command pattern. Anyway, in my opinion you should use threads for this. The code would be cleaner and you wouldn't have the risk of a slow client freezing your GUI.

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpServer - get the correct QTcpSocket

    Why would a slow client freeze the gui if the client is on another machine? Using threads is a bad idea here, it doesn't make the code any cleaner and it slows down the whole application and makes it more complex.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jan 2006
    Posts
    369
    Thanks
    14
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTcpServer - get the correct QTcpSocket

    Actually, the "server" is on on localhost... let me explain:

    This customer has a client server application written in C#. They decided to port it to linux, nice. The server work on mono, and the client does not, because it displays video. Ok, lets spit the client into two new parts:

    Qt Code:
    1. [server] <-> [client]
    2. [server] <-> {[backend (C#) ] <-> [frontend (Qt4)]}
    To copy to clipboard, switch view to plain text mode 

    Now the communication between the front end and backend will be done on TCP sockets. Both sides are on "localhost".

    Each time I talk to the customer and ask for implementation ideas, he keeps talking about threads and stuff. He is a on time C/win32 guy who does not fully understand that my whole application is single threaded yet (he did not overview the code yet), and that the model that Qt4 imposes by default is asynchronous callbacks.

    Anyway, my idea was to emit signals which contain the socket, back and forth between the front end and backend, or maybe the front end can directly write the messages to the socket. I am looking for other alternatives (create a new separate class for each connection might be a good idea, but it has a lot of overhead).

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QTcpServer - get the correct QTcpSocket

    Quote Originally Posted by elcuco View Post
    Actually, the "server" is on on localhost..
    It doesn't matter, it's a different process.

    Each time I talk to the customer and ask for implementation ideas,
    First rule of software development - never let your client anywhere near any implementation details. Either you are creating the application or he is.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. double QSignalMapper with QTcpServer/QTcpSocket
    By radeberger in forum Qt Programming
    Replies: 5
    Last Post: 5th May 2010, 18:46
  2. QThread vs QTcpServer feat. QTcpSocket
    By ilyagoo in forum Newbie
    Replies: 6
    Last Post: 14th December 2009, 07:46
  3. QTcpServer QTcpSocket problem
    By jmsbc in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2009, 17:42
  4. QTcpSocket, QTcpServer problem
    By frido in forum Qt Programming
    Replies: 3
    Last Post: 3rd April 2009, 23:16
  5. QTcpServer & QTcpSocket questions...
    By jxmot in forum Qt Programming
    Replies: 2
    Last Post: 24th April 2008, 21:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.