PDA

View Full Version : connectToHost() to receive broadcast udp datagram



mastupristi
15th March 2017, 09:29
Can I use QAbstractSocket::connectToHost() to receive broadcast udp datagrams?

If I try the unmodified broadcastsender/receiver all works and netstat is:

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp6 0 0 :::52337 :::* 12185/./broadcastse
udp6 0 0 :::45454 :::* 12172/broadcastrece


I modified broadcastreceiver as follow:

//! [0]
udpSocket = new QUdpSocket(this);
udpSocket->bind(45454, QUdpSocket::ShareAddress);
udpSocket->connectToHost(QHostAddress(QHostAddress::Any),0); // <- added line
//! [0]


Now it does not receive broadcast datagrams, but it receive correctly unicast datagrams.

the netstat command report is:

Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
udp6 0 0 ::1:45454 ::1:* ESTABLISHED 11488/broadcastrece


I wonder why it seems different local address, and where am I wrong.

here is attached the examples
12387

best regards
Max

Santosh Reddy
21st March 2017, 08:46
UDP is normally used is connection-less mode, so connectToHost() is not required, unless you are doing something special.

If you are sure you want to connect, specify a specific remote host name / remote ip address you want to connect. QHostAddress::Any is not an appropriate argument to connectToHost().