zoukankan      html  css  js  c++  java
  • qt tcp 通信实例

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QHostAddress>
    #include <QMessageBox>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->socket = new QTcpSocket(this);
    }
    void MainWindow::on_pushButton_2_clicked(){
        this->socket->close();
    }
    void MainWindow::on_pushButton_clicked(){
        this->socket->connectToHost("127.0.0.1",80000,QTcpSocket::ReadWrite);
        connect(this->socket,SIGNAL(connected()),this,SLOT(connected()));
    }
    void MainWindow::connected(){
        QMessageBox::about(this,"notice","connect successful");
        connect(this->socket,SIGNAL(readyRead()),this,SLOT(readyread()));
    }
    void MainWindow::readyread(){
        QMessageBox::about(this,"notice","ready read");
        QByteArray arr = this->socket->readAll();
        QDataStream *des = new QDataStream(&arr,QIODevice::ReadOnly);//重点
        QString str1;
        QString str2;
        (*des)>>str1>>str2;
        qDebug()<<str1+str2;
        QMessageBox::about(this,"x",str1+str2);
    
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

      服务端

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QTcpServer>
    #include <QTcpSocket>
    #include <QMessageBox>
    #include <QByteArray>
    #include <QString>
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        this->socket = nullptr;
        this->server = new QTcpServer(this);
        this->server->listen(QHostAddress::Any,80000);
        QObject::connect(this->server,SIGNAL(newConnection()),this,SLOT(newConnect()));
    }
    void MainWindow::newConnect(){
        this->socket = this->server->nextPendingConnection();
        QMessageBox::about(this,"notice","new connect");
        connect(this->socket,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
    }
    void MainWindow::ReceiveData(){
        QByteArray arr = this->socket->readAll();
        QDataStream dst(arr);
        QString str1;
        QString str2;
        dst>>str1>>str2;
        qDebug()<<str1<<str2;
    }
    void MainWindow::on_pushButton_clicked(){
        QString str = this->ui->lineEdit->text();
        QByteArray arr;
        QDataStream dst(&arr,QIODevice::ReadWrite);
        dst<<QString("message:")<<str;
        this->socket->write(arr);
    }
    void MainWindow::on_pushButton_2_clicked(){
        this->socket->close();
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    

      

    作者:first_semon
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。如有问题,欢迎交流
  • 相关阅读:
    pip install pli 提示:Could not find a version that satisfies the requirement PIL
    关于selenium部分元素定位不到的解决办法
    ERROR 1054 (42S22): Unknown column ‘password‘ in ‘field list‘
    通过Tomcat jpress连接不到数据库
    Navicat MySQL 连接出现 Authentication plugin ‘caching_sha2_password‘ cannot be loaded
    Selenium中核心属性以及方法
    selenium中定位frame中的元素
    selenium中截屏以及按照时间格式保存到相应文件夹
    Selenium中核心属性以及方法
    selenium中关于js脚本的一些操作
  • 原文地址:https://www.cnblogs.com/first-semon/p/6925185.html
Copyright © 2011-2022 走看看