QTcpSocket network socket communication

Realize server-side and client-side, the receiving method adopts the slot receiving method, and the official documents can be viewed by F1 F2 during the process

Server

New class, select base class

Source File

.h file

#ifndef SOCKET_H
#define SOCKET_H


#include <QObject>
#include <qtcpserver.h>
#include <qtcpsocket.h>


class socket : public QObject
{
    Q_OBJECT
public:
    explicit socket(int port,QObject *parent = nullptr);


    QTcpServer *tcpServer;
    QTcpSocket *tcpSocket;
    void WriteData(QByteArray bytes);
public slots:
    void acceptConnection();
    void readData();
};


#endif // SOCKET_H

.cpp file

#include "socket.h"


socket::socket(int port, QObject *parent) : QObject(parent)
{
    tcpServer = new QTcpServer();
    tcpServer->listen(QHostAddress::Any,port);
    tcpSocket = nullptr;
    connect(tcpServer,SIGNAL(newConnection()),this,SLOT(acceptConnection()));
}


void socket::acceptConnection(){
    qDebug()<<"connect";
    tcpSocket = tcpServer->nextPendingConnection();
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readData()));
    connect(tcpSocket, &QAbstractSocket::disconnected,tcpSocket, &QObject::deleteLater);
}


void socket::readData(){
    QByteArray bytes = tcpSocket->readAll();
    qDebug()<<bytes;
}


void socket::WriteData(QByteArray bytes){
    if(tcpSocket->isOpen()){
        tcpSocket->write(bytes);
    }
}

Points to note

  1. Need to quote two header files qtcpserver.h and qtcpsocket.h, two of which contain the server and the client
  2. The server is waiting for connection, and there is a connection that needs to use socket to communicate, inside the readyData signal
  3. When the usage is initialized in the main interface, socket *m = new socket(6789);

Client

Create a new class, select the base class, the same as the server

Source File

.cpp

#include "socket.h"
#include "servo.h"
#include "vrep.h"


socket::socket(QObject *parent) : QObject(parent)
{
    tcpSocket = new QTcpSocket(parent);
    connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readData()));
}


void socket::ConnectServer(const QString &hostName,int port){
    tcpSocket->connectToHost(hostName,port);
}
void socket::readData(){
    QByteArray bytes = tcpSocket->readAll();
    qDebug()<<bytes;
}


void socket::WriteData(QByteArray bytes){
    if(tcpSocket!=nullptr && tcpSocket->isOpen()){
        tcpSocket->write(bytes);
    }
    else
        qDebug()<<"disconnection";
}

.h

#ifndef SOCKET_H
#define SOCKET_H


#include <QObject>
#include <qtcpserver.h>
#include <qtcpsocket.h>

class socket : public QObject
{
    Q_OBJECT
public:
    explicit socket(QObject *parent = nullptr);


    QByteArray m_bytes;
    QTcpSocket *tcpSocket;


    void ConnectServer(const QString &hostName,int port);
    void WriteData(QByteArray bytes);
    uint8_t getCheck(QByteArray bytes);
private:
    int receiveLength;
public slots:
    void readData();
};

#endif // SOCKET_H

note

The instantiation needs to be in an accessible ui thread

m_socket = new socket(this);

Guess you like

Origin blog.csdn.net/shaynerain/article/details/106130986