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. Problem with QUdpSocket bind with port lower than 1024

Problem with QUdpSocket bind with port lower than 1024

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 5 Posters 527 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.
  • E Offline
    E Offline
    edoardo.videx
    wrote on last edited by
    #1

    Hi to everyone!
    I develop my desktop application with MacOS. My application use a udp port: 1002.
    I know that all the unix system has a protected ports below 1024.

    Initially i binded the ip and port in this way:

    // create a QUDP socket
    socket = new QUdpSocket(this);
    port = 1002;
    QHostAddress *addr;
    addr = new QHostAddress(ThisHostIpAddress);
    
    if(!socket->bind(*addr, port)){
        qDebug() << "I cannot bind socket with IP: " + ThisHostIpAddress + " and port: " + QString::number(port);
        qDebug() << socket->errorString();
    }
    
    this->socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,8000000);
    
    connect(socket, SIGNAL(readyRead()), this, SLOT(OnDataReceived())); 
    

    but in this way it didn't work.
    So i tried to change the binded ip address as QHostAddress::AnyIPv4:

    // create a QUDP socket
    socket = new QUdpSocket(this);
    port = 1002;
    QHostAddress *addr;
    addr = new QHostAddress(ThisHostIpAddress);
    
    if(!socket->bind(QHostAddress::AnyIPv4, port)){
        qDebug() << "I cannot bind socket with IP: " + ThisHostIpAddress + " and port: " + QString::number(port);
        qDebug() << socket->errorString();
    }
    
    this->socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,8000000);
    
    connect(socket, SIGNAL(readyRead()), this, SLOT(OnDataReceived())); 
    

    In this way, it seems work.. but not in the correct way. I'll explain: if my computer has only one network card, it works! But if you've 2 or more network cards you cannot decide the network card for outgoing the packets but it's always the first one.

    Someone can help me?

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

      The outgoing network interface will be whichever one has the least cost route to the destination IP, or the default gateway if no more specific route exists. Without knowing the destination IP, routing table and interfaces of your machine there's no way to be more specific.

      E 1 Reply Last reply
      0
      • E edoardo.videx

        Hi to everyone!
        I develop my desktop application with MacOS. My application use a udp port: 1002.
        I know that all the unix system has a protected ports below 1024.

        Initially i binded the ip and port in this way:

        // create a QUDP socket
        socket = new QUdpSocket(this);
        port = 1002;
        QHostAddress *addr;
        addr = new QHostAddress(ThisHostIpAddress);
        
        if(!socket->bind(*addr, port)){
            qDebug() << "I cannot bind socket with IP: " + ThisHostIpAddress + " and port: " + QString::number(port);
            qDebug() << socket->errorString();
        }
        
        this->socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,8000000);
        
        connect(socket, SIGNAL(readyRead()), this, SLOT(OnDataReceived())); 
        

        but in this way it didn't work.
        So i tried to change the binded ip address as QHostAddress::AnyIPv4:

        // create a QUDP socket
        socket = new QUdpSocket(this);
        port = 1002;
        QHostAddress *addr;
        addr = new QHostAddress(ThisHostIpAddress);
        
        if(!socket->bind(QHostAddress::AnyIPv4, port)){
            qDebug() << "I cannot bind socket with IP: " + ThisHostIpAddress + " and port: " + QString::number(port);
            qDebug() << socket->errorString();
        }
        
        this->socket->setSocketOption(QAbstractSocket::ReceiveBufferSizeSocketOption,8000000);
        
        connect(socket, SIGNAL(readyRead()), this, SLOT(OnDataReceived())); 
        

        In this way, it seems work.. but not in the correct way. I'll explain: if my computer has only one network card, it works! But if you've 2 or more network cards you cannot decide the network card for outgoing the packets but it's always the first one.

        Someone can help me?

        J.HilkJ Offline
        J.HilkJ Offline
        J.Hilk
        Moderators
        wrote on last edited by
        #3

        @edoardo-videx said in Problem with QUdpSocket bind with port lower than 1024:

        I know that all the unix system has a protected ports below 1024.

        and what do you think MacOS is ?


        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


        Q: What's that?
        A: It's blue light.
        Q: What does it do?
        A: It turns blue.

        E 1 Reply Last reply
        1
        • J.HilkJ J.Hilk

          @edoardo-videx said in Problem with QUdpSocket bind with port lower than 1024:

          I know that all the unix system has a protected ports below 1024.

          and what do you think MacOS is ?

          E Offline
          E Offline
          edoardo.videx
          wrote on last edited by
          #4

          @J-Hilk macOS is based on unix.. but i need to know if exist a workaround..

          JonBJ 1 Reply Last reply
          0
          • E edoardo.videx

            @J-Hilk macOS is based on unix.. but i need to know if exist a workaround..

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #5

            @edoardo-videx
            The "workaround" is why do you want to use a port lower than 1024

            My application use a udp port: 1002.

            Why? Change it to use > 1024....

            E 1 Reply Last reply
            0
            • JonBJ JonB

              @edoardo-videx
              The "workaround" is why do you want to use a port lower than 1024

              My application use a udp port: 1002.

              Why? Change it to use > 1024....

              E Offline
              E Offline
              edoardo.videx
              wrote on last edited by
              #6

              @JonB I cannot change the port. But anyway if, for example, i want to do a custom HTTP server that use the port 80, is it not possible? It's really absurd.

              Christian EhrlicherC 1 Reply Last reply
              0
              • C ChrisW67

                The outgoing network interface will be whichever one has the least cost route to the destination IP, or the default gateway if no more specific route exists. Without knowing the destination IP, routing table and interfaces of your machine there's no way to be more specific.

                E Offline
                E Offline
                edoardo.videx
                wrote on last edited by
                #7

                @ChrisW67 i've problem when i send a broadcast packets (255.255.255.255) in this case i cannot select the network card but MacOS choose for me

                1 Reply Last reply
                0
                • E edoardo.videx

                  @JonB I cannot change the port. But anyway if, for example, i want to do a custom HTTP server that use the port 80, is it not possible? It's really absurd.

                  Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @edoardo-videx said in Problem with QUdpSocket bind with port lower than 1024:

                  It's really absurd.

                  All ports below 1024 are meant to be used with special rights. If you program don't have these rights you can't use them. Don't see anything absurd here...

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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