QT 基于TCP协议的网络通信

QT下基于TCP协议的网络通信分为服务端和客服端程序两部分,与之前写的C网络通信不同的是QT下服务器端只需要定义一个服务器对象和用来进行通信的TcpSocket,只需通过监听和连接实现与客服端的连接;客服端中需要定义一个Tcpsocket,通过connectToHost(IP,端口号)进行连接。部分源代码如下:


服务器端:

widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
 
#include <QTcpServer>   // tcp 服务器类,=====> 负责监听是否有客户端连接
#include <QTcpSocket>   // tcp socket  ======> 负责tcp的通信
 
namespace Ui {
class widget;
}
 
class widget : public QWidget
{
    Q_OBJECT
 
public:
    explicit widget(QWidget *parent = 0);
    ~widget();
public slots:
    void newClient();
    void readData();
 
 
private:
    Ui::widget *ui;
    QTcpServer server;   //  定义一个服务器对象
};
 
#endif // WIDGET_H

chat.h

#ifndef CHAT_H
#define CHAT_H
 
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
 
namespace Ui {
class Chat;
}
 
class Chat : public QWidget
{
    Q_OBJECT
 
public:
    explicit Chat(QTcpServer *tcpServer,QWidget *parent = 0);
    ~Chat();
 
private slots:
    void on_pushButton_clicked();
 
private:
    Ui::Chat *ui;
    QTcpSocket *s;
};
 
#endif // CHAT_H

 

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <chat.h>
 
 
widget::widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget)
{
    ui->setupUi(this);
    // 监听本地的 ip 和端口
    server.listen(QHostAddress::Any, 9999);
 
    connect(&server, SIGNAL(newConnection()), this, SLOT(newClient()));
 
 
}
 
 
widget::~widget()
{
    delete ui;
}
 
 
void widget::newClient()
{
    while(server.hasPendingConnections())   //一次接受所有客服端的请求
    {
       Chat *c = new Chat(&server);
       c->show();
       c->setAttribute(Qt::WA_DeleteOnClose);
 
    }
}
 
void widget::readData()
{
 
}


chat.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <chat.h>
 
 
widget::widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget)
{
    ui->setupUi(this);
    // 监听本地的 ip 和端口
    server.listen(QHostAddress::Any, 9999);
 
    connect(&server, SIGNAL(newConnection()), this, SLOT(newClient()));
 
 
}
 
 
widget::~widget()
{
    delete ui;
}
 
 
void widget::newClient()
{
    while(server.hasPendingConnections())   //一次接受所有客服端的请求
    {
       Chat *c = new Chat(&server);
       c->show();
       c->setAttribute(Qt::WA_DeleteOnClose);
 
    }
}
 
void widget::readData()
{
 
}



客服端:

widget.h

#ifndef WIDGET_H
#define WIDGET_H
 
#include <QWidget>
#include <QTcpSocket>
 
namespace Ui {
class widget;
}
 
class widget : public QWidget
{
    Q_OBJECT
 
public:
    explicit widget(QWidget *parent = 0);
    ~widget();
 
private slots:
    void on_pushButton_2_clicked();
 
    void on_pushButton_clicked();
 
private:
    Ui::widget *ui;
    QTcpSocket socket;           //用来和服务器进行通信
};
 
#endif // WIDGET_H

 

widget.cpp

#include "widget.h"
#include "ui_widget.h"
#include <QHostAddress>
#include <QMessageBox>
 
widget::widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget)
{
    ui->setupUi(this);
 
    connect(&socket,&QTcpSocket::connected,
            [this](){
        ui->pushButton->setEnabled(true);
        ui->pushButton_2->setEnabled(false);
        QMessageBox::information(this,"连接服务器","已经连接服务器");
 
    });
    connect(&socket,&QTcpSocket::readyRead,
            [this]{
 
        while(socket.bytesAvailable())
        {
            QByteArray data = socket.readAll();
            QString str = QString("服务返回消息:%1").arg(QString(data));
            ui->textEdit->append(str);
        }
 
 
    });
}
 
widget::~widget()
{
    delete ui;
}
 
 
 
void widget::on_pushButton_2_clicked()
{
    QString ip = ui->lineEdit->text();
    qint16 port=ui->lineEdit_2->text().toInt();
    socket.connectToHost(QHostAddress(ip),port);
}
 
void widget::on_pushButton_clicked()
{
    QString data = ui->textEdit->toPlainText();
    socket.write(data.toUtf8());
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值