PDA

View Full Version : Specify source port on QUdpSocket packet



gio0166
21st March 2011, 21:29
Dear QT developers

I am a kind of newbie for QT. Since sometime I developed a code/ui that communicates with some I/O devices through a UDP protocol. So I am able to communicate but the problem is that the UDP packet should be sent through a specific port because the IO device is checking the source port and the source address of the coming packet I guess through the header.

I tried all the solutions around (binding first) but whatever I tried it didn't worked as I see with wireshark. Any help is much appreciated because I need it urgently.

I attach a piece of code that I am using to send.



void MainWindow::Sender(QByteArray blockOfData, quint16 port)
{
QString address = ui->baseAddressTF->text();
qDebug() << "Send + Receive";
socket = new QUdpSocket( this );
socket->bind(QHostAddress("127.0.0.1"), 6007);
socket->writeDatagram(blockOfData,QHostAddress(address), port);
qDebug() << socket->errorString();
}


Many Many thanks in advance

And to be more specific I want to send a packet to ip:10.0.0.2, port 6263 but through port 6007 and now I can't really seeing through wireshark that the source port is always a random port.

Thanks again

wysota
22nd March 2011, 09:46
What does bind() return?

gio0166
22nd March 2011, 11:32
Guys I think I found the problem . On mac os x port 6007 is used by x11 so always the bind is giving false. I tried else and it's finally working.

"wysota" thanks for the hint

fgiana
4th October 2011, 02:14
Hello,
What you have to do is modify the line:
socket-> bind (QHostAddress ("IP_NETWORK_CARD"), 6007);
IP_NETWORK_CARD and replace the IP address you have configured the NIC that is connected to the network.
example:
socket-> bind (QHostAddress ("192.168.1.101"), 6007);

sorry my English is not very good.