QUdpSocket. How to get the sender's ip address?
I 开发者_开发技巧have a server program that uses QUdpSocket. How do I find the ip address of the sender?
From http://doc.qt.io/archives/4.6/qudpsocket.html#readDatagram,
qint64 QUdpSocket::readDatagram ( char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0 )
You pass the addresses of a QHostAddress and a quint16 to the receive function, which get populated with the desired data.
You can get the sender address (and port) when you use the qint64 QUdpSocket::readDatagram ( char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0 )
method.
You need to use readDatagram(). From the documentation :
The sender's host address and port is stored in *address and *port (unless the pointers are 0).
精彩评论