【问题标题】:Get remote host IP address and port QUdpSocket获取远程主机IP地址和端口QUdpSocket
【发布时间】:2016-10-17 11:47:45
【问题描述】:

我使用 QUdpSocket 创建一个 UDP 服务器。当客户端连接到它时,我想知道客户端的 IP 地址和端口号。我该怎么做?

【问题讨论】:

标签: qt udp ip


【解决方案1】:

在服务器端,您可以看到数据来自使用的地址:

readDatagram(char * data, qint64 maxSize, QHostAddress * address = 0, quint16 * port = 0)

在收到 readyRead() 信号时读取数据。

【讨论】:

    【解决方案2】:
    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;
    

    这是获取发件人地址的正确方法。

    【讨论】:

      猜你喜欢
      • 2012-03-01
      • 2012-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-29
      • 2011-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多