Results 1 to 5 of 5

Thread: QSocketDevice and udp

  1. #1
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default QSocketDevice and udp

    Hello!
    I have application containing server and client side. Server broadcasts udp packets on specific port and listen to specific tcp port. Client listens to server's udp packets , process 'em (determine ip address, for example) and connects to server via tcp.

    I've wrote udp interaction like this:
    Qt Code:
    1. /*server*/
    2. /*initialization*/
    3. QSocketDevice * udp = new QSocketDevice(QSocketDevice::Datagram);
    4. if(!udp->bind(some-address, some-port))
    5. { ... }
    6. #ifdef QTS_WIN32
    7. #elif QTS_LINUX
    8. int one = 1;
    9. setsockopt(udp->socket(), SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
    10. #endif
    11.  
    12. /*timer loop, 3 sec*/
    13. udp->writeBlock(block, block-length, broadcast-address, some-port);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. /*client*/
    2. /*initialization*/
    3. QSocketDevice * udp = new QSocketDevice(QSocketDevice::Datagram);
    4. if(!udp->bind(some-other-address, some-port))
    5. { ... }
    6. #ifdef QTS_WIN32
    7. #elif QTS_LINUX
    8. int one = 1;
    9. setsockopt(udp->socket(), SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
    10. #endif
    11.  
    12. QSocketNotifier * udpn = new QSocketNotifier(udp->socket(), QSocketNotifier::Read);
    13. connect( udpn, SIGNAL( activated(int) ), this, SLOT( receiveSomething(int) ) );
    14.  
    15. /*void reciveSomething(int)*/
    16. QByteArray b = udp->readAll();
    17. /*doing something with QString(b)*/
    To copy to clipboard, switch view to plain text mode 

    It should provide fllowing possibilities:
    1. when client starts it can locate and connect to server.
    2. when server goes down and then up, clients can to re-locate and re-connect to it

    In fact, only possibility (1) works. Each client instance receive only few boradcast packets, despite server sends them all the time.

    Why do client cease to receive udp packets?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSocketDevice and udp

    Probably the server can't bind to the port again because the socket didn't use the REUSE option and kernel is forbidding anyone to bind to the same port again for some time. Do you actually check that bind is successful?

  3. #3
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QSocketDevice and udp

    Quote Originally Posted by wysota View Post
    Do you actually check that bind is successful?
    Sure. There is qFatal actually instead of dots.
    There are also qDebugs in timer loop and receiveSomething procs and according to them server don't stop to send packets but client ceases to receive them after second one.

    All packets contains same data. Can QSocketDevice cache 'em and cease to receive new as they are exactly the same as old ones?

    And I found one solution: in case client udp QSocketDevice would be recreated after each "server locating" along with QSocketNotify, the program would work fine.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSocketDevice and udp

    Quote Originally Posted by a550ee View Post
    All packets contains same data. Can QSocketDevice cache 'em and cease to receive new as they are exactly the same as old ones?
    Not likely. UDP doesn't discard duplicates.

    And I found one solution: in case client udp QSocketDevice would be recreated after each "server locating" along with QSocketNotify, the program would work fine.
    What happens with the Qt examples related to UDP if you kill the server and restart it again?

  5. #5
    Join Date
    Feb 2006
    Posts
    22
    Thanks
    4
    Qt products
    Qt3
    Platforms
    Unix/X11

    Default Re: QSocketDevice and udp

    What qt examples related to udp? It's Qt-3 and I only sample I found is a opensource network clipboard from some qt enthusiast.

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.