資源描述:
《基于tcp協(xié)議的網(wǎng)絡(luò)聊天室.doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在工程資料-天天文庫。
1、基于tcp協(xié)議的網(wǎng)絡(luò)聊天室青島科技人學(xué)信息科學(xué)技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告蔣棟學(xué)號:班級:軟件113姓名:1108050328成績:實(shí)驗(yàn)名稱:基于TCP的網(wǎng)絡(luò)聊天室1.實(shí)驗(yàn)日的:掌握TCP通訊協(xié)議、掌握QTcpSocket2.實(shí)驗(yàn)內(nèi)容:使用Qt的QtcpSocket實(shí)現(xiàn)簡單的網(wǎng)絡(luò)聊天程序,范例如圖:網(wǎng)絡(luò)聊天室■客戶端X離開聊天室包括拂去其程序和客戶端程序,服務(wù)端程序可以創(chuàng)建一個(gè)聊天室,客戶端程序可以輸入登陸的用戶名、服務(wù)器地址以及使用的端口號,然后進(jìn)入聊天室,聊天室屮的每一位用戶均可以看見發(fā)布的信息。1.實(shí)驗(yàn)方法:使用Qt的QtcpSocket,參考Q
2、t網(wǎng)絡(luò)編程實(shí)驗(yàn)。2.實(shí)驗(yàn)過程tcpServer端代碼:^include"dialog,h"#include〃ui_dial()g.h"青島科技人學(xué)信息科學(xué)技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告Dialog::Dialog(QWidget*parent):QDialog(parent),ui(newUi::Dialog)Iui->setupUi(this);ui->lineEdit->setText(“8010");port二8010;Dialog::"Dialog(){deleteui;}voidDialog::on_newchat_clicked(){serv
3、er二newServer(this,port);cormect(server,SIGNAL(updateServor(QString,int)),this,SLOT(updateServer(QString,int)));ui->newchat->setEnabled(false);}voidDialog::updateServer(QStringmsg,intlength){ui->listWidget->addltem(msg.left(length));}tcpServer端server,cpp代碼:#inelude"server.h
4、〃#includeServer::Server(QObject*parent,intport):QTcpServer(parent)listen(QIIostAddress::Any,port);}voidServer::incomingConnection(intsocketDescriptor){TcpclientSocket*tcpClientSocket二newTcpclientSocket(this);connect(tcpClientSocket,SIGNAL(updatedient(QString,in
5、t)),this,SLOT(updatedient(QString,int)));connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnectecl(int)));tcpClientSocket->setSocketDescriptor(socketDescriptor);青島科技人學(xué)信息科學(xué)技術(shù)學(xué)院實(shí)驗(yàn)報(bào)告tcpClientSocketList.append(tcpClientSocket);}voidServer::updateClient(QStr
6、ingmsg,intlength){emitupdateServer(msg,length);for(inti=0;i〈tcpClientSocketList?count.();i++){QTcpSockct*itom=tcpClicnt.SockctList?at(i);if(item-〉write(msg.toLatinl(),length)'^length){continue;};voidServer::slotDisconnected(intdescriptor)for(inti二0;i7、unt();i++){QTcpSocke二tcpClientSocketList.at(i);if(item->socketDescriptor()二二descriptor){tcpClientSocketList?reinovcAt(i);return;}}return;}Tcpclicnt端代碼:#include"dialog.h〃#inelude,zui_dialog.h,z#ineludeDialog::Dialog(QWidget*parent):QDialog(parenl),ui(newUi::Dia
8、log)Iui->setupUi(this);ui->lincEdit4-〉sctText(“8010");status二false;serverIP二newQllostAddr