欢迎来到淘文阁 - 分享文档赚钱的网站! | 帮助中心 好文档才是您的得力助手!
淘文阁 - 分享文档赚钱的网站
全部分类
  • 研究报告>
  • 管理文献>
  • 标准材料>
  • 技术资料>
  • 教育专区>
  • 应用文书>
  • 生活休闲>
  • 考试试题>
  • pptx模板>
  • 工商注册>
  • 期刊短文>
  • 图片设计>
  • ImageVerifierCode 换一换

    基于tcp协议的网络聊天室(共6页).doc

    • 资源ID:14690296       资源大小:681.50KB        全文页数:6页
    • 资源格式: DOC        下载积分:20金币
    快捷下载 游客一键下载
    会员登录下载
    微信登录下载
    三方登录下载: 微信开放平台登录   QQ登录  
    二维码
    微信扫一扫登录
    下载资源需要20金币
    邮箱/手机:
    温馨提示:
    快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如填写123,账号就是123,密码也是123。
    支付方式: 支付宝    微信支付   
    验证码:   换一换

     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    基于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.教师评语专心-专注-专业

    注意事项

    本文(基于tcp协议的网络聊天室(共6页).doc)为本站会员(飞****2)主动上传,淘文阁 - 分享文档赚钱的网站仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知淘文阁 - 分享文档赚钱的网站(点击联系客服),我们立即给予删除!

    温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




    关于淘文阁 - 版权申诉 - 用户使用规则 - 积分规则 - 联系我们

    本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

    工信部备案号:黑ICP备15003705号 © 2020-2023 www.taowenge.com 淘文阁 

    收起
    展开