PDA

View Full Version : UDP Network. receiving pakets



muhuhn
5th April 2015, 14:37
I am trying to write a programm that communicates with a server over the network. the server implementation is given, i can not change it.

the servers IP address is the 141.47.69.35 and the server aplication listens to port 11111. the server answers to the source port of the request. as a protocoll udp is used. the example code works fine for transmitting data to the server. with a network sniffer i can see that the server responds correcly. but the function readPendingDatagrams() is never called. i gues there is anything wrong with the ports, but i really have no more idea...


#include "cmyudp.h"
#include "iostream"

using namespace std;

MyUDP::MyUDP(QObject *parent) : QObject(parent)
{
// initialize port
port = 11111;

// create a QUDP socket
udpReceiveSocket = new QUdpSocket(this);

// creat host ip
hostIp = new QHostAddress("141.47.69.35");

// bind socket
udpReceiveSocket->connectToHost(*hostIp, port, QIODevice::ReadWrite);

// setup receiver calllback function
connect(udpReceiveSocket, SIGNAL(readyRead()), this, SLOT(readPendingDatagrams()))

}

void MyUDP::HelloUDP()
{
// create send data
QByteArray Data;
Data.append("Hallo Hilfe!");
udpReceiveSocket->write(Data);
}

void MyUDP::readPendingDatagrams()
{
cout << "received!" << endl;
while (udpReceiveSocket->hasPendingDatagrams()) {
cout << "received!" << endl;
}
}

anda_skoa
5th April 2015, 15:27
You are missing the bind() call.

Cheers,
_

muhuhn
5th April 2015, 15:53
Thanks for your response.
Is it possible to mix bind() and connectToHost()? As i understood either the one or the other should be used. Where is the bind() missing? i also tryed solutions only with bind() and the writeDatagram() functions. the result was the same: receiving a paket didn't work.

anda_skoa
5th April 2015, 22:02
bind() is needed if you want your UDP socket to listen for data.

connectToHost() is just a convenience for not having to specifiy the receiver when sending packets. I doubt it would conflict with bind().

"Connect" doesn't have any inherent meaning for UDP, the function is in QUdpSocket because it is in its base class and the developers decided to make it do something useful (allowing to you use write without specifying the receipient) instead of it just doing nothing (or generating an error),

Cheers,
_

muhuhn
6th April 2015, 06:48
OK, i think i got this. How can i bind a socket to the port that is used as a source port for transmitting? The transmitter picks its source port randomly, thats fine so far. But how do i tell the socket that it has to listen to that randomly choosen port? It would not be a Problem if the Server response would go to a defined port at client side. But i have to listen to a port that i actually don't know when calling bind().

Thanks for your help.

anda_skoa
6th April 2015, 08:53
Just to be sure: have you checked if binding to a port makes this also the port used for sending?

If not, you could try connectToHost() and then accessing localPort() for the value to bind()

Cheers,
_