Results 1 to 3 of 3

Thread: QUdpSocket connectToHost and bind an the same port

  1. #1
    Join Date
    May 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QUdpSocket connectToHost and bind an the same port

    Hi!
    I try to connect to a host using broadcast and bind the connected port to receive a message.

    With Qt3 QSocketDevice it worked fine doing this:
    Qt Code:
    1. address.setAddress( "255.255.255.255" );
    2. socket->setAddressReusable( true );
    3. socket->connect( address, 8080 );
    4.  
    5.  
    6. address.setAddress( "0.0.0.0" );
    7. socket2->setBlocking( false );
    8. socket2->setAddressReusable( true );
    9. socket2->bind( address, socket->port() );
    10. const char *msg = "my_message";
    11. socket->writeBlock( msg, strlen(msg) );
    To copy to clipboard, switch view to plain text mode 

    I tried with QUdpSocket
    Qt Code:
    1. address.setAddress( "255.255.255.255" );
    2. socket->connectToHost( address, 8080 );
    3.  
    4. address.setAddress( "0.0.0.0" );
    5.  
    6. printf("bind: %i\n",socket2->bind( socket->localPort(),QUdpSocket::ReuseAddressHint|QUdpSocket::ShareAddress ));
    7. printf("error %i: %s\n",socket2->error(),socket2->errorString().ascii());
    8.  
    9. const char *msg = "my_message";
    10. socket->write( msg, strlen(msg) );
    To copy to clipboard, switch view to plain text mode 
    I cannot bind the receiving socket.
    I tried it on windows:
    "error 3: The address is protected."
    and on linux:
    "error 8: The bound address is already in use"


    Any ideas how to get this to work?

  2. #2
    Join Date
    May 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket connectToHost and bind an the same port

    Qt Code:
    1. socket->connectToHost( QHostAddress::Broadcast, 8080 );
    2. int port = socket->localPort();
    3. const char *msg = "my_message";
    4. socket->write( msg, strlen(msg) );
    5. socket->disconnectFromHost();
    6. socket2->bind( port, QUdpSocket::ReuseAddressHint|QUdpSocket::ShareAddress );
    To copy to clipboard, switch view to plain text mode 
    This works on linux now.
    but on Windows (Vista) i still get a timeout in socket2->waitForReadyRead(5000)

  3. #3
    Join Date
    May 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QUdpSocket connectToHost and bind an the same port

    It seems to be a problem with Windows Vista. I tried it with XP and it worked.
    But on Vista the broadcast message is not being sent (according to wireshark).


    Edit:
    Using the QHostAddress "224.0.0.1" (multicast) solved my problem
    Last edited by aLiEnHeAd; 9th September 2010 at 14:38.

Similar Threads

  1. QTcpSocket::connectToHost
    By yogeshgokul in forum Qt Programming
    Replies: 18
    Last Post: 7th July 2009, 11:42
  2. QUdpSocket -- Rebinding with Different Port Numbers
    By swamyonline in forum Qt Programming
    Replies: 0
    Last Post: 22nd January 2009, 14:39
  3. Timing out a QFtp ConnectToHost
    By skp in forum Qt Programming
    Replies: 10
    Last Post: 27th April 2008, 14:10
  4. QUdpSocket bind / connectToHost ?
    By mnemonic_fx in forum Newbie
    Replies: 2
    Last Post: 17th July 2007, 04:20
  5. QTcpSocket connectToHost memory leak?
    By Lele in forum Qt Programming
    Replies: 9
    Last Post: 6th March 2006, 10:26

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.