我使用QUdpSocket创建UDP服务器。当客户端连接到它时,我想知道客户端的IP地址和端口号。我该怎么办?
答案 0 :(得分:0)
在服务器端,您可以看到数据来自的地址:
readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)
获取readyRead()信号时读取数据。
答案 1 :(得分:0)
QHostAddress sender;
quint16 senderPort;
socket->readDatagram(buffer.data(), buffer.size(), &sender, &senderPort);
QString received = QString(buffer);
qDebug() << "Message received on IP:" << socket->localAddress().toString() << "Port:" << socket->localPort();
qDebug() << "Message from:" << sender.toString();
qDebug() << "Message Port:" << senderPort;
qDebug() << "Message:" << received;
这是获取发件人地址的正确方法。