加入收藏 | 设为首页 | 会员中心 | 我要投稿 武汉站长网 (https://www.027zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 创业 > 经验 > 正文

QT网络编程Tcp下C/S架构的即时通信实例

发布时间:2020-12-24 16:05:11 所属栏目:经验 来源:网络整理
导读:先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面。 #ifndef TCPCLIENT_H#define TCPCLIENT_H #include QDialog#include QListWidget#include QLineEdit#include QPushButton#include QLabel#include QGridLayout#include QtNetWork/QHostAddr

这是服务端的界面的,把消息显示而已。实现这个布局。

#include "tcpserver.h"
 
TcpServer::TcpServer(QWidget *parent,f)
{
 setWindowTitle(tr("TCP Server"));
 
 ContentListWidget = new QListWidget;
 
 PortLabel = new QLabel(tr(" port"));
 PortLineEdit = new QLineEdit;
 
 CreateBtn = new QPushButton(tr("create chat"));
 mainLayout = new QGridLayout(this);
 mainLayout->addWidget(ContentListWidget,2);
 mainLayout->addWidget(PortLabel,0);
 mainLayout->addWidget(PortLineEdit,1);
 mainLayout->addWidget(CreateBtn,2);
 
 port=8010;
 PortLineEdit->setText(QString::number(port));
 
 connect(CreateBtn,SLOT(slotCreateServer()));
}
 
TcpServer::~TcpServer()
{
 
}
 
void TcpServer::slotCreateServer()
{
 server = new Server(this,port);
 connect(server,SIGNAL(updateServer(QString,int)),SLOT(updateServer(QString,int)));
 
 CreateBtn->setEnabled(false);
}
 
void TcpServer::updateServer(QString msg,int length)
{
 ContentListWidget->addItem(msg.left(length));
}

创建TCP的套接字,以便实现服务端和客户端的通信。

#ifndef TCPCLIENTSOCKET_H
#define TCPCLIENTSOCKET_H
 
#include <QtNetWork/QTcpSocket>
#include <QObject>
 
class TcpClientSocket : public QTcpSocket
{
 Q_OBJECT
public:
 TcpClientSocket();
signals:
 void updateClients(QString,int);
 void disconnected(int);
protected slots:
 void dataReceived();
 void slotDisconnected();
};
 
#endif // TCPCLIENTSOCKET_H

以上是头文件,具体的是:

#include "tcpclientsocket.h"
 
TcpClientSocket::TcpClientSocket()
{
 connect(this,SLOT(dataReceived()));
 connect(this,SLOT(slotDisconnected()));
}
 
void TcpClientSocket::dataReceived()
{
 while(bytesAvailable()>0)
 {
  int length = bytesAvailable();
  char buf[1024];
  read(buf,length);
 
  QString msg=buf;
  emit updateClients(msg,length);
 }
}
 
void TcpClientSocket::slotDisconnected()
{
 emit disconnected(this->socketDescriptor());
}

实现服务器,头文件:

#ifndef SERVER_H
#define SERVER_H
 
#include <QtNetWork/QTcpServer>
#include <QObject>
#include "tcpclientsocket.h"
 
class Server : public QTcpServer
{
 Q_OBJECT
public:
 Server(QObject *parent=0,int port=0);
 QList<TcpClientSocket*> tcpClientSocketList;
signals:
 void updateServer(QString,int);
public slots:
 void updateClients(QString,int);
 void slotDisconnected(int);
protected:
 void incomingConnection(int socketDescriptor);
};
 
#endif // SERVER_H
#include "server.h"
 
Server::Server(QObject *parent,int port)
 :QTcpServer(parent)
{
 listen(QHostAddress::Any,port);
}
 
void Server::incomingConnection(int socketDescriptor)
{
 TcpClientSocket *tcpClientSocket = new TcpClientSocket;
 connect(tcpClientSocket,SIGNAL(updateClients(QString,SLOT(updateClients(QString,int)));
 connect(tcpClientSocket,SIGNAL(disconnected(int)),SLOT(slotDisconnected(int)));
 
 tcpClientSocket->setSocketDescriptor(socketDescriptor);
 
 tcpClientSocketList.append(tcpClientSocket);
}
 
void Server::updateClients(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;
}

实现后的界面:

QT网络编程Tcp下C/S架构的即时通信实例

以上这篇QT网络编程Tcp下C/S架构的即时通信实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。

(编辑:武汉站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读