PDA

View Full Version : QUdpSocket connectToHost and bind an the same port



aLiEnHeAd
7th September 2010, 11:14
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:


address.setAddress( "255.255.255.255" );
socket->setAddressReusable( true );
socket->connect( address, 8080 );


address.setAddress( "0.0.0.0" );
socket2->setBlocking( false );
socket2->setAddressReusable( true );
socket2->bind( address, socket->port() );
const char *msg = "my_message";
socket->writeBlock( msg, strlen(msg) );


I tried with QUdpSocket


address.setAddress( "255.255.255.255" );
socket->connectToHost( address, 8080 );

address.setAddress( "0.0.0.0" );

printf("bind: %i\n",socket2->bind( socket->localPort(),QUdpSocket::ReuseAddressHint|QUdpSocke t::ShareAddress ));
printf("error %i: %s\n",socket2->error(),socket2->errorString().ascii());

const char *msg = "my_message";
socket->write( msg, strlen(msg) );

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?

aLiEnHeAd
8th September 2010, 15:26
socket->connectToHost( QHostAddress::Broadcast, 8080 );
int port = socket->localPort();
const char *msg = "my_message";
socket->write( msg, strlen(msg) );
socket->disconnectFromHost();
socket2->bind( port, QUdpSocket::ReuseAddressHint|QUdpSocket::ShareAddr ess );

This works on linux now.
but on Windows (Vista) i still get a timeout in socket2->waitForReadyRead(5000)

aLiEnHeAd
9th September 2010, 07:48
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). :confused:


Edit:
Using the QHostAddress "224.0.0.1" (multicast) solved my problem