基于TCP的网络聊天程序.docx_第1页
基于TCP的网络聊天程序.docx_第2页
基于TCP的网络聊天程序.docx_第3页
基于TCP的网络聊天程序.docx_第4页
基于TCP的网络聊天程序.docx_第5页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

基于TCP的网络聊天程序问题概述:网络聊天是网络应用中的一种常见的功能。TCP是一种可靠的、面向连接、面向数据流的传输协议,多数网络协议都使用TCP协议,包括HTTP和FTP,TCP协议非常适合数据的连续传输。技术难点:TCP通信协议;QTcpsocket的使用。实践方案:在linux_ubuntu下使用qt_eclipse 编写c+程序。由面向对象的思想,为了实现程序,在server终端部分有3个类:1. serve2. tcpclient3. tcpserveServe类是继承自QTcpServer,实现TCP协议的服务器;Tcpclient 继承自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 updateClients(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 disconnected(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* LineEditPort;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)TcpClientSocket *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);tcpClientSocketList.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;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 (bytesAvailable()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( parent, 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-setText(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(slotCreateServer();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(QString msg,int length)ListWidgetContent-addItem (msg.left(length) );Main.cpp#include tcpserver.h#include int main( int argc, char *argv )QApplication a( argc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论