資源描述:
《基于TCP的網(wǎng)絡(luò)聊天程序》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫。
1、基于TCP的網(wǎng)絡(luò)聊天程序問題概述:網(wǎng)絡(luò)聊天是網(wǎng)絡(luò)應(yīng)用中的一種常見的功能。TCP是一種可靠的、面向連接、面向數(shù)據(jù)流的傳輸協(xié)議,多數(shù)網(wǎng)絡(luò)協(xié)議都使用TCP協(xié)議,包括HTTP和FTP,TCP協(xié)議非常適合數(shù)據(jù)的連續(xù)傳輸。技術(shù)難點(diǎn):TCP通信協(xié)議;QTcpsocket的使用。實(shí)踐方案:在linux_ubuntu下使用qt_eclipse編寫c++程序。由面向?qū)ο蟮乃枷?,為了?shí)現(xiàn)程序,在server終端部分有3個(gè)類:1.serve2.tcpclient3.tcpserveServe類是繼承自QTcpServer,實(shí)現(xiàn)TCP協(xié)議的服務(wù)器;Tcpclient繼承自QTcpSocket,實(shí)現(xiàn)
2、一個(gè)TCP套接字;Tcpserve是一個(gè)QDialog,負(fù)責(zé)服務(wù)器終端的對(duì)話框顯示與控制。如下是對(duì)serve的實(shí)現(xiàn)代碼:Server.h#include#include"tcpclientsocket.h"classServer:publicQTcpServer{Q_OBJECTpublic:Server(QObject*parent=0,intport=0);QListtcpClientSocketList;signals:voidupdateServer(QString,int);publicslots:voi
3、dupdateClients(QString,int);voidslotDisconnected(int);protected:voidincomingConnection(intsocketDescriptor);};____________________________________________________________________Tcpclient.h#include#includeclassTcpClientSocket:publicQTcpSocket{Q_OBJECTpublic:TcpClientSock
4、et(QObject*parent=0);~TcpClientSocket();signals:voidupdateClients(QString,int);voiddisconnected(int);protectedslots:voiddataReceived();voidslotDisconnected();};__________________________________________________________________Tcpserver.h#include#include#include"server.h"
5、classTcpServer:publicQWidget{Q_OBJECTpublic:TcpServer(QWidget*parent=0,Qt::WindowFlagsf=0);~TcpServer();public:QListWidget*ListWidgetContent;QLabel*LabelPort;QLineEdit*LineEditPort;QPushButton*PushButtonCreate;intport;Server*server;public:voidslotCreateServer();voidupdateServer(QString,in
6、t);};________________________________________________________________Server.cpp#include#include"server.h"Server::Server(QObject*parent,intport):QTcpServer(parent){listen(QHostAddress::Any,port);}voidServer::incomingConnection(intsocketDescriptor){TcpClientSocket*tcpClientSocket
7、=newTcpClientSocket(this);connect(tcpClientSocket,SIGNAL(updateClients(QString,int)),this,SLOT(updateClients(QString,int)));connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));tcpClientSocket->setSocketDescriptor(socketDescriptor);tcp