Qt实现TcpClient和TcpServer连接收发数据

Qt实现TcpClient和TcpServer连接收发数据,TcpClent实现上位机给串口发送指令,没有什么问题了,可是让下位机做出相应的指令。
首先在TcpClient和TcpServer中的pro加入QT+=network。
界面设计:
Qt实现TcpClient和TcpServer连接收发数据
文章图片
Qt实现TcpClient和TcpServer连接收发数据
文章图片

接受指令设计:
tcpclient.h

#ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include #include class QTcpSocket; namespace Ui { class MainWindow; }class MainWindow : public QMainWindow { Q_OBJECTpublic: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; QTcpSocket *tcpSocket; QString currentForture; quint16 blockSize; private slots: void newConnect(); void readMessage(); void displayError(QAbstractSocket::SocketError); //void on_connectButton_clicked(); void on_connectpushButton_clicked(); }; #endif // MAINWINDOW_H


tcpclient.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); tcpSocket=new QTcpSocket(); connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readMessage())); connect(tcpSocket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(displayError(QAbstractSocket::SocketError))); }MainWindow::~MainWindow() { delete ui; }void MainWindow::newConnect() { //初始化数据大小信息为0 blockSize=0; //取消已有的连接 tcpSocket->abort(); //tcpSocket->connectToHost(ui->hostlineEdit->text(),ui->portlineEdit_2->text().toInt()); tcpSocket->connectToHost(QHostAddress::LocalHost,4196); }void MainWindow::readMessage() { QByteArray block; QDataStream in(tcpSocket); //QDataStream out(&block,QIODevice::ReadWrite); //设置数据流版本 in.setVersion(QDataStream::Qt_5_6); //out.setVersion(QDataStream::Qt_5_6); //如果是刚开始接受数据 if(blockSize==0) { if(tcpSocket->bytesAvailable()<(int)sizeof(quint16)) { //将接收到的数据存放在变量中 in>>blockSize; } if(tcpSocket->bytesAvailable()>nextFortune; if(nextFortune==currentForture) { QTimer::singleShot(0,this,&MainWindow::newConnect); return; } currentForture=nextFortune; //显示接收到的数据 qDebug()<messagelabel->setText(currentForture); }}void MainWindow::displayError(QAbstractSocket::SocketError) { qDebug()<errorString(); }//连接按钮 void MainWindow::on_connectpushButton_clicked() { newConnect(); }



发送指令设计:
server.h
#ifndef SERVER_H #define SERVER_H#include #include class QTcpServer; namespace Ui { class server; }class server : public QMainWindow { Q_OBJECTpublic: explicit server(QWidget *parent = 0); ~server(); private: Ui::server *ui; QTcpServer *tcpServer; private slots: void sendMessage(); }; #endif // SERVER_H

【Qt实现TcpClient和TcpServer连接收发数据】
server.cpp

#include "server.h" #include "ui_server.h" #include server::server(QWidget *parent) : QMainWindow(parent), ui(new Ui::server) { //QHostAddress &address="10.20.15.101"; ui->setupUi(this); tcpServer=new QTcpServer(this); if(!tcpServer->listen(QHostAddress::LocalHost,4196)) { qDebug()<<"错误"<errorString(); close(); } connect(tcpServer,SIGNAL(newConnection()),this,SLOT(sendMessage())); }server::~server() { delete ui; }void server::sendMessage() { QByteArray block; QDataStream out(&block,QIODevice::WriteOnly); out.setVersion(QDataStream::Qt_5_6); out<<(quint16)0; out<seek(0); out<<(quint16)(block.size()-sizeof(quint16)); //获取已经建立的连接的套接字 QTcpSocket *clientConnection=tcpServer->nextPendingConnection(); connect(clientConnection,SIGNAL(disconnected()),clientConnection, SLOT(deleteLater())); clientConnection->write(block); clientConnection->disconnectFromHost(); //发送数据 ui->label->setText("send message successful!!!"); }


Qt实现TcpClient和TcpServer连接收发数据
文章图片







    推荐阅读