gpt4 book ai didi

c++ - Qt4 DNS 代理 QUdpSocket

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:37:24 24 4
gpt4 key购买 nike

我实际上是在尝试使用 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);
}

最佳答案

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

最终的解决方案是:

QHostAddress sender;
quint16 senderPort;

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

并且在 readResponse() 中,

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

关于c++ - Qt4 DNS 代理 QUdpSocket,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1392494/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com