




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領
文檔簡介
1、第11章 網(wǎng)絡與通信,11.1 獲取本機網(wǎng)絡信息,11.2 基于UDP的網(wǎng)絡廣播程序,11.3 基于TCP的網(wǎng)絡聊天室程序,11.4 實現(xiàn)HTTP文件下載,11.5 實現(xiàn)FTP上傳和下載,11.1 獲取本機網(wǎng)絡信息,通過獲得本機網(wǎng)絡信息這一功能介紹如何使用QHostInfo、QNetworkInterface、QNetworkAddressEntry實現(xiàn)獲得本機的網(wǎng)絡信息。 實現(xiàn)步驟如下: (1) 頭文件networkinformation.h的具體代碼。 (2) 源文件networkinformation.cpp的具體代碼。,11.1 獲取本機網(wǎng)絡信息,(3) 源文件main.cpp的具體代
2、碼如下: #include #include networkinformation.h #include int main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codecForLocale(); NetworkInformation w; w.show(); return a.exec(); 此時,運行結(jié)果如圖11.1所示。,11.1 獲取本機網(wǎng)絡信息,以上完成了界面,下面是真正實現(xiàn)了獲得本機網(wǎng)絡信息的內(nèi)容。 (1) 在NetworkInformation.p
3、ro中添加如下代碼: QT += network (2) 在頭文件networkinformation.h中添加的代碼如下: #include public: void getHostInformation(); public slots: void slotDetail();,11.1 獲取本機網(wǎng)絡信息,(3) 在源文件networkinformation.cpp中添加代碼。其中,在構(gòu)造函數(shù)的最后添加: getHostInformation(); connect(detailBtn,SIGNAL(clicked(),this,SLOT(slotDetail(); getHostInforma
4、tion()函數(shù)獲得主機信息。具體實現(xiàn)代碼如下: void NetworkInformation:getHostInformation() QString localHostName = QHostInfo:localHostName(); LineEditLocalHostName-setText(localHostName); QHostInfo hostInfo = QHostInfo:fromName(localHostName); QList listAddress = hostInfo.addresses(); if(!listAddress.isEmpty() LineEditA
5、ddress-setText(listAddress.first().toString(); ,11.1 獲取本機網(wǎng)絡信息,slotDetail()函數(shù)獲得與網(wǎng)絡接口有關(guān)的信息,其具體實現(xiàn)代碼如下: void NetworkInformation:slotDetail() QString detail=; QList list=QNetworkInterface:allInterfaces(); for(int i=0;i entryList=interface.addressEntries(); for(int j=0;jentryList.count();j+) QNetworkAddre
6、ssEntry entry=entryList.at(j); detail=detail+t+tr(IP 地址:)+entry.ip().toString()+n; detail=detail+t+tr(子網(wǎng)掩碼:)+mask().toString()+n; detail=detail+t+tr(廣播地址:)+entry.broadcast().toString()+n; QMessageBox:information(this,tr(Detail),detail); ,11.1 獲取本機網(wǎng)絡信息,(4) 運行結(jié)果如圖11.2所示。 單擊“詳細”按鈕后,得到如圖11.3所示的詳細內(nèi)容。,11.
7、2 基于UDP的網(wǎng)絡廣播程序,11.2.1 UDP協(xié)議工作原理 如圖11.4所示,客戶端向服務器端發(fā)送一定長度的請求報文,報文大小的限制與各系統(tǒng)的協(xié)議實現(xiàn)有關(guān),但不得超過其下層IP協(xié)議規(guī)定的64KB;服務器端可同樣以報文形式做出響應。,11.2.2 UDP C/S編程模型,基于UDP協(xié)議的經(jīng)典C/S(Client/Server,客戶機/服務器)編程模型,程序編寫流程如圖11.5所示。,11.2.3 UDP服務器端,下面是服務器端的編程。 (1) 頭文件udpserver.h中聲明了需要的各種控件,其具體代碼如下: #include #include #include #include #inc
8、lude class UdpServer : public QDialog Q_OBJECT public: UdpServer(QWidget *parent = 0 ,Qt:WindowFlags f=0); UdpServer(); private: QLabel *TimerLabel; QLineEdit *TextLineEdit; QPushButton *StartBtn; QVBoxLayout *mainLayout; ;,11.2.3 UDP服務器端,(2) 源文件udpserver.cpp的具體代碼如下: #include udpserver.h UdpServer:U
9、dpServer(QWidget *parent , Qt:WindowFlags f) : QDialog(parent,f) setWindowTitle(tr(UDP Server); TimerLabel = new QLabel(tr(計時器:),this); TextLineEdit = new QLineEdit(this); StartBtn = new QPushButton(tr(開始:),this); mainLayout = new QVBoxLayout(this); mainLayout-addWidget(TimerLabel); mainLayout-addWi
10、dget(TextLineEdit); mainLayout-addWidget(StartBtn); ,11.2.3 UDP服務器端,(3) 源文件main.cpp的具體代碼如下: #include #include udpserver.h #include int main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codecForLocale(); UdpServer w; w.show(); return a.exec(); (4) 此時運行結(jié)果如圖11.6
11、所示。,11.2.4 UDP客戶端,下面是客戶端的編程。 (1) 頭文件udpclient.h中聲明了需要的各種控件,其具體代碼如下: #include #include #include #include class UdpClient : public QDialog Q_OBJECT public: UdpClient(QWidget *parent = 0 ,Qt:WindowFlags f=0); UdpClient(); private: QTextEdit *ReceiveTextEdit; QPushButton *CloseBtn; QVBoxLayout *mainLayo
12、ut; ;,11.2.4 UDP客戶端,(2) 源文件udpclient.cpp的具體代碼如下: #include udpclient.h UdpClient:UdpClient(QWidget *parent , Qt:WindowFlags f) : QDialog(parent,f) setWindowTitle(tr(UDP Client); ReceiveTextEdit = new QTextEdit(this); CloseBtn = new QPushButton(tr(Close),this); mainLayout=new QVBoxLayout(this); mainLa
13、yout-addWidget(ReceiveTextEdit); mainLayout-addWidget(CloseBtn); ,11.2.4 UDP客戶端,(3) 源文件main.cpp的具體內(nèi)容如下: #include #include udpclient.h int main(int argc, char *argv) QApplication a(argc, argv); UdpClient w; w.show(); return a.exec(); (4) 此時運行結(jié)果如圖11.7所示。,11.3 基于TCP的網(wǎng)絡聊天室程序,TCP協(xié)議與UDP協(xié)議的差別如表11.1所示。,11.3
14、.1 TCP協(xié)議工作原理,如圖11.11所示,TCP協(xié)議能為應用程序提供可靠的通信連接,使一臺計算機發(fā)出的字節(jié)流無差錯地發(fā)往網(wǎng)絡上的其他計算機。,11.3.2 TCP C/S編程模型,基于TCP協(xié)議的經(jīng)典C/S(Client/Server,客戶機/服務器)編程模型,程序編寫流程如圖11.12所示。,11.3.3 TCP服務器端,以下內(nèi)容是服務器端的編程,建立工程TcpS。 (1) 頭文件tcpserver.h中聲明了需要的各種控件,TcpServer繼承自QDialog,實現(xiàn)了服務器端的對話框顯示與控制。其具體代碼。 (2) 源文件tcpserver.cpp中,TcpServ
15、er類的構(gòu)造函數(shù)主要實現(xiàn)窗體各控件的創(chuàng)建、布局等,其具體代碼。 (3) 源文件main.cpp的具體代碼如下: #include #include “tcpserver.h” #include int main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codec ForLocale(); TcpServer w; w.show(); return a.exec(); ,11.3.3 TCP服務器端,(4) 此時運行結(jié)果如圖11.13所示。,11.3.4 TCP客戶
16、端,以下是客戶端的編程,建立工程TcpC。 (1) 頭文件tcpclient.h中,TcpClient類繼承自QDialog類,聲明了需要的各種控件,其具體代碼如下: (2) 源文件tcpclient.cpp的具體代碼。 (3) 源文件main.cpp的具體代碼。 #include #include tcpclient.h #include int main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codecForLocale(); TcpCli
17、ent w; w.show(); return a.exec(); ,11.3.4 TCP客戶端,(4) 運行結(jié)果如圖11.14所示。,11.3.4 TCP客戶端,首先完成聊天室的服務器端功能。 (1) 在工程文件TcpS中添加: QT += network (2) 在工程TcpS中添加C+ Class文件tcpclientsocket.h以及tcpclientsocket.cpp,TcpClientSocket繼承自QTcpSocket,實現(xiàn)一個TCP套接字。為了在服務器端實現(xiàn)與客戶端程序的通信。,11.3.4 TCP客戶端,頭文件tcpclientsoc
18、ket.h的具體代碼如下: #include #include class TcpClientSocket : public QTcpSocket Q_OBJECT /注意要添加宏(Q_OBJECT)實現(xiàn)信號與槽的通信 public: TcpClientSocket(QObject *parent=0); signals: void updateClients(QString,int); void disconnected(int); protected slots: void dataReceived(); void slotDisconnected(); ;,11.3.4 TCP客戶端,(
19、3) 源文件tcpclientsocket.cpp中,構(gòu)造函數(shù)(TcpClientSocket)的內(nèi)容如下(它指定了信號與槽的連接關(guān)系): #include tcpclientsocket.h TcpClientSocket:TcpClientSocket(QObject *parent) connect(this,SIGNAL(readyRead(),this,SLOT(dataReceived(); connect(this,SIGNAL(disconnected(),this,SLOT(slotDisconnected(); ,11.3.4 TCP客戶端,(4) 在工程TcpServer
20、.pro中添加C+ Class文件server.h以及server.cpp, Server繼承自QTcpServer,實現(xiàn)一個TCP協(xié)議的服務器。利用QTcpServer,開發(fā)者可以監(jiān)聽到指定端口的TCP連接。其具體代碼如下(注意加黑部分): #include #include #include tcpclientsocket.h /包含TCP的套接字 class Server : public QTcpServer Q_OBJECT /要添加宏(Q_OBJECT)實現(xiàn)信號與槽的通信 public: Server(QObject *parent=0,int port=0); QList tcp
21、ClientSocketList; signals: void updateServer(QString,int); public slots: void updateClients(QString,int); void slotDisconnected(int); protected: void incomingConnection(int socketDescriptor); ;,11.3.4 TCP客戶端,(5) 源文件server.cpp中,構(gòu)造函數(shù)(Server)的具體內(nèi)容如下: #include server.h Server:Server(QObject *parent,int
22、port) :QTcpServer(parent) listen(QHostAddress:Any,port); ,11.3.4 TCP客戶端,源文件server.cpp中,當出現(xiàn)一個新的連接時,QTcpSever觸發(fā)incomingConnection()函數(shù),參數(shù)socketDescriptor指定了連接的socket描述符,其具體代碼如下: void Server:incomingConnection(int socketDescriptor) TcpClientSocket *tcpClientSocket=new TcpClientSocket(this); connect(tcpC
23、lientSocket,SIGNAL(updateClients(QString,int), this,SLOT(updateClients(QString,int); connect(tcpClientSocket,SIGNAL(disconnected(int),this, SLOT(slotDisconnected(int); tcpClientSocket-setSocketDescriptor(socketDescriptor); tcpClientSocketList.append(tcpClientSocket); ,11.3.4 TCP客戶端,源文件server.cpp中,up
24、dateClients()函數(shù)將任意客戶端發(fā)來的信息進行廣播,保證聊天室的所有客戶均可以看到其他人的發(fā)言。其具體代碼如下: void Server:updateClients(QString msg,int length) emit updateServer(msg,length); for(int i=0;iwrite(msg.toLatin1(),length)!=length) continue; ,11.3.4 TCP客戶端,源文件server.cpp中,slotDisconnected()函數(shù)實現(xiàn)了從tcpClientSocketList列表中將斷開連接的TcpClientSocke
25、t對象刪除掉。其具體代碼如下: void Server:slotDisconnected(int descriptor) for(int i=0;isocketDescriptor()=descriptor) tcpClientSocketList.removeAt(i); return; return; ,11.3.4 TCP客戶端,(6) 在頭文件tcpserver.h添加的具體內(nèi)容如下: #include server.h private: int port; Server *server; public slots: void slotCreateServer(); void upda
26、teServer(QString,int);,11.3.4 TCP客戶端,(7) 在源文件tcpserver.cpp中,構(gòu)造函數(shù)中添加: port=8010; PortLineEdit-setText(QString:number(port); connect(CreateBtn,SIGNAL(clicked(),this,SLOT(slotCreateServer(); 在源文件tcpserver.cpp中,槽函數(shù)slotCreateServer()創(chuàng)建一個TCP服務器,具體內(nèi)容如下: void TcpServer:slotCreateServer() server = new Server
27、(this,port); connect(server,SIGNAL(updateServer(QString,int),this, SLOT(updateServer(QString,int); CreateBtn-setEnabled(false); ,11.3.4 TCP客戶端,(8) 此時工程中添加了很多文件,工程文件中的內(nèi)容已經(jīng)被改變,需重新在工程文件TcpS中添加: QT += network 此時運行服務器端工程TcpS編譯通過。單擊“創(chuàng)建聊天室”按鈕,便開通了TCP聊天室的服務器端,如圖11.15所示。 (9) 在客戶端工程文件TcpClie
28、中添加: QT += network,11.3.4 TCP客戶端,(10) 在頭文件tcpclient.h中添加: #include #include private: bool status; int port; QHostAddress *serverIP; QString userName; QTcpSocket *tcpSocket; public slots: void slotEnter(); void slotConnected(); void slotDisconnected(); void dataReceived(); void slotSend();,11.3
29、.4 TCP客戶端,(11) 在源文件tcpclient.cpp中,添加頭文件: #include #include 其中,在構(gòu)造函數(shù)中添加代碼: status = false; port = 8010; portLineEdit-setText(QString:number(port); serverIP =new QHostAddress(); connect(enterBtn,SIGNAL(clicked(),this,SLOT(slotEnter(); connect(sendBtn,SIGNAL(clicked(),this,SLOT(slotSend(); sendBtn-setE
30、nabled(false);,11.3.4 TCP客戶端,在源文件tcpclient.cpp中,槽函數(shù)slotEnter()實現(xiàn)了進入和離開聊天室的功能。具體代碼。 在源文件tcpclient.cpp中,槽函數(shù)slotConnected()為connected()信號的響應槽,當與服務器連接成功后,客戶端構(gòu)造一條進入聊天室的消息,并通知服務器。其具體代碼如下: void TcpClient:slotConnected() sendBtn-setEnabled(true); enterBtn-setText(tr(Leave); int length=0; QString msg=userNam
31、e+tr(:Enter Chat Room); if(length=tcpSocket-write(msg.toLatin1(),msg.length()!=msg.length() return; ,11.3.4 TCP客戶端,在源文件tcpclient.cpp中,槽函數(shù)slotSend()的具體代碼如下: void TcpClient:slotSend() if(sendLineEdit-text()=) return ; QString msg=userName+:+sendLineEdit-text(); tcpSocket-write(msg.toLatin1(),msg.lengt
32、h(); sendLineEdit-clear(); 在源文件tcpclient.cpp中,槽函數(shù)slotDisconnected()的具體內(nèi)容如下: void TcpClient:slotDisconnected() sendBtn-setEnabled(false); enterBtn-setText(tr(Enter); ,11.3.4 TCP客戶端,源文件tcpclient.cpp的dataReceived()函數(shù),當有數(shù)據(jù)到來時,觸發(fā)此函數(shù),從套接字中將有效數(shù)據(jù)取出并顯示。其具體代碼如下: void TcpClient:dataReceived() while(tcpSocket-b
33、ytesAvailable()0) QByteArray datagram; datagram.resize(tcpSocket-bytesAvailable(); QHostAddress sender; tcpSocket-read(datagram.data(),datagram.size(); QString msg=datagram.data(); contentListWidget-addItem(msg.left(datagram.size(); ,11.3.4 TCP客戶端,(12) 此時運行客戶端TcpC工程結(jié)果如圖11.16所示:,11.3.4 TCP客戶
34、端,(13) 最后,運行結(jié)果如圖11.17所示,登錄了兩個用戶的狀態(tài)。,11.4 實現(xiàn)HTTP文件下載,HTTP只使用一個TCP連接來處理請求、響應和文件的傳輸,如圖11.18和圖11.19所示。,11.4 實現(xiàn)HTTP文件下載,文件下載是網(wǎng)絡應用的常用功能,下面介紹如何實現(xiàn)基于HTTP協(xié)議的文件下載功能。具體實現(xiàn)步驟如下: (1) 頭文件httpclient.h的具體內(nèi)容。 (2) 源文件httpclient.cpp的具體代碼。,11.4 實現(xiàn)HTTP文件下載,(3) 源文件main.cpp的具體內(nèi)容如下: #include #include httpclient.h #include in
35、t main(int argc, char *argv) QApplication a(argc, argv); QTextCodec:setCodecForTr(QTextCodec:codecForLocale(); HttpClient w; w.show(); return a.exec(); (4) 此時運行結(jié)果如圖11.20所示。,11.4 實現(xiàn)HTTP文件下載,下面介紹具體功能實現(xiàn): (1) 在HttpC中添加: QT += network (2) 在頭文件httpclient.h中添加如下代碼: #include #include private: QHttp
36、 *httpClient; QFile *file; bool httpRequestAborted; int requestId; public slots: void slotDownload(); void slotCancel(); void slotQuit(); void httpRequestFinished(int,bool); void httpDataReadProgress(int,int); void httpResponseHeaderReceived(const QHttpResponseHeader ,11.4 實現(xiàn)HTTP文件下載,(3) 在源文件httpcli
37、ent.cpp中添加的內(nèi)容如下: #include #include #include 其中,在構(gòu)造函數(shù)中添加: connect(getBtn,SIGNAL(clicked(),this,SLOT(slotDownload(); connect(cancelBtn,SIGNAL(clicked(),this,SLOT(slotCancel(); connect(quitBtn,SIGNAL(clicked(),this,SLOT(slotQuit(); httpClient =new QHttp(this); connect(httpClient,SIGNAL(requestFinished(
38、int,bool), this,SLOT(httpRequestFinished(int,bool); connect(httpClient,SIGNAL(dataReadProgress(int,int), this,SLOT(httpDataReadProgress(int,int); connect(httpClient,SIGNAL(responseHeaderReceived(QHttpResponseHeader), this,SLOT(httpResponseHeaderReceived(QHttpResponseHeader); cancelBtn-setEnabled(fal
39、se);,11.4 實現(xiàn)HTTP文件下載,槽函數(shù)slotDownload()實現(xiàn)文件的下載,其具體代碼如下: 槽函數(shù)slotCancel()表示中斷下載,置位中斷請求標志,并對按鈕的狀態(tài)進行設置。其具體代碼。 void HttpClient:slotCancel() httpRequestAborted=true; httpClient-abort(); getBtn-setEnabled(true); cancelBtn-setEnabled(false); 槽函數(shù)slotQuit()的具體代碼如下: void HttpClient:slotQuit() accept(); ,11.4 實現(xiàn)
40、HTTP文件下載,函數(shù)httpRequestFinished()處理請求結(jié)束的響應,具體代碼。 函數(shù)httpDataReadProgress()響應傳輸進度狀態(tài)指示,主要完成進度條的更新,具體代碼如下: void HttpClient:httpDataReadProgress(int done,int total) progressBar-setMaximum(total); progressBar-setValue(done); ,11.4 實現(xiàn)HTTP文件下載,函數(shù)httpResponseHeaderReceived()用于接收到服務器端的響應頭時的處理,具體代碼如下: void Http
41、Client:httpResponseHeaderReceived(const QHttpResponseHeader 運行結(jié)果如圖11.21所示。,11.5 實現(xiàn)FTP上傳和下載,FTP的會話模型如圖11.22所示。當用戶啟動與遠程主機間的一個FTP會話時,F(xiàn)TP客戶端首先發(fā)起建立一個與FTP服務器端口號21之間的TCP控制連接,然后通過該控制連接把用戶名和口令發(fā)送給服務器。,11.5 實現(xiàn)FTP上傳和下載,下面將介紹如何實現(xiàn)基于FTP協(xié)議的文件上傳和下載功能。具體實現(xiàn)步驟如下: (1) 頭文件ftpclient.h中聲明了各種需要的控件,其具體代碼。 (2) 源文件ftpclient.cpp的具體代碼。 (3) 源文件main.cpp的具體代碼如下: #include #includ
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025屆廣東省揭陽市惠來一中化學高二下期末學業(yè)水平測試模擬試題含解析
- 福建省福州市屏東中學2025屆高二下化學期末調(diào)研模擬試題含解析
- 四川省成都市溫江中學2025屆高一下化學期末經(jīng)典模擬試題含解析
- 2025屆阿里市重點中學高二下化學期末預測試題含解析
- 梅州農(nóng)業(yè)資金管理辦法
- 校準驗證記錄管理辦法
- 國內(nèi)登山管理辦法珠峰
- 智能電網(wǎng)與能源互聯(lián)網(wǎng)協(xié)同創(chuàng)新-洞察及研究
- 高壓滅菌鍋驗證與性能測試報告
- 農(nóng)墾集團土地管理辦法
- 第二單元 主題活動一《我是聰明的消費者》(說課稿)-2023-2024學年四年級下冊綜合實踐活動內(nèi)蒙古版
- 2024年物聯(lián)網(wǎng)平臺開發(fā)與運營服務合同3篇
- 建設單位安全質(zhì)量管理制度
- 2022-2023學年天津市濱海新區(qū)高一(下)期末語文試卷
- 2024年中國安全應急產(chǎn)業(yè)發(fā)展研究報告
- 2024年優(yōu)居房產(chǎn)加盟業(yè)務保密協(xié)議3篇
- 中國當代文學專題-003-國開機考復習資料
- 企業(yè)自然災害安全應急預案
- 高新技術(shù)企業(yè)研發(fā)費用管理辦法
- 老年急重癥診療及護理
- 中小學家長會期中期末家長會253
評論
0/150
提交評論