Nominate our 2022 Qt Champions!

[Solved] QUdpSocket - create a connection

  • When i open a UDP connection i define the device IP and the Port of the device B, so the device A send a message to device B using the IP and Port that i did set. The device A open a Port too for incoming messages, how can i define this Port or it will always be randomly arbitrated?


  • "QUdpSocket::bind(const QHostAddress& address, quint16 port)":

    bq. bool QUdpSocket::bind(const QHostAddress& address, quint16 port)
    Binds this socket to the address address and the port port. When bound, the signal readyRead() is emitted whenever a UDP datagram arrives on the specified address and port. This function is useful to write UDP servers.

    Lest we forget to nitpick: you cannot create an UDP "connection". UDP is stateless and connectionless, just several datagrams sent from one endpoint to another. If you need a "connection", stateful, with flow control and sequencing you will have to use TCP.

  • [quote author="Lukas Geyer" date="1314027251"]several datagrams sent from one endpoint to another. If you need a "connection", stateful, with flow control and sequencing you will have to use TCP.[/quote]
    In the context of QUdpSocket, a connection has a different meaning, according to the documentation:

    [quote]With QUdpSocket, you can also establish a virtual connection to a UDP server using connectToHost() and then use read() and write() to exchange datagrams without specifying the receiver for each datagram.[/quote]
    It is basically just setting the default receiver address and port for datagrams to be send.

  • !!

    The first 6 messages that you can see in the wireshark screenshot is udp messages from a qt aplication to a device in the network and the device receive the "lol" messages.
    The device is configure to receive udp messages in port 2000 and send to port 2001.

    I send the messages No. 7, 10 and 11, close the aplication, open again and send the No. 23, 24 and 25 and you can see that the source port changes from 52541 to 36020.

    After i tried to send something from the device ( to the qt aplication ( but it send to the port 2001 however the port is 36020 so it give me an unreachable message.

    The code that i'm using is:

    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    #include <QDebug>
    #include <QtNetwork>

    MainWindow::MainWindow(QWidget *parent) :
    ui(new Ui::MainWindow)

    socket = new QUdpSocket;
    socket->connectToHost("", 2000);
    connect(socket, SIGNAL(readyRead()), this, SLOT(teste()));
    if (socket->state()==QUdpSocket::ConnectedState)
        qDebug() << "Connected";


    delete ui;

    void MainWindow::on_pushButton_clicked()

    void MainWindow::sendKey(QString a){
    qDebug()<<"comando: "+a;
    QString s(a);

    void MainWindow::teste(){
    qDebug()<<"aqui" << socket->readAll();

    Maybe is some mistake in my knowledge about networks :s


  • So... you want to control the port that is used for sending an UDP message? AFAIK, that doesn't work. A port is selected at random (or some other algorithm that you can't influence). And why would you want to influence it? The interesting port is the port the application listens on, not the port it sends from.

  • So, how can i define the port where the aplication is listening?

    I had the bind declaration after connectToHost and it gives me this error: QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState but i already correct it.

  • You already do that: using bind().

  • You cannot use the same code for the server and the client.

    The server binds to a specfic port and waits for clients to "connect".
    The client "connects" to a server using the address and port specified in the servers code.

    Your application can be server and client at the same time, but that's still two different pieces of code.

    See the "Broadcast Sender": and "Broadcast Receiver": example.

  • The aplication is comunicating with a module like this:
    The problem is that i need to set the ports:
    "Set up the protocol and port number
    set ip proto 1 // enable UDP as the protocol
    set ip host <ip address> // set the IP address of remote host
    set ip remote <port> // set the remote port number on which the host is listening
    set ip local <port> // set the port number on which the WiFly module will listen
    save // saves the settings in config file
    reboot // reboots the module so that the above settings take effect"

    And the bind is not working :s

  • The board is sending UDP datagrams to your application, right?

    So your application has to use bind() to the port specified in the set ip remote <port> line. Keep in mind, that you cannot / should not use ports < 1024 and ports that are already taken.

    Take a look at the Broadcast Receiver example, it does exactly the same.

  • i create another socket:
    @ socketIn = new QUdpSocket;
    connect(socketIn, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));@

    and now it is working :D

    Thanks all ;)

  • You're welcome! If the problem is solved for you feel to edit the title of your inital post to '[Solved] QUdpSocket ...' so it is indicated as such.

Log in to reply