Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize simple TCP Communication with QT5

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "how to achieve simple TCP communication in QT5". In daily operation, I believe many people have doubts about how to achieve simple TCP communication in QT5. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to achieve simple TCP communication in QT5". Next, please follow the editor to study!

Network sockets using QT need to add a sentence to the .pro file:

QT + = network 1, client

1. The code on the client side is a little simpler than that on the server. Generally speaking, using the QTcpSocket class in QT to communicate with the server requires only the following 5 steps:

(1) create a QTcpSocket socket object

Socket = new QTcpSocket ()

(2) use this object to connect to the server

Socket- > connectToHost (IP, port)

(3) use the write function to send data to the server

Socket- > write (data)

(4) when new data arrives in the socket receiving buffer, a readRead () signal is sent, so a slot function is added to the signal to read the data.

QObject::connect (socket, & QTcpSocket::readyRead, this, & MainWindow::socket_Read_Data); void MainWindow::socket_Read_Data () {QByteArray buffer; / / read buffer data buffer = socket- > readAll ();}

(5) disconnect from the server (for the difference between close () and disconnectFromHost (), press F1 for help)

Socket- > disconnectFromHost ()

2. The following are the client routines

The first is the mainwindow.h file:

/ / mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H# include # include namespace Ui {class MainWindow;} class MainWindow: public QMainWindow {Q_OBJECT public: explicit MainWindow (QWidget * parent = 0); ~ MainWindow (); private slots: void on_pushButton_Connect_clicked (); void on_pushButton_Send_clicked (); void socket_Read_Data (); void socket_Disconnected (); private: Ui::MainWindow * ui; QTcpSocket * socket;} # endif / / MAINWINDOW_H

Then there is the mainwindow.cpp file:

/ / mainwindow.cpp#include "mainwindow.h" # include "ui_mainwindow.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); socket = new QTcpSocket (); / / Connect signal slot QObject::connect (socket, & QTcpSocket::readyRead, this, & MainWindow::socket_Read_Data) QObject::connect (socket, & QTcpSocket::disconnected, this, & MainWindow::socket_Disconnected); ui- > pushButton_Send- > setEnabled (false); ui- > lineEdit_IP- > setText ("127.0.0.1"); ui- > lineEdit_Port- > setText ("8765");} MainWindow::~MainWindow () {delete this- > socket; delete ui } void MainWindow::on_pushButton_Connect_clicked () {if (ui- > pushButton_Connect- > text () = = tr ("connection")) {QString IP; int port; / / get the IP address IP = ui- > lineEdit_IP- > text (); / / get the port number port = ui- > lineEdit_Port- > text (). ToInt () / / cancel the existing connection socket- > abort (); / / Connect the server socket- > connectToHost (IP, port); / / wait for the connection to succeed if (! socket- > waitForConnected (30000)) {qDebug () setEnabled (true); / / modify the key text ui- > pushButton_Connect- > setText ("disconnect") } else {/ / disconnect socket- > disconnectFromHost (); / / modify the key words ui- > pushButton_Connect- > setText ("connection"); ui- > pushButton_Send- > setEnabled (false);}} void MainWindow::on_pushButton_Send_clicked () {qDebug () toPlainText () / / get the contents of the text box and send socket- > write (ui- > textEdit_Send- > toPlainText (). ToLatin1 ()); socket- > flush ();} void MainWindow::socket_Read_Data () {QByteArray buffer; / / read buffer data buffer = socket- > readAll (); if (! buffer.isEmpty ()) {QString str = ui- > textEdit_Recv- > toPlainText (); str+=tr (buffer) / / refresh the display ui- > textEdit_Recv- > setText (str);}} void MainWindow::socket_Disconnected () {/ / send button failure ui- > pushButton_Send- > setEnabled (false); / / modify the keystroke text ui- > pushButton_Connect- > setText ("connection"); qDebug () listen (QHostAddress::Any, port)

(3) when the server is accessed by the client, it sends a newConnection () signal, so add a slot function to the signal and accept the client access with a QTcpSocket object.

Connect (server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect); void MainWindow::server_New_Connect () {/ / get client connection socket = server- > nextPendingConnection ();}

(4) use the write function of socket to send data to the client

Socket- > write (data)

(5) when new data arrives in the socket receiving buffer, a readRead () signal is sent, so a slot function is added to the signal to read the data.

QObject::connect (socket, & QTcpSocket::readyRead, this, & MainWindow::socket_Read_Data); void MainWindow::socket_Read_Data () {QByteArray buffer; / / read buffer data buffer = socket- > readAll ();}

(6) cancel interception

Server- > close ()

2. The following is the routine of the server

The first is the mainwindow.h file:

/ / mainwindow.h#ifndef MAINWINDOW_H#define MAINWINDOW_H# include # include # include namespace Ui {class MainWindow;} class MainWindow: public QMainWindow {Q_OBJECT public: explicit MainWindow (QWidget * parent = 0); ~ MainWindow (); private slots: void on_pushButton_Listen_clicked (); void on_pushButton_Send_clicked (); void server_New_Connect (); void socket_Read_Data (); void socket_Disconnected (); private: Ui::MainWindow * ui QTcpServer* server; QTcpSocket* socket;}; # endif / / MAINWINDOW_H

Then there is the mainwindow.cpp file:

# include "mainwindow.h" # include "ui_mainwindow.h" MainWindow::MainWindow (QWidget * parent): QMainWindow (parent), ui (new Ui::MainWindow) {ui- > setupUi (this); ui- > lineEdit_Port- > setText ("8765"); ui- > pushButton_Send- > setEnabled (false); server = new QTcpServer (); / / Connect signal slot connect (server,&QTcpServer::newConnection,this,&MainWindow::server_New_Connect) } MainWindow::~MainWindow () {server- > close (); server- > deleteLater (); delete ui;} void MainWindow::on_pushButton_Listen_clicked () {if (ui- > pushButton_Listen- > text () = = tr ("listening")) {/ / get the port number int port = ui- > lineEdit_Port- > text () from the input box. ToInt () / / listen on the specified port if (! server- > listen (QHostAddress::Any, port)) {/ / if there is an error, output the error message qDebug () pushButton_Listen- > setText ("cancel listening"); qDebug () state () = = QAbstractSocket::ConnectedState) {/ / close the connection socket- > disconnectFromHost () } * / socket- > abort (); / / cancel listening server- > close (); / / modify keystroke text ui- > pushButton_Listen- > setText ("listening"); / / send button failure ui- > pushButton_Send- > setEnabled (false);}} void MainWindow::on_pushButton_Send_clicked () {qDebug () toPlainText () / / get the contents of the text box and send socket- > write (ui- > textEdit_Send- > toPlainText (). ToLatin1 ()); socket- > flush ();} void MainWindow::server_New_Connect () {/ / get the client connection socket = server- > nextPendingConnection (); / / connect the signal slot of QTcpSocket to read the new data QObject::connect (socket, & QTcpSocket::readyRead, this, & MainWindow::socket_Read_Data) QObject::connect (socket, & QTcpSocket::disconnected, this, & MainWindow::socket_Disconnected); / send button enable ui- > pushButton_Send- > setEnabled (true); qDebug () readAll (); if (! buffer.isEmpty ()) {QString str = ui- > textEdit_Recv- > toPlainText (); str+=tr (buffer); / / Refresh display ui- > textEdit_Recv- > setText (str) }} void MainWindow::socket_Disconnected () {/ / send button failure ui- > pushButton_Send- > setEnabled (false); qDebug ()

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report