PDA

View Full Version : QSocketDevice and udp



a550ee
4th October 2006, 12:40
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:


/*server*/
/*initialization*/
QSocketDevice * udp = new QSocketDevice(QSocketDevice::Datagram);
if(!udp->bind(some-address, some-port))
{ ... }
#ifdef QTS_WIN32
#elif QTS_LINUX
int one = 1;
setsockopt(udp->socket(), SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
#endif

/*timer loop, 3 sec*/
udp->writeBlock(block, block-length, broadcast-address, some-port);




/*client*/
/*initialization*/
QSocketDevice * udp = new QSocketDevice(QSocketDevice::Datagram);
if(!udp->bind(some-other-address, some-port))
{ ... }
#ifdef QTS_WIN32
#elif QTS_LINUX
int one = 1;
setsockopt(udp->socket(), SOL_SOCKET, SO_BROADCAST, &one, sizeof(one));
#endif

QSocketNotifier * udpn = new QSocketNotifier(udp->socket(), QSocketNotifier::Read);
connect( udpn, SIGNAL( activated(int) ), this, SLOT( receiveSomething(int) ) );

/*void reciveSomething(int)*/
QByteArray b = udp->readAll();
/*doing something with QString(b)*/


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?

wysota
4th October 2006, 16:55
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?

a550ee
4th October 2006, 17:07
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.

wysota
4th October 2006, 17:16
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?

a550ee
4th October 2006, 17:27
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.