PDA

View Full Version : UDP Communication issue



Thixomag
22nd February 2009, 20:52
Hello

I’m making an application with an UDP client/server communication.
The client has to open the 3301 port on his router to receive the socket. (and the server has to open the 3300, but that is not importante)
And I would like to know how I could receive the socket on the PC's client without this opening port.

Server Side :
-Listening

m_udpSocket = new QUdpSocket;
m_udpSocket->bind(QHostAddress::Any, 3300, QUdpSocket::ShareAddress);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(get()));

-Sending

QUdpSocket udpSocket;
udpSocket.bind(3301); // I don't know if that is important
udpSocket.writeDatagram(datagram, ip, 3301);


Client Side :
-Listening

m_serverAdress = new QHostAddress("**.**.***.**");

m_udpSocket = new QUdpSocket;
m_udpSocket->bind(QHostAddress::Any, 3301, QUdpSocket::ShareAddress); /// if I put *m_serverAdress instead of QHostAddress::Any, I receive nothing even with the port openning
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(get()));

-Sending

m_udpSocket->writeDatagram(datagram, *m_serverAdress, 3300);



Thanks You

wysota
23rd February 2009, 09:53
You can't. If it was possible, using firewalls wouldn't make sense because any application could have overriden it. The only thing you could do is to use a port that the firewall passes through like the HTTP port (TCP/80) but you need to have some mechanism that will proxy the traffic or you can set up a VPN connection but one way or another this requires a manual intervention somewhere.

Thixomag
24th February 2009, 11:09
Are you really sure ? Because I heard that it could work with some router.
And if I analyse with Wireshark (a network protocol analyzer) some application like Skype, I see that my PC can receive UDP socket without openning port.

Thanks you for your help

Thixomag
24th February 2009, 13:13
This method works with me and my co-developer :
Client side :

//Ecoute
m_udpSocket = new QUdpSocket;
m_udpSocket->bind(QHostAddress::Any, m_port, QUdpSocket::ShareAddress);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(get()));

// ...

//Envoi
m_udpSocket->writeDatagram(datagram, *m_adresseServeur, m_port);


Serveur side:

//Ecoute
m_udpSocket = new QUdpSocket;
m_udpSocket->bind(QHostAddress::Any, m_port, QUdpSocket::ShareAddress);
connect(m_udpSocket, SIGNAL(readyRead()), this, SLOT(get()));

// ...

//Envoi
m_udpSocket->writeDatagram(datagram, ipCLient, m_port);

You have to use the same port and the same socket to receive and to send.
Ok, problem is clear.

wysota
24th February 2009, 20:43
Hmm? No, you don't have to use the same port, that's a false assumption :)

Thixomag
24th February 2009, 21:05
Ok, but I don't succeed with a lot of other method and test, with two ports. Maybe you know what I have to do in the code to succeed.

wysota
24th February 2009, 21:08
In my opinion this is not a matter of your code but of the router that drops the transmission.

Thixomag
24th February 2009, 21:16
But is it better to use two ports ?

wysota
24th February 2009, 21:18
And this proves... what? Apart from the fact that you didn't bind the socket anywhere, of course...

Thixomag
24th February 2009, 21:21
Yes sorry, I saw and I edited my post ^^

wysota
24th February 2009, 21:38
It's not that it's better or worse. You can do as many ports as you wish and this won't make your transmission to be accepted or dropped by the router (unless of course it is configured to do so, but this only works on a case by case basis).

Thixomag
24th February 2009, 21:56
Ok thanks.

Until now I do my test on simple application. Now I do the modification on my real application (a FPS) :

remplace

m_udpSocket->writeDatagram(datagram, ipCLient, m_port);
instead of

QUdpSocket udpSocket;
udpSocket.writeDatagram(datagram, ipCLient, m_port);

and when the server or the client run the line m_udpSocket->writeDatagram(datagram, ipCLient, m_port); the application stop working.
Maybe you have an idea ? I tested to do waitForBytesWritten but it change nothing

wysota
25th February 2009, 01:01
I bet dollars against nuts that your socket object goes out of scope before any data has a chance to leave it (in the second case).