Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    [Solved] Can't get peer Adress from QTcpSocket

    General and Desktop
    2
    7
    6216
    Loading More Posts
    • 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
      buggi last edited by

      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 Reply Quote 0
      • B
        beemaster last edited by

        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 Reply Quote 0
        • B
          buggi last edited by

          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 Reply Quote 0
          • B
            beemaster last edited by

            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 Reply Quote 0
            • B
              buggi last edited by

              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 Reply Quote 0
              • B
                beemaster last edited by

                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 Reply Quote 0
                • B
                  buggi last edited by

                  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 Reply Quote 0
                  • First post
                    Last post