Results 1 to 3 of 3

Thread: How to destroy a UdpSocket completely

  1. #1
    Join Date
    Oct 2006
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default How to destroy a UdpSocket completely

    I create a UdpSocket and work well, then destroy it.
    However, when i create it again, my program does not work.
    Please give me some advice.

    Qt Code:
    1. void GPSNetwork::startReceive()
    2. {
    3. stop = false;
    4. udpSocketRead = new QUdpSocket();
    5. udpSocketRead->bind(45454);
    6. connect(udpSocketRead, SIGNAL(readyRead()),this, SLOT(start()));
    7. }
    8.  
    9. void GPSNetwork::run()
    10. {
    11. while ((udpSocketRead->hasPendingDatagrams())&&(stop==false))
    12. {
    13. QByteArray datagram;
    14. datagram.resize(udpSocketRead->pendingDatagramSize());
    15. udpSocketRead->readDatagram(datagram.data(),datagram.size());
    16. if (datagram.size()!=0)
    17. emit msg(datagram.data());
    18. }
    19. }
    20.  
    21. void GPSNetwork::stopReceiveFromNetwork()
    22. {
    23. stop=true;
    24. disconnect(udpSocketRead, 0,this,0);
    25. delete udpSocketRead;
    26. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 21st October 2006 at 08:17. Reason: missing [code] tags

  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: How to destroy a UdpSocket completely

    My guess is that the operating system prevents you from binding to a port you just closed. Either don't bind the socket or set proper options to the socket (SO_REUSE if I remember correctly).

  3. #3
    Join Date
    Aug 2006
    Posts
    6
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to destroy a UdpSocket completely

    How about allowing reuse of the socket descriptor? In GPSNetwork::startReceive()

    udpSocketRead = new QUdpSocket();
    // get socket descriptor from the socket
    int fd = udpSocketRead->socketDescriptor();

    // allow multiple sockets to use the same PORT number
    unsigned int yes=1;
    if (setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof (yes)) < 0)
    {
    perror("Reusing ADDR failed");
    exit(1);
    }
    // then bind etc

    Yuriy

Similar Threads

  1. Replies: 1
    Last Post: 24th June 2006, 20:55

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.