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. QTcpSocket accepts localhost but not 127.0.0.1
Forum Updated to NodeBB v4.3 + New Features

QTcpSocket accepts localhost but not 127.0.0.1

Scheduled Pinned Locked Moved General and Desktop
2 Posts 2 Posters 8.1k Views 1 Watching
  • 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.
  • M Offline
    M Offline
    merajc
    wrote on last edited by
    #1

    Hello

    I have a Qt server app running on my computer, and a Qt client app which connects with:
    @socket.connectToHost("localhost", 9998);@
    or
    @socket.connectToHost("my-pc-name", 9998);@

    The above works perfectly. But the following:
    @socket.connectToHost("127.0.0.1", 9998);@
    or
    @socket.connectToHost(QHostAddress("localhost"), 9998);@

    These two lines don't work.

    My Apache server on the other hand responds to these IPs well when I enter them on my browser:
    @127.0.0.1
    192.168.0.2
    my-pc-name
    localhost@

    I downloaded the latest Qt SDK from qt.nokia.com for my Ubuntu 12.04 installation. The about page says:
    @Qt Creator 2.4.1
    Based on Qt 4.7.4 (64 bit)
    Built on Jan 25 2012 at 11:24:18
    From revision 8cd370e163@

    What could be the problem? The biggest problem is that on my N9 I even the face problem when trying these lines:
    @socket.connectToHost("my-pc-name", 9998);@

    and
    @socket.connectToHost("192.168.0.2", 9998);@

    Thank you!

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by
      #2

      QHostAddress() when constructed with a string expects an IP address not a name and it does not do a name lookup. So, QHostAddress("localhost") is not valid. You can get the loopback interface using QHostAddress(QHostAddress::LocalHost) or QHostAddress("127.0.0.1").

      QAbstractSocket::connectToHost(), when given a QString, does a name lookup if needed, i.e. it takes a name or an IP address in the string. Both variants work correctly here. If you have no name resolution service then this might fail (I don't know about the N9).

      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