• Qt TCP


    一、介绍

      传输控制协议(TCP,Transmission Control Protocol)是一种面向连接的、可靠的、基于字节流的传输层通信协议,TCP是为了在不可靠的互联网络上提供可靠的端到端字节流而专门设计的一个传输协议。

    二、界面展示

      

    三、实现TCP

      ① 首先在Qt中实现TCP需要在工程pro中加入 QT += network

      ② main.cpp

    #include "servewidget.h"
    #include <QApplication>
    #include"clientwidget.h"
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        ServeWidget server;
        server.show();
    
        ClientWidget client;
        client.move(0,0);
    
        client.show();
    
        return a.exec();
    }
    ClientWidget.cpp
    #include "clientwidget.h"
    #include "ui_clientwidget.h"
    #include <QHostAddress>
    #include <QDebug>
    ClientWidget::ClientWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ClientWidget)
    {
        ui->setupUi(this);
        //初始化套接字
        tcpSocket = nullptr;
        //分配空间,指定父对象
        tcpSocket = new QTcpSocket(this);
        //建立连接的信号槽
        connect(tcpSocket,&QTcpSocket::connected,
                [=]()
                {
                     ui->textEditRead->setText("恭喜,成功连接服务器!");
                }
                );
        //连接建立后收到服务器数据的信号槽
        connect(tcpSocket,&QTcpSocket::readyRead,
                [=]()
               {
             //获取对方发送的内容
            QByteArray array = tcpSocket->readAll();
            //显示到编辑框
            ui->textEditRead->append(array);//append,添加内容
                }
                );
        //设置窗口标题
        setWindowTitle("TCP客户端v");
    }
    
    ClientWidget::~ClientWidget()
    {
        delete ui;
    }
    
    void ClientWidget::on_buttonConnect_clicked()
    {
        //获取服务器IP和端口
        QString ip = ui->lineEditIP->text();
        qint16 port = ui->lineEditPort->text().toInt();
        //主动连接服务器
        tcpSocket->connectToHost(QHostAddress(ip),port);
    }
    
    void ClientWidget::on_buttonSend_clicked()
    {
        //获取编辑框内容
         QString str = ui->textEditWrite->toPlainText();
         //发送数据
         tcpSocket->write( str.toUtf8().data() );
    
    }
    
    void ClientWidget::on_buttonClose_clicked()
    { 
        //主动断开连接
        tcpSocket->disconnectFromHost();
    }
    ServeWidget.cpp
    #include "servewidget.h"
    #include "ui_servewidget.h"
    
    ServeWidget::ServeWidget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::ServeWidget)
    {
        ui->setupUi(this);
        //初始化套接字
        tcpServer = nullptr;
        tcpSocket = nullptr;
        //监听套接字,指定父对象自动回收空间
        tcpServer = new QTcpServer(this);
        //设定端口
        tcpServer->listen(QHostAddress::Any,8888);
        //新连接建立的信号槽
        connect(tcpServer,&QTcpServer::newConnection,[=]()
        {
        //取出建立好连接的套接字
            tcpSocket = tcpServer->nextPendingConnection();
        //获取对方的IP和端口
        QString ip = tcpSocket->peerAddress().toString();
        quint16 port = tcpSocket->peerPort();
        //将信息显示到UI
        QString temp = QString("[%1:%2]:成功连接").arg(ip).arg(port);
        ui->textEditRead->setText(temp);
        //连接建立后,读取到数据的信号槽
        connect(tcpSocket,&QTcpSocket::readyRead,
                [=]()
                {
                 //从通信套接字取出内容
                 QByteArray array = tcpSocket->readAll();
                 //显示到UI
                 ui->textEditRead->append(array);
                } );
        });
        setWindowTitle("TCP服务器,端口:8888");
    }
    
    ServeWidget::~ServeWidget()
    {
        delete ui;
    }
    
    void ServeWidget::on_ButtonSend_clicked()
    {
        if(nullptr == tcpSocket)
        {
            return;
        }
        //获取编辑区内容
        QString str = ui->textEditWrite->toPlainText();
        //给对方发数据,使用的套接字为tcpSocket
        tcpSocket->write( str.toUtf8().data() );
    }
    
    void ServeWidget::on_ButtonClose_clicked()
    {
        if(nullptr == tcpSocket)
        {
            return;
        }
        //主动和客户端断开连接
        tcpSocket->disconnectFromHost();
        //关闭套接字
        tcpSocket->close();
        tcpSocket = nullptr;
    
    }

    四、代码地址

      https://download.csdn.net/download/jiangCsion/11970339

      

  • 相关阅读:
    抓包来看ftp状态码
    socket基础篇
    密码复杂度检查函数
    time模块
    读取日志文件,搜索关键字,打印关键字前5行。yield、deque实例
    装饰器--函数
    yield用法
    字符编码
    pycharm + git实现两台电脑代码同步
    PyCharm常见用法
  • 原文地址:https://www.cnblogs.com/jiangson/p/11833747.html
Copyright © 2020-2023  润新知