PDA

View Full Version : udpsocket cannot bind,



saman_artorious
23rd October 2013, 13:11
I am running a udpsocket client program which writes to and reads from server. when I run it, bind() returns false. Although I can still send data but I am wondering why sockets cannot bind and trigger readState slot.

As I checked a udp client does not bind, so I only set the port when initialized it. later I write like this:



void UDP::UDPInit(int port)
{
socketPort = port;
udpsocket = new QUdpSocket(this);
}




void UDP::sendCommand(QByteArray data)
{
if(udpsocket->writeDatagram(data.data(),QHostAddress(ip),socketP ort)==-1)
emit clientLogMessage(QString("UDPCLIENT : Write problem !"));
else
udpsocket->flush();

// QByteArray datagram;
// int m = udpsocket->read(datagram.data(), 11);
// qDebug() << errno;
// qDebug() << m;

while (!udpsocket->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udpsocket->pendingDatagramSize());
qDebug() << udpsocket->pendingDatagramSize();
udpsocket->readDatagram(datagram.data(), datagram.size());
emit dataReceived(datagram);
}
}

but the result is -1 and it does not read anything from it. WHY? how can I solve this problem?

ChrisW67
24th October 2013, 01:40
Your code extracts do not call bind(): you will not receive if you do not call bind.
What "result" is -1?

writeDatagram() expects either a QByteArray, address, and port OR a char* buffer, size, address, and port. Which do you think you are doing? Are you ignoring a compiler warning?

Your code starting at line 13 says, "While I have no pending datagrams try to read a datagram." Does that sound right to you?

saman_artorious
26th October 2013, 11:13
Your code extracts do not call bind(): you will not receive if you do not call bind.

I added bind(), but still it returns false! I can only bind my own ip address and not the other machines.

What "result" is -1?

writeDatagram() expects either a QByteArray, address, and port OR a char* buffer, size, address, and port. Which do you think you are doing? Are you ignoring a compiler warning?

no I am not missing any compiler warning. I write char* buffer as the first buffer to writeDatagram(), write is successful, the other machine receives packets successfully.


Your code starting at line 13 says, "While I have no pending datagrams try to read a datagram." Does that sound right to you?

Yes, you are right, I shall omit ! sign.

Here is my final code but still I bind returns false and I cannot receive any packets! (my ip is 100.147 and the machine I am connecting is 100.143)



#include "UDP.h"

UDP::UDP(QObject *parent) :
QObject(parent)
{

}
//=======================================
void UDP::UDPInit(int port)
{
socketPort = port;
host = new QHostAddress("192.168.100.143");
myPort = 11000;
udpsocket = new QUdpSocket(this);

bool res = udpsocket->bind(QHostAddress("192.168.100.143"),myPort);
connect(udpsocket,SIGNAL(connected()),this,SLOT(so cketConnected()));
connect(udpsocket, SIGNAL(readyRead()), this, SLOT(readSocket()));
}
//=======================================
void UDP::socketError( QAbstractSocket::SocketError )
{
qDebug() << "UDPCLIENT ERROR: "<< udpsocket->errorString();
}

//========================================
void UDP::socketConnected()
{
qDebug() << "UDPCLIENT : Socket connected!";
emit clientLogMessage(QString("UDPCLIENT : Connected !"));
}


void UDP::sendCommand(QByteArray data)
{
if(udpsocket->writeDatagram(data.data(),QHostAddress(ip),socketP ort)==-1)
emit clientLogMessage(QString("UDPCLIENT : Write problem !"));
else
udpsocket->flush();
}

void UDP::readSocket()
{
while (udpsocket->hasPendingDatagrams())
{
QByteArray datagram;
datagram.resize(udpsocket->pendingDatagramSize());
qDebug() << udpsocket->pendingDatagramSize();
udpsocket->readDatagram(datagram.data(), datagram.size(), host, &myPort);
emit dataReceived(datagram);


qDebug() << datagram.toHex();
}
}

ChrisW67
26th October 2013, 12:22
Your bind() call is asking to receive packets at port 11000 on the other machine. That will be difficult for this machine to do.

You are still sending writeDatagram() invalid arguments.

saman_artorious
26th October 2013, 12:33
Your bind() call is asking to receive packets at port 11000 on the other machine. That will be difficult for this machine to do.

You are still sending writeDatagram() invalid arguments.

Do you mean I shall try other ports to find a suitable one? I also tried 1234 but it failed. I need more explanation is needed please.
About the datagram the prototype is as following:
qint64 QUdpSocket::writeDatagram(const QByteArray & datagram, const QHostAddress & host, quint16 port)
and
qint64 QUdpSocket::writeDatagram(const char * data, qint64 size, const QHostAddress & address, quint16 port)

well this time I used used the first prototype instead of the second:



if(udpsocket->writeDatagram(data,QHostAddress(ip),myPort)==-1)


Edit: I tested the program with different port addresses, still I cannot bind() to server, though I can write packet to server successfully, with not packets receipt from the other side.

listen here please: I change the ip and bind() to my own machine, in this case I can now read from the other machine, but I cannot write to it anymore. I tried to define two sockets, one for writing and one for reading, but still I cannot write to the other machine. I am confused totally, don't know how to solve this.

anda_skoa
26th October 2013, 15:04
Can you post an update code?

Check that the address you use for bind is one of your local machine and the one you use for sending is one of the remote machine.

Cheers,
_

ChrisW67
26th October 2013, 21:19
What is the value of the variable 'ip'? How is that related to the value of variable 'host' or the remote host? Where is it defined and set?

Your original code was close... One socket can handle the whole conversation.

saman_artorious
27th October 2013, 10:45
Can you post an update code?

Check that the address you use for bind is one of your local machine and the one you use for sending is one of the remote machine.

Cheers,
_

Yes, you are right, I mentioned the server address in bind(), I shoudav mentioned the local machine address instead. And as chris said, we I needed bind() in order to activate the ReadyRead() signal to receive packets.

Cheers,