基于tcp协议的网络聊天室(共6页).doc
精选优质文档-倾情为你奉上班级: 软件113 姓名: 蒋栋 学号: 成绩: 实验名称: 基于TCP的网络聊天室1.实验目的: 掌握TCP通讯协议、掌握QTcpSocket2.实验内容: 使用Qt的QtcpSocket实现简单的网络聊天程序,范例如图: 包括拂去其程序和客户端程序,服务端程序可以创建一个聊天室,客户端程序可以输入登陆的用户名、服务器地址以及使用的端口号,然后进入聊天室,聊天室中的每一位用户均可以看见发布的信息。3.实验方法: 使用Qt的QtcpSocket,参考Qt网络编程实验。4.实验过程tcpServer端 代码:#include "dialog.h"#include "ui_dialog.h"Dialog:Dialog(QWidget *parent) : QDialog(parent), ui(new Ui:Dialog) ui->setupUi(this); ui->lineEdit->setText("8010"); port=8010;Dialog:Dialog() delete ui;void Dialog:on_newchat_clicked() server=new Server(this,port); connect(server,SIGNAL(updateServer(QString,int),this,SLOT(updateServer(QString,int); ui->newchat->setEnabled(false);void Dialog:updateServer(QString msg,int length) ui->listWidget->addItem (msg.left(length) );tcpServer端server.cpp 代码:#include "server.h"#include <QTcpSocket>Server:Server(QObject*parent,int port) :QTcpServer(parent) listen(QHostAddress:Any,port);void Server:incomingConnection(int socketDescriptor) TcpclientSocket*tcpClientSocket=new TcpclientSocket(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);void Server:updateClient(QString msg, int length) emit updateServer(msg,length); for(int i=0;i<tcpClientSocketList.count();i+) QTcpSocket*item=tcpClientSocketList.at(i); if(item->write(msg.toLatin1(),length)!=length) continue ; void Server:slotDisconnected(int descriptor) for(int i=0;i<tcpClientSocketList.count();i+) QTcpSocket*item=tcpClientSocketList.at(i); if(item->socketDescriptor()=descriptor) tcpClientSocketList.removeAt(i); return; return;Tcpclient端代码:#include "dialog.h"#include "ui_dialog.h"#include <QMessageBox>Dialog:Dialog(QWidget *parent) : QDialog(parent), ui(new Ui:Dialog) ui->setupUi(this); ui->lineEdit_4->setText("8010"); status=false; serverIP =new QHostAddress(); port = 8010; ui->pb_send->setEnabled(false);Dialog:Dialog() delete ui;void Dialog:on_pb_send_clicked() slotSend();void Dialog:on_pb_enter_clicked() slotEnter();void Dialog:slotEnter() if(!status) QString ip=ui->lineEdit_3->text(); if(!serverIP->setAddress(ip) QMessageBox:information(this,tr("error"),tr("server ip address error!"); return; if(ui->LineEditUser->text()="") QMessageBox:information(this,tr("error"),tr("User name error!"); return ; userName=ui->LineEditUser->text(); tcpSocket = new QTcpSocket(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 int length = 0; QString msg=userName+tr(":Leave Chat Room"); if(length=tcpSocket->write(msg.toLatin1(),msg.length()!=msg.length() return ; tcpSocket->disconnectFromHost(); status=false; void Dialog:slotConnected() int length = 0; ui->pb_send->setEnabled( true ); ui->pb_enter->setText(tr("Leave"); QString msg=userName+tr(":Enter Chat Room"); if(length=tcpSocket->write(msg.toLatin1(),msg.length()!=msg.length() return; void Dialog:slotDisconnected() ui->pb_send->setEnabled( false ); ui->pb_enter->setText(tr("Enter");void Dialog:slotSend() if(ui->LineEditSend->text()="") return ; QString msg=userName+":"+ui->LineEditSend->text(); tcpSocket->write(msg.toLatin1(),msg.length(); ui->LineEditSend->clear();void Dialog:dataReceived() while (tcpSocket->bytesAvailable()>0) QByteArray datagram; datagram.resize(tcpSocket->bytesAvailable(); tcpSocket->read(datagram.data(), datagram.size(); QString msg=datagram.data(); ui->listWidget->addItem (msg.left(datagram.size(); 5. 实验结果客户端1 服务器端 客户端26.实验总结 经过本次实验练习了TCP通讯协议、QTcpSocket 的使用。基本掌握了它们的使用。服务端程序可以创建一个聊天室,客户端程序可以输入登陆的用户名、服务器地址以及使用的端口号,然后进入聊天室,聊天室中的每一位用户均可以看见发布的信息。本次实验测试环境为本机。首先要给本机设置ip地址。另外程序中要注意端口的赋值。 7.教师评语专心-专注-专业