基于tcp的网络聊天程序

上传人:第*** 文档编号:34037601 上传时间:2018-02-20 格式:DOCX 页数:5 大小:22.44KB
返回 下载 相关 举报
基于tcp的网络聊天程序_第1页
第1页 / 共5页
基于tcp的网络聊天程序_第2页
第2页 / 共5页
基于tcp的网络聊天程序_第3页
第3页 / 共5页
基于tcp的网络聊天程序_第4页
第4页 / 共5页
基于tcp的网络聊天程序_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《基于tcp的网络聊天程序》由会员分享,可在线阅读,更多相关《基于tcp的网络聊天程序(5页珍藏版)》请在金锄头文库上搜索。

1、基于 TCP 的网络聊天程序问题概述:网络聊天是网络应用中的一种常见的功能。TCP 是一种可靠的、面向连接、面向数据流的传输协议,多数网络协议都使用 TCP 协议,包括 HTTP 和FTP,TCP 协议非常适合数据的连续传输。技术难点:TCP 通信协议;QTcpsocket 的使用。实践方案:在 linux_ubuntu 下使用 qt_eclipse 编写 c+程序。由面向对象的思想,为了实现程序,在 server 终端部分有 3 个类:1. serve2. tcpclient3. tcpserveServe 类是继承自 QTcpServer,实现 TCP 协议的服务器;Tcpclient 继

2、承自 QTcpSocket,实现一个 TCP 套接字;Tcpserve 是一个 QDialog,负责服务器终端的对话框显示与控制。如下是对 serve 的实现代码:Server.h#include #include tcpclientsocket.hclass Server : public QTcpServerQ_OBJECTpublic:Server(QObject *parent = 0,int port=0);QList tcpClientSocketList;signals:void updateServer(QString,int);public slots:void update

3、Clients(QString,int);void slotDisconnected(int);protected:void incomingConnection(int socketDescriptor);_Tcpclient.h#include #include class TcpClientSocket : public QTcpSocketQ_OBJECTpublic:TcpClientSocket( QObject *parent=0);TcpClientSocket();signals:void updateClients(QString,int);void disconnecte

4、d(int);protected slots:void dataReceived();void slotDisconnected();_Tcpserver.h#include #include #include server.hclass TcpServer : public QWidgetQ_OBJECTpublic:TcpServer( QWidget *parent=0, Qt:WindowFlags f=0 );TcpServer();public:QListWidget *ListWidgetContent;QLabel* LabelPort;QLineEdit* LineEditP

5、ort;QPushButton* PushButtonCreate;int port;Server *server;public :void slotCreateServer();void updateServer(QString,int);_Server.cpp#include #include server.hServer:Server(QObject *parent,int port): QTcpServer(parent)listen(QHostAddress:Any,port);void Server:incomingConnection(int socketDescriptor)T

6、cpClientSocket *tcpClientSocket = new TcpClientSocket(this);connect(tcpClientSocket,SIGNAL(updateClients(QString,int),this,SLOT(updateClients(QString,int);connect(tcpClientSocket,SIGNAL(disconnected(int),this,SLOT(slotDisconnected(int);tcpClientSocket-setSocketDescriptor(socketDescriptor);tcpClientS

7、ocketList.append(tcpClientSocket);void Server:updateClients(QString msg,int length)emit updateServer(msg,length);for(int i=0;iwrite(msg.toLatin1(), length)!=length)continue ;void Server:slotDisconnected(int descriptor)for(int i=0;isocketDescriptor ()=descriptor)tcpClientSocketList.removeAt(i);return

8、;return;Tcpclient.cpp#include tcpserver.hTcpClientSocket:TcpClientSocket( QObject *parent) connect(this, SIGNAL(readyRead(),this, SLOT(dataReceived();connect(this, SIGNAL(disconnected(),this, SLOT(slotDisconnected();TcpClientSocket:TcpClientSocket()void TcpClientSocket:dataReceived()while (bytesAvai

9、lable()0)char buf1024;int length=bytesAvailable();read(buf, length);QString msg=buf;emit updateClients(msg,length);void TcpClientSocket:slotDisconnected()emit disconnected(this-socketDescriptor ();Tcpserve.cpp#include tcpserver.hTcpServer:TcpServer( QWidget *parent, Qt:WindowFlags f ) : QWidget( par

10、ent, f )QFont font(Times,12,QFont:Normal,FALSE);setFont(font);setWindowTitle(tr(TCP 网络);QVBoxLayout *vbMain = new QVBoxLayout( this );ListWidgetContent = new QListWidget( this);vbMain-addWidget( ListWidgetContent ); QHBoxLayout *hb = new QHBoxLayout( );LabelPort = new QLabel( this );LabelPort-setTex

11、t(tr(端口:);hb-addWidget( LabelPort );LineEditPort = new QLineEdit(this);hb-addWidget( LineEditPort );vbMain-addLayout(hb);PushButtonCreate = new QPushButton( this);PushButtonCreate-setText( tr( 启动 ) );vbMain-addWidget( PushButtonCreate );connect(PushButtonCreate,SIGNAL(clicked(),this,SLOT(slotCreateS

12、erver();port = 8089;LineEditPort-setText(QString:number(port);TcpServer:TcpServer()void TcpServer:slotCreateServer()server = new Server(this,port);connect(server,SIGNAL(updateServer(QString,int),this,SLOT(updateServer(QString,int);PushButtonCreate-setEnabled(false);void TcpServer:updateServer(QStrin

13、g msg,int length)ListWidgetContent-addItem (msg.left(length) );Main.cpp#include tcpserver.h#include int main( int argc, char *argv )QApplication a( argc, argv );QTextCodec:setCodecForTr(QTextCodec:codecForName(gb18030);QFont font(Times,12,QFont:Normal,FALSE);a.setFont(font);TcpServer *tcpserver = new TcpServer();tcpserver-show();return a.exec();

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号