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. Connecting and reading data from multiple QTcpSockets with QTcpServer
Forum Updated to NodeBB v4.3 + New Features

Connecting and reading data from multiple QTcpSockets with QTcpServer

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 525 Views
  • 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.
  • C Offline
    C Offline
    cdecde57
    wrote on last edited by
    #1

    Hello! I recently began working with QTcpSocket and QTcpServer, I am where I have a server which will accept all the oncoming clients / tcpsockets. It works great and all, however I have hit into a difficult dilemma.

    Every new connection I will take the pending connection and insert it into a vector as well as set up connections so I can communicate with the client. (Save the clients socket in a vector)

     QTcpSocket *newConnection{m_Server->nextPendingConnection()};
        
        //QList<QTcpSocket*>
        m_PendingC.append(newConnection);
    
        //Connection for when the client disconnects from the server
        connect(newConnection, &QTcpSocket::disconnected, this, &MyServer::disconnected);
    
        //Connection for when the client sends data to the server
        connect(newConnection, &QTcpSocket::readyRead, this, &MyServer::reading);
    
    ...
    
    void MyServer::reading(){
        qDebug() << "Data from the client is :" << /*reference to the client*/.readAll();
    }
    

    However, the problem occurs when I receive data from the client, I don't know of any way to check which client sent the information and going through the list of clients and checking by saying .readAll() seems to be inefficient.

    Is there any method or signal I can use that will return a reference / pointer to the socket who sent the data to the server?

    1 Reply Last reply
    0
    • nageshN Offline
      nageshN Offline
      nagesh
      wrote on last edited by
      #2

      @cdecde57 in the slot typecast the this->sender() to socket type.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        cdecde57
        wrote on last edited by cdecde57
        #3

        Thank you for your help, Worked like a charm! I guess just understanding what Qt provides makes things a lot easier, lol.

        Also sorry for the late response, I had to finish another task which took my time away from programming.

        Anyways, if anyone wants to see what I did based off of @nagesh s feedback I provide it down below just for the sake of it.

        I used a lambada function as the slot and used the sender() function in the this pointer to capture the specific socket making the call and from there I was able to send it to the appropriate function. I know that you can make function calls with SLOT(myFunciton(params)) etc, but I couldn't get the signal to work and decided to just go the easy way with lambada, but the main idea is that by using the sender function you can obtain the object who triggered the signal to start with.

           connect(newConnection, &QTcpSocket::readyRead, this, [=](){
               reading(static_cast<QTcpSocket*>(this->sender()));
           });
        
        jsulmJ 1 Reply Last reply
        0
        • C cdecde57

          Thank you for your help, Worked like a charm! I guess just understanding what Qt provides makes things a lot easier, lol.

          Also sorry for the late response, I had to finish another task which took my time away from programming.

          Anyways, if anyone wants to see what I did based off of @nagesh s feedback I provide it down below just for the sake of it.

          I used a lambada function as the slot and used the sender() function in the this pointer to capture the specific socket making the call and from there I was able to send it to the appropriate function. I know that you can make function calls with SLOT(myFunciton(params)) etc, but I couldn't get the signal to work and decided to just go the easy way with lambada, but the main idea is that by using the sender function you can obtain the object who triggered the signal to start with.

             connect(newConnection, &QTcpSocket::readyRead, this, [=](){
                 reading(static_cast<QTcpSocket*>(this->sender()));
             });
          
          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @cdecde57 There is no need for sender(). You're already using a lambda as slot, so simply capture the sender:

          connect(newConnection, &QTcpSocket::readyRead, this, [newConnection, this](){
                 reading(newConnection);
             });
          

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          3

          • Login

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