Results 1 to 2 of 2

Thread: connectToHost() to receive broadcast udp datagram

  1. #1
    Join Date
    May 2009
    Posts
    75
    Thanks
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question connectToHost() to receive broadcast udp datagram

    Can I use QAbstractSocket::connectToHost() to receive broadcast udp datagrams?

    If I try the unmodified broadcastsender/receiver all works and netstat is:
    Qt Code:
    1. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    2. udp6 0 0 :::52337 :::* 12185/./broadcastse
    3. udp6 0 0 :::45454 :::* 12172/broadcastrece
    To copy to clipboard, switch view to plain text mode 

    I modified broadcastreceiver as follow:
    Qt Code:
    1. //! [0]
    2. udpSocket = new QUdpSocket(this);
    3. udpSocket->bind(45454, QUdpSocket::ShareAddress);
    4. udpSocket->connectToHost(QHostAddress(QHostAddress::Any),0); // <- added line
    5. //! [0]
    To copy to clipboard, switch view to plain text mode 

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

    the netstat command report is:
    Qt Code:
    1. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
    2. udp6 0 0 ::1:45454 ::1:* ESTABLISHED 11488/broadcastrece
    To copy to clipboard, switch view to plain text mode 

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

    here is attached the examples
    qt5-examples.tar.gz

    best regards
    Max
    Last edited by mastupristi; 15th March 2017 at 10:10.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: connectToHost() to receive broadcast udp datagram

    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().
    Last edited by Santosh Reddy; 21st March 2017 at 08:51.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 1
    Last Post: 11th March 2016, 03:16
  2. QUdpsocket send and receive broadcast in linux
    By danics in forum Qt Programming
    Replies: 5
    Last Post: 4th February 2015, 12:26
  3. Reading last datagram in UDP network
    By euch in forum Qt Programming
    Replies: 5
    Last Post: 6th December 2010, 00:21
  4. Reading TCP datagram header by Qt
    By Kill3rReaper in forum Qt Programming
    Replies: 11
    Last Post: 8th June 2010, 13:49
  5. UDP datagram receive
    By mdannenb in forum Qt Programming
    Replies: 8
    Last Post: 27th July 2008, 04:30

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.