使用 QUdpSocket 的 Qt4 DNS 服务器
我正在为我正在开发的应用程序编写 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,您确定要绑定到
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 regardingShareAddress
: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.您在哪个平台上运行?
我认为Linux出于安全原因不允许普通用户绑定1024以下。
Which platform are you running on?
I think Linux does not allow normal users to bind below 1024 for security reasons.