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. Subclass of QTcpSocket not working properly under Linux

Subclass of QTcpSocket not working properly under Linux

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 360 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.
  • Z Offline
    Z Offline
    ZZZCY
    wrote on last edited by
    #1

    I'm trying to run a code that can run correctly under Windows, but I find it can't run under Linux, the slot never triggered.
    void MainWindow::slotNewConnection() {
    while (tcpserver->hasPendingConnections()) {
    QTcpSocket *tcp = tcpserver->nextPendingConnection();
    TcpPlatformSocket *tcpsocket = new TcpPlatformSocket;
    tcpsocket->setSocketDescriptor(tcp->socketDescriptor());
    connect(tcpsocket,&TcpPlatformSocket::readyRead,this,&MainWindow::slotReadyRead);
    TcpPlatformSocketList.append(tcpsocket);
    mode = MODE_WAIT_TYPE;
    }
    }

    void MainWindow::slotReadyRead() {
    QObject* obj = sender();
    TcpPlatformSocket* ptcpSocket = dynamic_cast<TcpPlatformSocket*>(obj);

    QByteArray buffer = ptcpSocket->readAll();
    for(int i=0; i<buffer.length(); i++){
        readStateMachine(ptcpSocket, buffer.at(i));
    }
    

    }

    class TcpPlatformSocket : public QTcpSocket
    {
    Q_OBJECT
    public:
    explicit TcpPlatformSocket(QObject *parent = nullptr);
    void setPlatformType(platform_t platformType);
    platform_t getPlatformType();

    private:
    platform_t platformType;

    };

    Christian EhrlicherC 1 Reply Last reply
    0
    • Z ZZZCY

      I'm trying to run a code that can run correctly under Windows, but I find it can't run under Linux, the slot never triggered.
      void MainWindow::slotNewConnection() {
      while (tcpserver->hasPendingConnections()) {
      QTcpSocket *tcp = tcpserver->nextPendingConnection();
      TcpPlatformSocket *tcpsocket = new TcpPlatformSocket;
      tcpsocket->setSocketDescriptor(tcp->socketDescriptor());
      connect(tcpsocket,&TcpPlatformSocket::readyRead,this,&MainWindow::slotReadyRead);
      TcpPlatformSocketList.append(tcpsocket);
      mode = MODE_WAIT_TYPE;
      }
      }

      void MainWindow::slotReadyRead() {
      QObject* obj = sender();
      TcpPlatformSocket* ptcpSocket = dynamic_cast<TcpPlatformSocket*>(obj);

      QByteArray buffer = ptcpSocket->readAll();
      for(int i=0; i<buffer.length(); i++){
          readStateMachine(ptcpSocket, buffer.at(i));
      }
      

      }

      class TcpPlatformSocket : public QTcpSocket
      {
      Q_OBJECT
      public:
      explicit TcpPlatformSocket(QObject *parent = nullptr);
      void setPlatformType(platform_t platformType);
      platform_t getPlatformType();

      private:
      platform_t platformType;

      };

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      You copy the socket descriptor from one socket to another for no reason. Use the returned QTcpSocket instead creating a new one with the same socket descriptor.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Z 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        You copy the socket descriptor from one socket to another for no reason. Use the returned QTcpSocket instead creating a new one with the same socket descriptor.

        Z Offline
        Z Offline
        ZZZCY
        wrote on last edited by
        #3

        @Christian-Ehrlicher I want to use my TcpPlatformSocket instead of QTcpsocket

        JonBJ 1 Reply Last reply
        0
        • Z ZZZCY

          @Christian-Ehrlicher I want to use my TcpPlatformSocket instead of QTcpsocket

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @ZZZCY
          Since QTcpSocket *QTcpServer::nextPendingConnection() returns a base QTcpSocket created by Qt intrastructure I think you would have to override void QTcpServer::incomingConnection(qintptr socketDescriptor) and create your own to achieve that.

          In which case it may not be worth the effort subclassing to create your own? Maybe your TcpPlatformSocket could just encapsulate instead?

          Z 1 Reply Last reply
          2
          • JonBJ JonB

            @ZZZCY
            Since QTcpSocket *QTcpServer::nextPendingConnection() returns a base QTcpSocket created by Qt intrastructure I think you would have to override void QTcpServer::incomingConnection(qintptr socketDescriptor) and create your own to achieve that.

            In which case it may not be worth the effort subclassing to create your own? Maybe your TcpPlatformSocket could just encapsulate instead?

            Z Offline
            Z Offline
            ZZZCY
            wrote on last edited by
            #5

            @JonB Thanks for your reply, I solved the problem according to your description

            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