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. QTcpServer::incomingConnection() && get ip address

QTcpServer::incomingConnection() && get ip address

Scheduled Pinned Locked Moved General and Desktop
10 Posts 4 Posters 25.5k 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.
  • L Offline
    L Offline
    luca
    wrote on 15 Nov 2010, 15:22 last edited by
    #1

    Hi all,
    I have a QTcpServer subclass:
    @
    class StarServer : public QTcpServer
    {
    Q_OBJECT
    public:
    StarServer(int port, QObject *parent = 0);
    protected:
    void incomingConnection(int socketDescriptor);
    ...
    }
    @

    incomingConnection create a QThread and pass it the socketDescriptor:
    @
    void StarServer::incomingConnection(int socket_descriptor)
    {
    AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(socket_descriptor, this);
    thread->start();
    }
    @

    I need to know the ip address of the remote host that connect to the server before starting the tread using socket_descriptor.

    I tried this:
    @
    void StarServer::incomingConnection(int socket_descriptor)
    {
    QTcpSocket tcp_sock_tmp;
    tcp_sock_tmp.setSocketDescriptor(socket_descriptor);
    qDebug() << tcp_sock_tmp.peerAddress().toString();

    AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(socket_descriptor, this);
    thread->start();
    

    }
    @

    In this way I get the ip address but next the thread can't use the socket_descriptor.

    Is there another way to get the ip?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      tony
      wrote on 15 Nov 2010, 15:26 last edited by
      #2

      Hi,

      you should connect to newConnection() signal, and then get the new QTcpSocket from nextPendingConnection().

      So you have localAddress(), localPort(), peerAddress() and peerPort(), I mean, everything you need :)

      Tony.

      1 Reply Last reply
      0
      • L Offline
        L Offline
        luca
        wrote on 15 Nov 2010, 15:31 last edited by
        #3

        I also need to pass the socketDescriptor to a thread so I must use StarServer::incomingConnection() .

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tony
          wrote on 15 Nov 2010, 15:36 last edited by
          #4

          Well, why don't you pass the QTcpSocket pointer to the thread? Do you need to customize some socket options?

          T.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            luca
            wrote on 15 Nov 2010, 15:43 last edited by
            #5

            Using QTcpSocket in a different thread require to pass the socket descriptor to the thread and construct the QTcpSocket in the QThread subclass as shown in the "threaded fortune server example":http://doc.qt.nokia.com/latest/network-threadedfortuneserver.html

            1 Reply Last reply
            0
            • T Offline
              T Offline
              tony
              wrote on 15 Nov 2010, 15:49 last edited by
              #6

              I see ... well, I used to pass QTcpSocket and make a "moveToThread", to fix thread affinity.

              T.

              1 Reply Last reply
              0
              • L Offline
                L Offline
                luca
                wrote on 15 Nov 2010, 16:12 last edited by
                #7

                It works! Thanks!
                I've never tried to construct a QTcpSocket and then move it to another thread.

                Now I have this:
                @
                void StarServer::incomingConnection(int socket_descriptor)
                {
                QTcpSocket *tcp_sock_tmp = new QTcpSocket();
                tcp_sock_tmp->setSocketDescriptor(socket_descriptor);
                qDebug() << tcp_sock_tmp->peerAddress().toString();

                AutenticazioneSatelliteThread *thread = new AutenticazioneSatelliteThread(tcp_sock_tmp, this);
                
                thread->start();
                

                }
                @

                and in AutenticazioneSatelliteThread :
                @
                AutenticazioneSatelliteThread::AutenticazioneSatelliteThread(QTcpSocket *socket, QObject *parent)
                : QThread(parent)
                {
                qDebug() << socket->peerAddress().toString();
                socket->moveToThread(this);

                autenticazioneSatelliteSocket = socket;
                

                ....
                @

                Thanks again!
                (se capiti in Ancona... ti devo una birra...)

                1 Reply Last reply
                0
                • T Offline
                  T Offline
                  tony
                  wrote on 15 Nov 2010, 17:13 last edited by
                  #8

                  You're welcome!

                  ( ci conto :) )

                  1 Reply Last reply
                  0
                  • C Offline
                    C Offline
                    captainst
                    wrote on 29 May 2011, 02:05 last edited by
                    #9

                    Hi, are you sure that the client socket is running in a seperate thread after using
                    @socket->moveToThread(this); @ in the constructor of the thread class ?

                    I think that the thread class won't execute in the second thread until the "run" is called, which implys that the constructor of the thread class executes still in the main thread. It probably indicates that your "moveToThread" won't move the socket to the second thread. You socket client will execute still in the main thread.

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      dangelog
                      wrote on 30 May 2011, 09:04 last edited by
                      #10

                      [quote author="Luca" date="1289835800"]Using QTcpSocket in a different thread require to pass the socket descriptor to the thread and construct the QTcpSocket in the QThread subclass as shown in the "threaded fortune server example":http://doc.qt.nokia.com/latest/network-threadedfortuneserver.html
                      [/quote]

                      BTW, I asked and I've been told that the docs (and the example) are wrong about that point:

                      [quote]Note: The returned QTcpSocket object cannot be used from another thread. If you want to use an incoming connection from another thread, you need to override incomingConnection().[/quote]

                      Software Engineer
                      KDAB (UK) Ltd., a KDAB Group company

                      1 Reply Last reply
                      0
                      • B bogong referenced this topic on 20 Aug 2023, 09:44

                      • Login

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