QT tcp通信

QT tcp通信

TcpTest.pro

cpp 复制代码
QT       += core gui network

CONFIG += C++11

main.cpp

cpp 复制代码
#include "serverwidget.h"
#include <QApplication>

#include "clientwidget.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    ServerWidget w;
    w.show();

    ClientWidget w2;
    w2.show();

    return a.exec();
}

serverwidget.h

cpp 复制代码
#include <QTcpServer>  // 监听套接字
#include <QTcpSocket>  // 通信套接字

private slots:
    void on_pushButtonsend_clicked();

    void on_pushButton_2close_clicked();

private:
    

    QTcpServer *tcpServer; // 监听套接字
    QTcpSocket *tcpSocket; // 通信套接字

};

serverwidget.cpp

cpp 复制代码
#include "serverwidget.h"
#include "ui_serverwidget.h"
#include <QDebug>

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

    tcpServer = NULL;
    tcpSocket = NULL;

    // 监听套接字,指定父对象让其自动回收空间
    tcpServer = new QTcpServer(this);
    tcpServer->listen(QHostAddress::Any, 8888);

    setWindowTitle("服务器: 8888");

    connect(tcpServer, &QTcpServer::newConnection, [=](){
       // 取出建立好连接的套接字
        tcpSocket = tcpServer->nextPendingConnection();
        // 获取对方的ip和端口
        QString ip = tcpSocket->peerAddress().toString();
        // qint16 port = tcpSocket->peerPort();
				quint16 port = tcpSocket->peerPort();
        // qDebug() << port;
        QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
        ui->textEdit->setText(temp);

        connect(tcpSocket, &QTcpSocket::readyRead, [=](){
           // 从通信套接字中取出内容
            QByteArray array = tcpSocket->readAll();
            ui->textEdit->append(array);
        });

    });

}

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

void ServerWidget::on_pushButtonsend_clicked()
{
    if(NULL == tcpSocket){
        return;
    }

    // 获取编辑区内容
    QString str = ui->textEdit_2->toPlainText();
    // 给对方发送数据 使用套接字是tcpSocket
    tcpSocket->write( str.toUtf8().data() );

}

void ServerWidget::on_pushButton_2close_clicked()
{
    if(NULL == tcpSocket){
        return;
    }

    // 主动和客户端断开连接
    tcpSocket->disconnectFromHost();
    tcpSocket->close();
    tcpSocket = NULL;
}

clientwidget.h

cpp 复制代码
#include <QTcpSocket> // 通信套接字

private slots:
    void on_pushButtonconn_clicked();

    void on_pushButton_2send_clicked();

    void on_pushButton_3close_clicked();

private:
    

    QTcpSocket *tcpsocket; // 通信套接字

clientwidget.cpp

cpp 复制代码
#include "clientwidget.h"
#include "ui_clientwidget.h"

#include <QHostAddress>

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

    tcpsocket = NULL;

    // 分配空间,指定父对象
    tcpsocket = new QTcpSocket(this);

    setWindowTitle("客户端");

    connect(tcpsocket, &QTcpSocket::connected, [=](){
       ui->textEdit->setText("成功和服务器建立好连接");
    });

    connect(tcpsocket, &QTcpSocket::readyRead, [=](){
        // 获取对方发送的内容 从通信套接字中取出内容
        QByteArray array = tcpsocket->readAll();
        // 追加到编辑区中
        ui->textEdit->append(array);
    });

}

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

void ClientWidget::on_pushButtonconn_clicked()
{
    // 获取服务器ip和端口
    QString ip = ui->lineEdit_2IP->text();
    quint16 port = ui->lineEditport->text().toInt();

    // 主动和服务器建立连接
    tcpsocket->connectToHost(QHostAddress(ip), port);

}

void ClientWidget::on_pushButton_2send_clicked()
{
    // 获取编辑框内容
    QString str = ui->textEdit_2->toPlainText();
    // 发送数据
    tcpsocket->write( str.toUtf8().data() );
}

void ClientWidget::on_pushButton_3close_clicked()
{
    // 主动和对方断开连接
    tcpsocket->disconnectFromHost();
    tcpsocket->close();

}

serverwidget.ui

clientwidget.ui

相关推荐
群联云防护小杜24 分钟前
深度隐匿源IP:高防+群联AI云防护防绕过实战
运维·服务器·前端·网络·人工智能·网络协议·tcp/ip
笑衬人心。36 分钟前
TCP 拥塞控制算法 —— 慢启动(Slow Start)笔记
笔记·tcp/ip·php
有书Show10 小时前
个人IP的塑造方向有哪些?
网络·网络协议·tcp/ip
HHRL-yx10 小时前
C++网络编程 5.TCP套接字(socket)通信进阶-基于多线程的TCP多客户端通信
网络·c++·tcp/ip
肥or胖11 小时前
【FFmpeg 快速入门】本地播放器 项目
开发语言·qt·ffmpeg·音视频
快乐觉主吖12 小时前
Unity网络通信的插件分享,及TCP粘包分包问题处理
tcp/ip·unity·游戏引擎
iblade14 小时前
网络:TCP序列号和滑动窗口,顺序保证
网络·tcp/ip·php
HHRL-yx16 小时前
C++网络编程 2.TCP套接字(socket)编程详解
网络·c++·tcp/ip
景彡先生16 小时前
STM32以太网开发详解:基于LwIP协议栈实现TCP/UDP通信(附网络摄像头案例)
网络·stm32·tcp/ip
R-G-B16 小时前
【27】MFC入门到精通——MFC 修改用户界面登录IP IP Address Control
tcp/ip·ui·mfc·mfc 用户界面登录·mfc ip登录·mfc address登录