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

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

发布时间:2020-12-24 14:20:02 所属栏目:经验 来源:网络整理
导读:副标题#e# 先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面。 #ifndef TCPCLIENT_H#define TCPCLIENT_H #include QDialog#include QListWidget#include QLineEdit#include QPushButton#include QLabel#include QGridLayout#include QtNetWor
副标题[/!--empirenews.page--]

先写一个客户端,实现简单的,能加入聊天,以及加入服务器的界面。

#ifndef TCPCLIENT_H
#define TCPCLIENT_H
 
#include <QDialog>
#include <QListWidget>
#include <QLineEdit>
#include <QPushButton>
#include <QLabel>
#include <QGridLayout>
#include <QtNetWork/QHostAddress>
#include <QtNetWork/QTcpSocket>
 
class TcpClient : public QDialog
{
 Q_OBJECT
 
public:
 TcpClient(QWidget *parent = 0,Qt::WindowFlags f=0);
 ~TcpClient();
private:
 QListWidget *contentListWidget;
 QLineEdit *sendLineEdit;
 QPushButton *sendBtn;
 QLabel *userNameLabel;
 QLineEdit *userNameLineEdit;
 QLabel *serverIPLabel;
 QLineEdit *serverIPLineEdit;
 QLabel *portLabel;
 QLineEdit *portLineEdit;
 QPushButton *enterBtn;
 QGridLayout *mainLayout;
 bool status;
 int port;
 QHostAddress *serverIP;
 QString userName;
 QTcpSocket *tcpSocket;
public slots:
 void slotEnter();
 void slotConnected();
 void slotDisconnected();
 void dataReceived();
 void slotSend();
};
 
#endif // TCPCLIENT_H

有一个加入服务器的按钮,还有一个发送消息的按钮,在头文件,先定义两个函数。

#include "tcpclient.h"
#include <QMessageBox>
#include <QHostInfo>
 
TcpClient::TcpClient(QWidget *parent,Qt::WindowFlags f)
 : QDialog(parent,f)
{
 setWindowTitle(tr("TCP Client"));
 
 contentListWidget = new QListWidget;
 
 sendLineEdit = new QLineEdit;
 sendBtn = new QPushButton(tr("send"));
 
 userNameLabel = new QLabel(tr("name"));
 userNameLineEdit = new QLineEdit;
 
 serverIPLabel = new QLabel(tr("server"));
 serverIPLineEdit = new QLineEdit;
 
 portLabel = new QLabel(tr("port"));
 portLineEdit = new QLineEdit;
 
 enterBtn= new QPushButton(tr("join chat"));
 
 mainLayout = new QGridLayout(this);
 mainLayout->addWidget(contentListWidget,1,2);
 mainLayout->addWidget(sendLineEdit,0);
 mainLayout->addWidget(sendBtn,1);
 mainLayout->addWidget(userNameLabel,2,0);
 mainLayout->addWidget(userNameLineEdit,1);
 mainLayout->addWidget(serverIPLabel,3,0);
 mainLayout->addWidget(serverIPLineEdit,1);
 mainLayout->addWidget(portLabel,4,0);
 mainLayout->addWidget(portLineEdit,1);
 mainLayout->addWidget(enterBtn,5,2);
 
 status = false;
 
 port = 8010;
 portLineEdit->setText(QString::number(port));
 
 serverIP =new QHostAddress();
 
 connect(enterBtn,SIGNAL(clicked()),this,SLOT(slotEnter()));
 connect(sendBtn,SLOT(slotSend()));
 
 sendBtn->setEnabled(false);
}
 
TcpClient::~TcpClient()
{
 
}
 
void TcpClient::slotEnter()
{
 if(!status)
 {
  QString ip = serverIPLineEdit->text();
  if(!serverIP->setAddress(ip))
  {
   QMessageBox::information(this,tr("error"),tr("server ip address error!"));
   return;
  }
 
  if(userNameLineEdit->text()=="")
  {
   QMessageBox::information(this,tr("User name error!"));
   return;
  }
 
  userName=userNameLineEdit->text();
 
  tcpSocket = new QTcpSocket(this);
  connect(tcpSocket,SIGNAL(connected()),SLOT(slotConnected()));
  connect(tcpSocket,SIGNAL(disconnected()),SLOT(slotDisconnected()));
  connect(tcpSocket,SIGNAL(readyRead()),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 TcpClient::slotConnected()
{
 sendBtn->setEnabled(true);
 enterBtn->setText(tr("离开"));
 
 int length=0;
 QString msg=userName+tr(":Enter Chat Room");
 if((length=tcpSocket->write(msg.toLatin1(),msg.length()))!=msg.length())
 {
  return;
 }
}
 
void TcpClient::slotSend()
{
 if(sendLineEdit->text()=="")
 {
  return ;
 }
 
 QString msg=userName+":"+sendLineEdit->text();
 
 tcpSocket->write(msg.toLatin1(),msg.length());
 sendLineEdit->clear();
}
 
void TcpClient::slotDisconnected()
{
 sendBtn->setEnabled(false);
 enterBtn->setText(tr("join chat"));
}
 
void TcpClient::dataReceived()
{
 while(tcpSocket->bytesAvailable()>0)
 {
  QByteArray datagram;
  datagram.resize(tcpSocket->bytesAvailable());
 
  tcpSocket->read(datagram.data(),datagram.size());
 
  QString msg=datagram.data();
  contentListWidget->addItem(msg.left(datagram.size()));
 }
}

实现界面布局。在Enter槽函数中,确定加入还是离开的服务器的功能。如果加入了,就将消息,写到tcpsocket中,构造消。

服务端的头文件:

#ifndef TCPSERVER_H
#define TCPSERVER_H
 
#include <QDialog>
#include <QListWidget>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGridLayout>
#include "server.h"
 
class TcpServer : public QDialog
{
 Q_OBJECT
 
public:
 TcpServer(QWidget *parent = 0,Qt::WindowFlags f=0);
 ~TcpServer();
private:
 QListWidget *ContentListWidget;
 QLabel *PortLabel;
 QLineEdit *PortLineEdit;
 QPushButton *CreateBtn;
 QGridLayout *mainLayout;
 int port;
 Server *server;
public slots:
 void slotCreateServer();
 void updateServer(QString,int);
};
 
#endif // TCPSERVER_H

(编辑:南平站长网)

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

热点阅读