Qt4 DNS代理QUdpSocket

编程入门 行业动态 更新时间:2024-10-22 23:10:32
本文介绍了Qt4 DNS代理QUdpSocket的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I'm essentially trying to make a DNS proxy application using Qt4. If I set my DNS nameserver to 'localhost' then I want to forward all DNS requests to the server specified in the remoteSocket object. Everything seems to be working fine except sending the data from the remoteSocket object back to the localSocket object which is requesting the DNS lookup.

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

When writing to localSocket, is there anything specific I need to know about that? The problem seems to be in 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,但是不是这样。

I was assuming that using QUdpSocket::bind(), the resulting socket object would be able to obtain the peerAddress/peerPort using the access methods, however that was not the case.

最终的解决方案做:

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

在readResponse()中,

And in readResponse(),

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

更多推荐

Qt4 DNS代理QUdpSocket

本文发布于:2023-10-09 18:27:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476439.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:DNS   QUdpSocket

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!