[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?
Thanks
-
"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.
-
[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. -
!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
-
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.
-
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.
-
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
-
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.