PDA

View Full Version : Working with QUdpSocket



csvivek
16th October 2008, 06:44
Hi,

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");
else
qDebug("Bind Fail");



kindly help me..
Thanks in advance.

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

if(m_udpsocket->bind(port))
qDebug("Bind Success");
else
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()));

csvivek
16th October 2008, 08: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.

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

csvivek
16th October 2008, 08: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.

mhoover
30th May 2009, 04: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.

MattPhillips
30th October 2009, 02: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.

Matt