Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Setting up two QTcpServer with 2 clients on 2 threads
Forum Updated to NodeBB v4.3 + New Features

Setting up two QTcpServer with 2 clients on 2 threads

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kpks
    wrote on last edited by
    #1

    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");
    this.app=app;
    t.start();
    }

    public void run(){        
        TcpServer ts = new TcpServer();                                     // Create the server
        ts.setPort( port );                                                 // Set the port
    
        ts.addTcpServerListener( new TcpServer.Listener() {                 // Add listener
            //@Override
        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.

    Thanks,
    kpks

    1 Reply Last reply
    0
    • J Offline
      J Offline
      janfaroe
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • dheerendraD Offline
        dheerendraD Offline
        dheerendra
        Qt Champions 2022
        wrote on last edited by
        #3

        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.

        Dheerendra
        @Community Service
        Certified Qt Specialist
        http://www.pthinks.com

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved