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 and Specifying Client Outgoing Network Device
QtWS25 Last Chance

QTcpSocket and Specifying Client Outgoing Network Device

Scheduled Pinned Locked Moved General and Desktop
qtcpsocketbinding
6 Posts 3 Posters 3.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.
  • R Offline
    R Offline
    radian
    wrote on last edited by radian
    #1

    Windows 8.1 user here, using Qt 5.3. Trying to learn network programming (please bear with me). Let's say I have two network devices on my machine. One is assigned the IP 192.168.1.2, and the other 192.168.1.3. The first device has priority.

    My goal is to create a QTcpServer on 192.168.1.2 and a QTcpSocket client on 192.168.1.3. The way I envision this would work is the data packets from the client will start at 192.168.1.3 (on some port), travel to the router, then to the server at 192.168.1.2 (on some port). Hopefully this sounds reasonable.

    Here's the problem. I can't find a functioning way to specify an outgoing address/device with QTcpSocket. There appears to be a bind method, but it doesn't do much. Each time I send that from the client, it travels on the default device at 192.168.1.2.

    Any ideas?

    socket = new QTcpSocket(this);
    qDebug() << socket->localAddress(); // shows "0"
    qDebug() << socket->localPort(); // shows "0"

    socket->bind(QHostAddress("192.168.1.3"), 50000);
    qDebug() << socket->localAddress(); // shows "50000"
    qDebug() << socket->localPort(); // shows "0"

    //socket->setLocalAddress(QHostAddress("192.168.1.4")); // error, says it's protected
    //socket->setLocalPort("50000"); // error, says it's protected
    //qDebug() << socket->localAddress();
    //qDebug() << socket->localPort();

    socket->connectToHost("google.com", 80); // network manager shows data on 192.168.1.2

    1 Reply Last reply
    0
    • R Offline
      R Offline
      radian
      wrote on last edited by radian
      #2

      Can anyone lend a hand? Thanks!

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mcosta
        wrote on last edited by
        #3

        HI and welcome to devnet,

        you can access to the native socket descriptor with http://doc.qt.io/qt-5/qabstractsocket.html#socketDescriptor and use native API to do this (is possible to do in Linux but I don't know about WIndows or OS X)

        Once your problem is solved don't forget to:

        • Mark the thread as SOLVED using the Topic Tool menu
        • Vote up the answer(s) that helped you to solve the issue

        You can embed images using (http://imgur.com/) or (http://postimage.org/)

        1 Reply Last reply
        0
        • cybercatalystC Offline
          cybercatalystC Offline
          cybercatalyst
          wrote on last edited by
          #4

          @radian can you check whether the bind() method returns true? And if it doesn't, what does error() say? Most probably something is going wrong that you did not think of and the first thing to do is to check error reporting.

          R 1 Reply Last reply
          0
          • cybercatalystC cybercatalyst

            @radian can you check whether the bind() method returns true? And if it doesn't, what does error() say? Most probably something is going wrong that you did not think of and the first thing to do is to check error reporting.

            R Offline
            R Offline
            radian
            wrote on last edited by radian
            #5

            @mcosta Thanks, I'll look into it. Are there any resources out there on manipulation of the socketDescriptor?

            @cybercatalyst It actually returns True, so must be working?

            So I tried another test, and am finding myself even more confused. I created a TCP server on 192.168.1.3 (a physical USB to Ethernet adapter device, called "Ethernet2") and then used telnet to open a socket to 192.168.1.3 from 192.168.1.2 (computer main network adapter, called "Ethernet").

            Sending and receiving data between the server from telnet works fine. The strange thing is that Windows network monitor for Ethernet2 shows no change in data transmitted or received.

            I would have thought that data should be changing equivalently between Ethernet and Ethernet2, but that doesn't appear to be the case. Just to be clear, both the computer ethernet device and USB ethernet device are connected to the router.

            Any idea why this would be the case?

            1 Reply Last reply
            0
            • cybercatalystC Offline
              cybercatalystC Offline
              cybercatalyst
              wrote on last edited by
              #6

              So you assume the measurements are wrong? Can you try with a better tool, like Wireshark? This shows you single packets and all details.

              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