欢迎来到天天文库
浏览记录
ID:38796629
大小:22.44 KB
页数:5页
时间:2019-06-19
《基于TCP的网络聊天程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、基于TCP的网络聊天程序问题概述:网络聊天是网络应用中的一种常见的功能。TCP是一种可靠的、面向连接、面向数据流的传输协议,多数网络协议都使用TCP协议,包括HTTP和FTP,TCP协议非常适合数据的连续传输。技术难点:TCP通信协议;QTcpsocket的使用。实践方案:在linux_ubuntu下使用qt_eclipse编写c++程序。由面向对象的思想,为了实现程序,在server终端部分有3个类:1.serve2.tcpclient3.tcpserveServe类是继承自QTcpServer,实现TCP协议的服务器;Tcpclient继承自QTcpSocket,实现
2、一个TCP套接字;Tcpserve是一个QDialog,负责服务器终端的对话框显示与控制。如下是对serve的实现代码: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
此文档下载收益归作者所有