【问题标题】:Qt4 DNS Proxy QUdpSocketQt4 DNS 代理 QUdpSocket
【发布时间】:2009-09-08 07:29:21
【问题描述】:

我实际上是在尝试使用 Qt4 制作 DNS 代理应用程序。如果我将我的 DNS 名称服务器设置为“localhost”,那么我想将所有 DNS 请求转发到 remoteSocket 对象中指定的服务器。除了将数据从 remoteSocket 对象发送回请求 DNS 查找的 localSocket 对象外,一切似乎都工作正常。

当写到 localSocket 时,有什么我需要知道的具体信息吗?问题似乎出在 readResponse() 中。

#include "dns.h"

Dns::Dns()
{
}

void Dns::initSocket()
{
    localDatagram = new QByteArray();
    remoteDatagram = new QByteArray();

    localSocket = new QUdpSocket();
    connect(localSocket, SIGNAL(readyRead()), this, SLOT(readRequest()), Qt::DirectConnection);
    localSocket->bind(QHostAddress::LocalHost, 53);

    remoteSocket = new QUdpSocket();
    remoteSocket->connectToHost(QHostAddress("4.2.2.1"), 53);
    connect(remoteSocket, SIGNAL(readyRead()), this, SLOT(readResponse()), Qt::DirectConnection);

}

void Dns::readRequest()
{
    while (localSocket->hasPendingDatagrams()) {
        localDatagram->resize(localSocket->pendingDatagramSize());\
        localSocket->readDatagram(localDatagram->data(), localDatagram->size());
        remoteSocket->write(*localDatagram);
    }
}

void Dns::readResponse()
{
    QByteArray bytes(remoteSocket->readAll());
    qDebug() << "BYTES: [" << bytes.toBase64() << "]";
    localSocket->write(bytes);
}

【问题讨论】:

  • 对此的任何帮助将不胜感激。
  • 您没有告诉我们如何坏了,只是说坏了。是什么让你觉得它坏了?您从调试线得到什么输出?你试图做什么来解决这个问题?你的 readResponse() 插槽甚至被调用了吗?请添加更多信息,我会尽力提供帮助。

标签: c++ qt proxy qt4 dns


【解决方案1】:

我假设使用 QUdpSocket::bind(),生成的套接字对象将能够使用访问方法获取 peerAddress/peerPort,但事实并非如此。

最终的解决方案是:

QHostAddress sender;
quint16 senderPort;

localSocket->readDatagram(localDatagram->data(), localDatagram->size(), &sender, &senderPort);

而在 readResponse() 中,

localSocket->writeDatagram(bytes, sender, senderPort);

【讨论】:

  • 是的,UDP 是一种无连接协议,因此信息可用的唯一时间是数据包到达时。
猜你喜欢
  • 1970-01-01
  • 2012-10-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-22
相关资源
最近更新 更多