使用 QUdpSocket 的 Qt4 DNS 服务器

发布于 2024-08-04 04:04:45 字数 434 浏览 2 评论 0原文

我正在为我正在开发的应用程序编写 DNS 服务器,但遇到了一些困难。

我想绑定到本地主机上的端口 53 并侦听 DNS 查找请求,然后发回响应。不幸的是,当我调用 QUdpSocket::bind() 时,生成的套接字不可写。我是否需要传递一个标志才能发送数据?

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

稍后,在建立连接后,我想调用 QUdpSocket::write* 方法之一,但这不起作用,因为 writeable 为 false。

I am writing a DNS server for an application I'm working on and I'm running into some difficulties.

I want to bind to port 53 on localhost and listen for DNS lookup requests, then send back the response. Unfortunately, when I call QUdpSocket::bind() the resulting socket is not writeable. Is there a flag I need to pass to make it so I can send data back?

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

Later on, after the connection is established, I want to call one of the QUdpSocket::write* methods, but that is not working as writeable is false..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

从来不烧饼 2024-08-11 04:04:45

首先,您确定要绑定到QHostAddress::Lockhost吗?您知道这在任何公共网络上都是不可见的,对吗?它仅对同一物理盒子上的应用程序可见。

其次,您没有检查 QUdpSocket::bind 调用的返回参数,因此您无法知道绑定调用是否已执行实际上成功了。

第三,您是否尝试过不使用 QUdpSocket::ShareAddress 提示? BindMode 枚举的 Qt 文档 神秘地说明了有关 ShareAddress< /代码>:

但是,因为任何服务都是
允许重新绑定,这个选项是
受一定的安全保障
考虑因素。

我不确定这些安全考虑因素是什么 - 但首先要尝试的事情之一就是简化您的代码。

最后,也许问题是你的 onReadyRead() 插槽中的代码?发布该代码可以帮助我们诊断问题。

First, are you sure you want to bind to QHostAddress::Lockhost? You realise that this will not be visible on any public network, right? It will only be visible to applications on the same physical box..

Second, you're not checking the return parameter from the QUdpSocket::bind call, so you have no way of knowing whether the bind call actually succeeded.

Third, have you tried without the QUdpSocket::ShareAddress hint? The Qt documentation for BindMode enum cryptically states regarding ShareAddress:

However, because any service is
allowed to rebind, this option is
subject to certain security
considerations.

I'm not sure what these security considerations are - but one of the first things to try is to simplify your code.

Finally, perhaps the problem is the code inside your onReadyRead() slot? Posting that code may help us diagnose the issue.

轮廓§ 2024-08-11 04:04:45

您在哪个平台上运行?

我认为Linux出于安全原因不允许普通用户绑定1024以下。

Which platform are you running on?

I think Linux does not allow normal users to bind below 1024 for security reasons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文