源码下载:http://download.csdn.net/download/u011314012/10191972
在应用程序开发中网络编程非常重要,目前互联网通信的TCP/IP协议,自上而下分为应用层、传输层、网际层和网络接口层这四层。实际编写网络应用程序时只使用到
传输层和应用层,所涉及的协议主要包括UDP、TCP、FTP和HTTP等。
10.1获取本机网络信息
在网络应用中,进场需要获得本机的主机名、IP地址和硬件地址等网络信息。运用QHostInfo、QNetworkInterface、QNetworkAddressEntry可获得本机的网络信息。
运行效果如下:
networkinformation.h
#ifndef NETWORKINFORMATION_H
#define NETWORKINFORMATION_H
#include <QWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include <QMessageBox>
class NetworkInformation : public QWidget
{
Q_OBJECT
public:
NetworkInformation(QWidget *parent = 0);
~NetworkInformation();
void getHostInformation();
public slots:
void slotHostDetailInformation();
private:
QLabel *hostLabel;
QLineEdit *hostNameLineEdit;
QLabel *ipLabel;
QLineEdit *ipLineEdit;
QPushButton *detailBtn;
QGridLayout *mainLayout;
};
#endif // NETWORKINFORMATION_H
networkinformation.cpp
#include "networkinformation.h"
#include <QHostInfo>
#include <QNetworkInterface>
NetworkInformation::NetworkInformation(QWidget *parent)
: QWidget(parent)
{
hostLabel = new QLabel(tr("主机名:"));
hostNameLineEdit = new QLineEdit;
ipLabel = new QLabel(tr("IP 地址:"));
ipLineEdit = new QLineEdit;
detailBtn = new QPushButton(tr("详情"));
mainLayout = new QGridLayout(this);
mainLayout->addWidget (hostLabel, 0, 0);
mainLayout->addWidget (hostNameLineEdit, 0, 1);
mainLayout->addWidget (ipLabel, 1, 0);
mainLayout->addWidget (ipLineEdit, 1, 1);
mainLayout->addWidget (detailBtn, 2, 0, 1, 2);
getHostInformation ();
connect (detailBtn, SIGNAL(clicked(bool)), this, SLOT(slotHostDetailInformation()));
}
NetworkInformation::~NetworkInformation()
{
}
/*
* QString localHostName = QHostInfo::localHostName ():获得本机主机名。QHostInfo提供了一系列有关网络信息的静态函数,
* 可以根据主机名获得分配的IP地址,也可以根据IP地址获得相应的主机名
*
* QHostInfo hostInfo = QHostInfo::fromName (localHostName):根据主机名获得相关信息,包括IP地址等。QHostInfo::
* fromName()函数通过主机名查找IP地址信息。
*
* if(!listAdress.isEmpty ()){...}获得的主机IP地址列表可能为空。在不为空的情况下使用第一个IP地址。
*/
void NetworkInformation::getHostInformation ()
{
QString localHostName = QHostInfo::localHostName ();
hostNameLineEdit->setText (localHostName);
QHostInfo hostInfo = QHostInfo::fromName (localHostName);
QList<QHostAddress> listAdress = hostInfo.addresses ();
if(!listAdress.isEmpty ())
{
ipLineEdit->setText (listAdress.first ().toString ());
}
}
/*
* QNetworkInterface类提供了一个主机IP地址和网络接口的列表
*
* interface.name():获得网络接口的名称
* interface.hardwareAddress():获得网络接口的硬件地址
* interface.addressEntries():每个网络接口包括0个或多个IP地址,每个IP地址有选择性地与一个子网掩码和一个广播地址相关联。
* QNetworkAddressEntry类存储了被网络接口支持的一个IP地址,同时还包括与之相关的子网掩码和广播地址
*
* QMessageBox::information
* (
QWidget*parent, //消息框的父窗口指针
const QString& title, //消息框的标题栏
const QString& text, //消息框的文字提示信息
StandardButtonsbuttons=Ok, //同Question消息框的注释内容
StandardButton defaultButton=NoButton //同Question消息框的注释内容
);
*/
void NetworkInformation::slotHostDetailInformation ()
{
QString detail = "";
QList<QNetworkInterface> list = QNetworkInterface::allInterfaces ();
for(int i = 0; i < list.count (); i++)
{
QNetworkInterface interface = list.at (i);
detail = detail + tr("设备") + interface.name () + "\n";
detail = detail + tr("硬件地址") + interface.hardwareAddress () + "\n";
QList<QNetworkAddressEntry> entryList = interface.addressEntries ();
for(int j = 0; j < entryList.count (); j++)
{
QNetworkAddressEntry entry = entryList.at(j);
detail = detail + "\t" + tr("IP 地址:") + entry.ip ().toString () + "\n";
detail = detail + "\t" + tr("子网掩码") + entry.netmask ().toString () + "\n";
detail = detail + "\t" + tr("广播地址") + entry.broadcast ().toString () + "\n";
}
}
QMessageBox::information (this, tr("Detail"), detail);
}
10.2基于UDP的网络广播程序
用户数据报协议(User Data Protocol,UDP)是一种简单轻量级、不可靠、面向数据报、无连接的传输层协议,可以应用在可靠性不是十分重要的场合,如短消息、广
播信息等。
适合应用的情况有以下几种:
网络数据大多为短消息。
拥有大量客户端。
对数据安全性无特殊要求。
网络负担非常重,但对响应速度要求高。
10.2.1 UDP协议工作原理
如下图所示,UDP客户端向UDP服务器发送一定长度的请求报文,报文大小的限制与各系统的协议实现有关,但不得超过其下层IP协议规定的64KB;UDP服务器同样以报
文形式作出响应。如果服务器未收到此请求,客户端不会重发,因此报文的传输是不可靠的。
例如,常用的聊天工具--腾讯QQ软件就是使用UDP协议发送协议消息的,因此有时会出现收不到消息的情况。
10.2.2 UDP编程模型
基于UDP协议的经典编程模型,程序编程的通信流程如图所示。
可以看出,在UDP方式下,客户端并不与服务器建立连接,它只负责调用发送函数向服务器发出数据报。类似地,服务器也不从客户端接收链接,只负责调用接收函数,
等待来自某客户端的数据到达。
Qt中通过QUdpSocket类实现UDP协议的编程。接下来通过一个实例,介绍如何实现基于UDP协议的广播应用,它由UDP服务器和UDP客户端两部分组成。
10.2.3 UDP服务器编程
udpserver.h
#ifndef UDPSERVER_H
#define UDPSERVER_H
#include <QDialog>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QVBoxLayout>
#include <QUdpSocket>
#include <QTimer>
class UdpServer : public QDialog
{
Q_OBJECT
public:
UdpServer(QWidget *parent = 0);
~UdpServer();
public slots:
void StartBtnClicked();