Important: Please read the Qt Code of Conduct -

Setting up two QTcpServer with 2 clients on 2 threads

  • Hello,
    I have a main application which will run a simulation and set up a QTcpServer to receive messages over localhost from 1 or 2 embedded clients. The sockets have to work asynchronously.

    Previously, I have set up (in Java not Qt) a listen thread, created a socket in the thread, waited for a connection on a particular port, then just plotted the data from the connection. Since there were no slots or signals, it was straight forward. And I had only one embedded system to talk to.
    @CODE SNIPPET JAVA***********
    ListenAndWriteThread(LiveGraph app){
    t = new Thread(this,"Demo Thread");;

    public void run(){        
        TcpServer ts = new TcpServer();                                     // Create the server
        ts.setPort( port );                                                 // Set the port
        ts.addTcpServerListener( new TcpServer.Listener() {                 // Add listener
        public void socketReceived( TcpServer.Event evt ) {             // New stream
        DataStreamWriter fout =null; 
        InputStream in = null;
        OutputStream out = null;@

    CODE SNIPPET END**********
    Now, I need a signal from the server when client 1 connects to set up the GUI Sliders and Dials.
    When the client starts sending data, I need signal from the socket to update the GUI appropriately.

    All the examples I have seen seem to close the connection, I want to ensure minimal latency (50 samples per second) and keep both the connections open while I continue running the simulator in the background.
    The output from the simulator is plotted on the bottom panel of the GUI, updated every 100 ms.

    SHould I start a server first and for each connection request spin off a separate thread?
    The thread, depending on the client port will have specific instructions for handling incoming data...

    Or should I have the tcpserver create a socket and have the socket launch threads for handling incoming data?

    I seek clarity, I am totally confused by the idea of thread and signal/socket. I am even more confused newbie after going through all the examples in the QTcpSocket section of the documentation.

    Any guidance and / or example code would be most welcome.


  • Have you checked the fortune cookie server example? I seem to recall that it deals with the things you are trying to achieve. Depending on how you deal with things, you don't even have to create threads. You can use QFuture to launch a method in a new thread. Easier to manage IMO, but it depends on the use case of course.

    bq. Or should I have the tcpserver create a socket and have the socket launch threads for handling incoming data?

    That sounds to me like the soundest approach.

  • Qt Champions 2017

    You don't need threads at all. What you are looking at is concurrent TCP server. Multiple clients connect to server simultaneously. Just write TCP server. Connect with multiple client. It will work.

Log in to reply