基于tcp协议的网络聊天室

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------班级:软件113姓名:蒋栋学号:1108050328成绩:实验名称:基于TCP的网络聊天室1.实验目的:掌握TCP通讯协议、掌握QTcpSocket2.实验内容:使用Qt的QtcpSocket实现简单的网络聊天程序,范例如图:包括拂去其程序和客户端程序,服务端程序可以创建一个聊天室,客户端程序可以输入登陆的用户名、服务器地址以及使用的端口号,然后进入聊天室,聊天室中的每一位用户均可以看见发布的信息。3.实验方法:使用Qt的QtcpSocket,参考Qt网络编程实验。4.实验过程tcpServer端代码:#includedialog.h#includeui_dialog.h青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------Dialog::Dialog(QWidget*parent):QDialog(parent),ui(newUi::Dialog){ui-setupUi(this);ui-lineEdit-setText(8010);port=8010;}Dialog::~Dialog(){deleteui;}voidDialog::on_newchat_clicked(){server=newServer(this,port);connect(server,SIGNAL(updateServer(QString,int)),this,SLOT(updateServer(QString,int)));ui-newchat-setEnabled(false);}voidDialog::updateServer(QStringmsg,intlength){ui-listWidget-addItem(msg.left(length));}tcpServer端server.cpp代码:#includeserver.h#includeQTcpSocketServer::Server(QObject*parent,intport):QTcpServer(parent){listen(QHostAddress::Any,port);}voidServer::incomingConnection(intsocketDescriptor){TcpclientSocket*tcpClientSocket=newTcpclientSocket(this);connect(tcpClientSocket,SIGNAL(updateClient(QString,int)),this,SLOT(updateClient(QString,int)));connect(tcpClientSocket,SIGNAL(disconnected(int)),this,SLOT(slotDisconnected(int)));tcpClientSocket-setSocketDescriptor(socketDescriptor);青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------tcpClientSocketList.append(tcpClientSocket);}voidServer::updateClient(QStringmsg,intlength){emitupdateServer(msg,length);for(inti=0;itcpClientSocketList.count();i++){QTcpSocket*item=tcpClientSocketList.at(i);if(item-write(msg.toLatin1(),length)!=length){continue;};}}voidServer::slotDisconnected(intdescriptor){for(inti=0;itcpClientSocketList.count();i++){QTcpSocket*item=tcpClientSocketList.at(i);if(item-socketDescriptor()==descriptor){tcpClientSocketList.removeAt(i);return;}}return;}Tcpclient端代码:#includedialog.h#includeui_dialog.h#includeQMessageBoxDialog::Dialog(QWidget*parent):QDialog(parent),ui(newUi::Dialog){ui-setupUi(this);ui-lineEdit_4-setText(8010);status=false;serverIP=newQHostAddress();port=8010;ui-pb_send-setEnabled(false);}青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------Dialog::~Dialog(){deleteui;}voidDialog::on_pb_send_clicked(){slotSend();}voidDialog::on_pb_enter_clicked(){slotEnter();}voidDialog::slotEnter(){if(!status){QStringip=ui-lineEdit_3-text();if(!serverIP-setAddress(ip)){QMessageBox::information(this,tr(error),tr(serveripaddresserror!));return;}if(ui-LineEditUser-text()==){QMessageBox::information(this,tr(error),tr(Usernameerror!));return;}userName=ui-LineEditUser-text();tcpSocket=newQTcpSocket(this);connect(tcpSocket,SIGNAL(connected()),this,SLOT(slotConnected()));connect(tcpSocket,SIGNAL(disconnected()),this,SLOT(slotDisconnected()));connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(dataReceived()));tcpSocket-connectToHost(*serverIP,port);status=true;}else青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------{intlength=0;QStringmsg=userName+tr(:LeaveChatRoom);if((length=tcpSocket-write(msg.toLatin1(),msg.length()))!=msg.length()){return;}tcpSocket-disconnectFromHost();status=false;}}voidDialog::slotConnected(){intlength=0;ui-pb_send-setEnabled(true);ui-pb_enter-setText(tr(Leave));QStringmsg=userName+tr(:EnterChatRoom);if((length=tcpSocket-write(msg.toLatin1(),msg.length()))!=msg.length()){return;}}voidDialog::slotDisconnected(){ui-pb_send-setEnabled(false);ui-pb_enter-setText(tr(Enter));}voidDialog::slotSend(){if(ui-LineEditSend-text()==){return;}QStringmsg=userName+:+ui-LineEditSend-text();tcpSocket-write(msg.toLatin1(),msg.length());ui-LineEditSend-clear();}青岛科技大学信息科学技术学院实验报告----------------------------------------------------------------------------------------------------------------------voidDialog::dataReceived(){while(tcpSocket-bytesAvailable()0){QByteArraydatagram;datagram.resize(tcpSocket-bytesAvailable());tcpSocket-read(datagram.data(),datagram.size());QStringmsg=datagram.data();ui-listWidget-addItem(msg.left(datagram.size()));}}5.实验结果客户端1服务器端客户端26.实验总结经过本次实验练习了TCP通讯协议、QTcpSocket的使用。基本掌握了它们的使用。服务端程序可以创建一个聊天室,客户端程序可以输入登陆的用户名、服务器地址以及使用的端口号,然后进入聊天室,聊天室中的每一位用户均可以看见发布的信息。本次实验测试环境为本机。首先要给本机设置ip地址。另外程序中要注意端口的赋值。7.教师评语

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功