通过QT的UDP通信,访问DNS服务器进行域名解析小例程

使用QT的网络套接字需要.pro文件中加入一句:

QT       += network
1、创建套接字:

uSocket = new QUdpSocket;
2、绑定本地IP:

uSocket->bind(QHostAddress("192.168.1.1"), 53);
3、连接接收信号和槽

connect(uSocket, SIGNAL(readyRead()), this, SLOT(receive()));
4、向目的DNS服务器发送数据
    if(uSocket->writeDatagram(msg, QHostAddress("114.114.114.114"), 53) <= 0)
        perror("uSocket");
5、接收数据

nLength = uSocket->pendingDatagramSize();
baData.resize(nLength);
uSocket->readDatagram(baData.data(), baData.size());
二、一下是程序例程:

首先是mainwindow.h文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include 
#include 
#include 
#include 
#include 

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT


public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();


private slots:
    void receive();
    void on_sendButton_clicked();

private:
    QUdpSocket *uSocket;
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H
然后是mainwindow.cpp文件:

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setWindowTitle(tr("域名解析"));

    ui->lineEdit_domain->setText("www.baidu.com");

    uSocket = new QUdpSocket;
    uSocket->bind(QHostAddress("192.168.1.1"), 53);
    connect(uSocket, SIGNAL(readyRead()), this, SLOT(receive()));
}
/*
 * UDP数据接收槽函数
 */
void MainWindow::receive()
{
    QByteArray baData;
    int nLength;
    while(uSocket->hasPendingDatagrams())
    {
        nLength = uSocket->pendingDatagramSize();
        baData.resize(nLength);
        uSocket->readDatagram(baData.data(), baData.size());
    }
    //解析最后4个字节为IP地址
    QString strIp = QString::number((unsigned char)baData[nLength-4], 'g', 10) + "." +
                        QString::number((unsigned char)baData[nLength-3], 'g', 10) + "." +
                        QString::number((unsigned char)baData[nLength-2], 'g', 10) + "." +
                        QString::number((unsigned char)baData[nLength-1], 'g', 10);
    //显示IP
    ui->lineEdit_ip->setText(strIp);
}

MainWindow::~MainWindow()
{
    if(uSocket != null )
        uSocket->close();
    delete ui;
}
/*
 * 发送按键槽函数
 */
void MainWindow::on_sendButton_clicked()
{
    char tail[] = {0x00,0x00,0x01,0x00,0x01};
    char szMsg[] = {0x2f,0x1f,0x01,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00};
    QString domain = ui->lineEdit_domain->text();
    QString strName1 = domain.section('.', 0, 0);
    QString strName2 = domain.section('.', 1, 1);
    QString strName3 = domain.section('.', 2, 2);
    //域名请求指令组包
    QByteArray msg;
    msg.append(szMsg, 12);
    msg.append(strName1.length());
    msg.append(strName1);
    msg.append(strName2.length());
    msg.append(strName2);
    msg.append(strName3.length());
    msg.append(strName3);
    msg.append(tail, sizeof(tail));
    //UDP数据发送
    if(uSocket->writeDatagram(msg, QHostAddress("114.114.114.114"), 53) <= 0)
        perror("uSocket");
}

最后是ui的设计:

通过QT的UDP通信,访问DNS服务器进行域名解析小例程_第1张图片

附:完整代码链接:

http://download.csdn.net/download/huiking2005/10236239

点击打开链接

你可能感兴趣的:(QT,UDP,DNS)