View Full Version : Working with QUdpSocket

16th October 2008, 05:44

I have a udpserver that is waiting for a data from a client to be connected and this server is written in C. Now i have a client who can send and receive data to this server. This client is coded with QT. I am able to send data to this server thru the writeDatagram() cal. but to recieve the data. I have used the readyRead() signal. but the slot connected is not at all getting called.

I have not binded the object of QUdpSocket to the corresponding port or IP at the client side.
I thought this would be the problem but when i try to bind the object. Bind is getting failed. when both the client and server are running in the same pc. i have also tried with different pc's where the bind is a success and the client server are working fine.

Please tell me a way where i can make the binding success when both the client and server are running the same PC.
I tried using the following code but still the problem exists with same PC.

if(m_udpsocket->bind(port,QUdpSocket::ReuseAddressHint|QUdpSocket: :ShareAddress))
qDebug("Bind Success");
qDebug("Bind Fail");

kindly help me..
Thanks in advance.

16th October 2008, 06:57
first, leave the BindFlags away. I dont know if they arent causing the error.

qDebug("Bind Success");
qDebug("Bind Fail");
and second you should compile your app with CONFIG += console to see if theres an error with your connect method. your method should look like that:

connect(udpsocket, SIGNAL(readyRead()),
this, SLOT(readDatagram()));

16th October 2008, 07:25
No i am not getting that kind of errors with the connect statement.
CONFIG += console is added.

The slot is called when both the server and client runs in different pc's. but since the bind is failed the udpsocket is in an unconnected state and hence the slot is not getting called.

when they are run in different pc's the socket is in boundstate.

16th October 2008, 07:36
what do QUdpSocket:errorString() and QUdpSocket::error() report?

16th October 2008, 07:40
The Error string was QAbstractSocket::AddressInUseError.
But the problem is solved in the server side i tried to use

setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))

it worked. Thanks everybody for the responses and time.

30th May 2009, 03:43
I had this happen when I had one socket sending one way and another socket sending another. A lot of copy/pasting was going on and I forgot to update one of the methods to use the commandSocket instead of the dataSocket.

Hope that helps somebody.

30th October 2009, 01:38
Hey Caudel,

Thanks for the errorString() tip--I didn't know about that, it's helped me get closer to a solution to my problem.