• 大小: 19KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: 其他
  • 标签: TCP  多客户端  

资源简介

QT TCP一个服务端连接多个客户端通信,欢迎大家参考!

资源截图

代码片段和文件信息

#include “frmmain.h“
#include “ui_frmmain.h“
#include “myhelper.h“

frmMain::frmMain(QWidget *parent) :
    QWidget(parent)
    ui(new Ui::frmMain)
{
    ui->setupUi(this);

    FormHelper::FormInCenter(this);
    FormHelper::FormNotResize(this);    

    this->Init();
}

frmMain::~frmMain()
{
    delete ui;
}

void frmMain::Init()
{
    ui->btnSend->setEnabled(false);
    ui->txtReceive->setReadOnly(true);
    tcpClient=new QTcpSocket(this);
    tcpClient->abort();//取消原有连接
    connect(tcpClientSIGNAL(readyRead())thisSLOT(ReadData()));
    connect(tcpClientSIGNAL(error(QAbstractSocket::SocketError))thisSLOT(ReadError(QAbstractSocket::SocketError)));
}

void frmMain::ReadData()
{
    QByteArray buffer=tcpClient->readAll();
    if (!buffer.isEmpty())
    {
        if (ui->ckHexReceive->isChecked())
        {
            ui->txtReceive->append(“接收数据:“+myHelper::ByteArrayToHexStr(buffer));
        }
        else
        {
            ui->txtReceive->append(“接收数据:“+buffer);
        }
    }
}

void frmMain::ReadError(QAbstractSocket::SocketError)
{
    tcpClient->disconnectFromHost();
    ui->btnConnect->setText(“连接“);
    ui->txtReceive->append(tr(“连接服务器失败原因:%1“).arg(tcpClient->errorString()));
}

void frmMain::on_btnConnect_clicked()
{
    if (ui->btnConnect->text()==“连接“)
    {
        tcpClient->connectToHost(ui->txtIP->text()ui->txtPort->text().toInt());
        if (tcpClient->waitForConnected(1000))
        {
            ui->btnConnect->setText(“断开“);
            ui->txtReceive->append(“连接服务器成功“);
            ui->btnSend->setEnabled(true);
        }
    }
    else
    {
        tcpClient->disconnectFromHost();
        tcpClient->waitForDisconnected(1000);
        if (tcpClient->state() == QAbstractSocket::UnconnectedState || tcpClient->waitForDisconnected(1000))
        {
            ui->btnConnect->setText(“连接“);
            ui->txtReceive->append(“断开连接成功“);
            ui->btnSend->setEnabled(true);
        }
    }
}

void frmMain::on_btnClear_clicked()
{
    ui->txtReceive->clear();
}

void frmMain::on_btnSend_clicked()
{
    QString data=ui->txtSend->text();

    if (data!=““)
    {
        if (ui->ckHexSend->isChecked())
        {
            tcpClient->write(myHelper::HexStrToByteArray(data));
        }
        else
        {
            tcpClient->write(data.toLatin1());
        }
    }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2016-05-13 17:01  Qt TCP多客户端通信\
     目录           0  2016-05-07 11:43  Qt TCP多客户端通信\myTcpClient\
     文件        2534  2016-05-05 17:09  Qt TCP多客户端通信\myTcpClient\frmmain.cpp
     文件         559  2013-09-03 15:44  Qt TCP多客户端通信\myTcpClient\frmmain.h
     文件        5036  2013-09-04 17:05  Qt TCP多客户端通信\myTcpClient\frmmain.ui
     文件         278  2016-05-05 17:06  Qt TCP多客户端通信\myTcpClient\main.cpp
     文件       15044  2016-05-05 17:04  Qt TCP多客户端通信\myTcpClient\myhelper.h
     文件         505  2016-05-05 14:04  Qt TCP多客户端通信\myTcpClient\myTcpClient.pro
     文件       18266  2016-05-05 17:22  Qt TCP多客户端通信\myTcpClient\myTcpClient.pro.user
     目录           0  2016-05-13 16:36  Qt TCP多客户端通信\myTcpServer\
     文件        3568  2016-05-13 16:36  Qt TCP多客户端通信\myTcpServer\frmmain.cpp
     文件         770  2016-05-10 10:14  Qt TCP多客户端通信\myTcpServer\frmmain.h
     文件        4535  2016-05-06 13:26  Qt TCP多客户端通信\myTcpServer\frmmain.ui
     文件         187  2016-05-13 15:33  Qt TCP多客户端通信\myTcpServer\main.cpp
     文件         905  2016-05-10 17:39  Qt TCP多客户端通信\myTcpServer\myclientsocket.cpp
     文件         519  2013-09-04 09:49  Qt TCP多客户端通信\myTcpServer\myclientsocket.h
     文件        2268  2016-05-13 15:32  Qt TCP多客户端通信\myTcpServer\mytcpserver.cpp
     文件         921  2016-05-13 15:13  Qt TCP多客户端通信\myTcpServer\mytcpserver.h
     文件         590  2016-05-06 14:00  Qt TCP多客户端通信\myTcpServer\myTcpServer.pro
     文件       18266  2016-05-11 18:17  Qt TCP多客户端通信\myTcpServer\myTcpServer.pro.user

评论

共有 条评论