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. [Solved] QUdpSocket - create a connection
Forum Updated to NodeBB v4.3 + New Features

[Solved] QUdpSocket - create a connection

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 34.5k 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
    metRo_
    wrote on 22 Aug 2011, 15:09 last edited by
    #1

    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?

    Thanks

    1 Reply Last reply
    0
    • L Offline
      L Offline
      lgeyer
      wrote on 22 Aug 2011, 15:34 last edited by
      #2

      "QUdpSocket::bind(const QHostAddress& address, quint16 port)":http://doc.qt.nokia.com/4.7/qudpsocket.html#bind

      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.

      1 Reply Last reply
      0
      • A Offline
        A Offline
        andre
        wrote on 22 Aug 2011, 15:51 last edited by
        #3

        [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.

        1 Reply Last reply
        1
        • M Offline
          M Offline
          metRo_
          wrote on 22 Aug 2011, 17:23 last edited by
          #4

          !http://dl.dropbox.com/u/6416035/wireshark.png(QUdpSocket)!

          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 (192.168.1.79) to the qt aplication (192.168.1.66) 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) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
          {
          ui->setupUi(this);

          socket = new QUdpSocket;
          socket->connectToHost("192.168.1.79", 2000);
          socket->bind(2001);
          socket->waitForConnected(1000);
          connect(socket, SIGNAL(readyRead()), this, SLOT(teste()));
          if (socket->state()==QUdpSocket::ConnectedState)
              qDebug() << "Connected";
          else
              exit(-1);
          

          }

          MainWindow::~MainWindow()
          {
          delete ui;
          }

          void MainWindow::on_pushButton_clicked()
          {
          sendKey("lol");
          }

          void MainWindow::sendKey(QString a){
          qDebug()<<"comando: "+a;
          QString s(a);
          socket->write(s.toAscii());
          }

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

          Maybe is some mistake in my knowledge about networks :s

          Thanks

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on 22 Aug 2011, 17:57 last edited by
            #5

            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.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              metRo_
              wrote on 22 Aug 2011, 18:01 last edited by
              #6

              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.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andre
                wrote on 22 Aug 2011, 18:08 last edited by
                #7

                You already do that: using bind().

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on 22 Aug 2011, 18:13 last edited by
                  #8

                  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":http://doc.qt.nokia.com/4.7/network-broadcastsender.html and "Broadcast Receiver":http://doc.qt.nokia.com/4.7/network-broadcastreceiver.html example.

                  1 Reply Last reply
                  0
                  • M Offline
                    M Offline
                    metRo_
                    wrote on 22 Aug 2011, 18:41 last edited by
                    #9

                    The aplication is comunicating with a module like this: http://www.rovingnetworks.com/rn-174.php
                    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

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on 22 Aug 2011, 18:59 last edited by
                      #10

                      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.

                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        metRo_
                        wrote on 22 Aug 2011, 19:07 last edited by
                        #11

                        i create another socket:
                        @ socketIn = new QUdpSocket;
                        socketIn->bind(2001,QUdpSocket::ShareAddress);
                        connect(socketIn, SIGNAL(readyRead()),this, SLOT(processPendingDatagrams()));@

                        and now it is working :D

                        Thanks all ;)

                        1 Reply Last reply
                        0
                        • L Offline
                          L Offline
                          lgeyer
                          wrote on 22 Aug 2011, 19:47 last edited by
                          #12

                          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.

                          1 Reply Last reply
                          0

                          1/12

                          22 Aug 2011, 15:09

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved