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. [Solved] Can't get peer Adress from QTcpSocket
QtWS25 Last Chance

[Solved] Can't get peer Adress from QTcpSocket

Scheduled Pinned Locked Moved General and Desktop
7 Posts 2 Posters 6.8k 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.
  • B Offline
    B Offline
    buggi
    wrote on last edited by
    #1

    Hello,
    I recently started programming with Qt and am now working on a asynchronous QTcpServer. Now what I am trying to do is create a new File for every unique client that connects to my Server with this Code:

    @socket = new QTcpSocket(this);

    socket->setSocketDescriptor(handle); 
    
    qDebug()<<socket->localAddress().toIPv4Address();
    qDebug()<<socket->localPort();
    MyClient::FileName = MainWindow::FileName.append("/").append(socket->localAddress().toIPv4Address()).append(".txt");
    qDebug()<<MyClient::FileName;
    
    connect(socket, SIGNAL(connected()), this, SLOT(connected()));
    connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
    connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    
    qDebug()<<"Client connected";@ 
    

    But the Filename is either empty or I get "3232243626" as output from "qDebug()<<socket->localAddress().toIPv4Address();" what leads into having a File with the name "ᾪ".
    I hope somebody can tell me what I am doing wrong.
    Thanks in advance.

    1 Reply Last reply
    0
    • B Offline
      B Offline
      beemaster
      wrote on last edited by
      #2

      Probably you should use "QTcpServer":http://qt-project.org/doc/qt-4.8/qtcpserver.html to listen incoming connections and then obtain socket descriptor via "nextPendingConnection":http://qt-project.org/doc/qt-4.8/qtcpserver.html#nextPendingConnection
      Have a look at "Fortune Server Example":http://qt-project.org/doc/qt-4.8/network-fortuneserver.html

      1 Reply Last reply
      0
      • B Offline
        B Offline
        buggi
        wrote on last edited by
        #3

        Ok I dont really know what you mean but I think I am already doing that. I will better post my complete Code:
        Server.cpp:
        @void MyServer::StartServer ()
        {
        if(listen(QHostAddress::Any, 45454))
        {
        qDebug()<<"Server is listening";
        }
        else
        {
        qDebug()<<"Server failed to start!";
        }
        }

        void MyServer::incomingConnection (qintptr handle)
        {
        client = new MyClient(this);
        client->CreatSocket(handle);
        }@
        Client.cpp:
        @void MyClient::CreatSocket(qintptr handle)
        {
        socket = new QTcpSocket(this);

        socket->setSocketDescriptor(handle); 
        
        qDebug()<<socket->localAddress().toIPv4Address();
        qDebug()<<socket->localPort();
        MyClient::FileName = MainWindow::FileName.append("/").append(socket->localAddress().toIPv4Address()).append(".txt");
        qDebug()<<MyClient::FileName;
        
        connect(socket, SIGNAL(connected()), this, SLOT(connected()));
        connect(socket, SIGNAL(disconnected()), this, SLOT(disconnected()));
        connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
        
        qDebug()<<"Client connected";
        

        }@
        The Server logic runs fine: I can connect, I can exchange data etc. the only thing that blows my mind is, why I cant get the correct Ip of the connected socket.
        PS: Thank you for your fast response.

        1 Reply Last reply
        0
        • B Offline
          B Offline
          beemaster
          wrote on last edited by
          #4

          OK, I see, you mix bsd socket API with Qt API. A bit offtopic, but I suggest you to start using "QTcpServer::listen":http://qt-project.org/doc/qt-4.8/qtcpserver.html#listen instead of just calling listen() next time.

          If I understand, you want to obtain remote peer address. Have you tried calling "peerAddress":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#peerAddress and "peerPort":http://qt-project.org/doc/qt-4.8/qabstractsocket.html#peerPort ?

          1 Reply Last reply
          0
          • B Offline
            B Offline
            buggi
            wrote on last edited by
            #5

            Hey beemaster, thank you for helping me!
            So I changed "if(listen(QHostAddress::Any, 45454))" to "if(this->listen(QHostAddress::Any, 45454))". Is that better?

            So I used peerAddress and peerPort instead and my output actually changed. I get "3232243573" for peerAddress now what leads into having a file named "ή". By the way, I discovered that if I connect from localhost the peerAddress is empty and if I connect from any maschine to the server they all get the same number as peer address. I dont know if this matters but the server is runnng on opensuse 13.1 and the clients I tested with are Windows 7 maschines.

            1 Reply Last reply
            0
            • B Offline
              B Offline
              beemaster
              wrote on last edited by
              #6

              When you call
              @
              socket->peerAddress().toIPv4Address();
              @

              you get a 4 byte integer. If you want IP address in form of string you should call
              @
              socket->peerAddress().toString();
              @

              1 Reply Last reply
              0
              • B Offline
                B Offline
                buggi
                wrote on last edited by
                #7

                Yeah!! :) Thank you, that did the Trick. I didn't use this because when I tested by connecting from localhost I got the Ipv6 Address back, but with an other host the String is what I expected it to be.

                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