2669 views|1 replies

164

Posts

0

Resources
The OP
 

4412 development board Qt network programming-TCP implementation server and client [Copy link]

Network programming includes TCP and UDP. TCP programming requires two classes: QTcpServer and QTcpSocket.

.

1 TCP implements server and client
TCP protocol (Transmission Control Protocol) is a connection-oriented, reliable, byte stream-based transport layer communication protocol that transmits data stably and reliably.
Search in the help index to find two important classes as shown in the figure:

Both classes are used in server programming, while only the QTcpSocket object is used in client programming.
The basic usage of the QTcpServer class in this experiment:
(1) Listen for client connections.
(2) Whenever a new client connects to the server, a signal is automatically triggered.
(3) Create a Socket object based on the new client currently connected, and hand over the data sending and receiving actions to the socket.
(4) Close the server close();
Basic usage of the QTcpSocket class:
(1) Server: Get the connection status and bind the socket when there is a new connection.
(2) Client: Connect to the server through the socket, and a signal is triggered when the connection is successful.

(3) When data arrives, a signal will be triggered and read using readAll().
(4) Send and receive data by reading and writing sockets.
Specific steps:
Step 1: Create a project and add a network to the project file .pro, as shown in the figure:

Step 2: Design the UI interface,
1. Set the main window size in the property editing bar:

2. Add components
Receive window: Plain Text EditSend
window, IP address window, port number window: Line EditOpen
server, close server: Push ButtonAfter
dragging, lay out each one and set the component size as needed. Here, the port number box is set to the minimum of 200

Button layout: Drag the buttons and springs, then click Horizontal layout.

Then select all components and click Grid Layout:

Finally, change the component name comment, and the result will be as shown below:

Step 3: Server-side programming:
1. Create a QTcpServer object
2. Create a listening port so that the client can use this port to access the server, using the listen function.
bool listen(const QHostAddress &address = QHostAddress::Any, quint16 port = 0);
The first parameter is the binding address (that is, the local server IP), and the second parameter is the bound port number of this server.
When listening to a port, if a new connection comes in, a newConnection() signal is issued.
3. When the server object is accessed, a newConnection() signal is issued, so a slot function is added to the signal and a QTcpSocket
object is used to accept client access.
4. When new data arrives in the socket receiving buffer, a readyRead() signal is issued, a slot function is added to the signal, and readyRead() is used
to read.
5. The socket can directly call the write() member function to send data.
6. Close the port number.
The code is as follows:

#include <QMainWindow>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class TcpServer;
}
class TcpServer : public QMainWindow
{
Q_OBJECT
public:

explicit TcpServer(QWidget *parent = 0); ~TcpServer();
QTcpServer * tcpServer;
QTcpSocket * tcpSocket;
public slots:
void newConnection_Slot(void);
void readyRead_Solt(void);
private slots:
void on_openBu_clicked();
void on_sendBu_clicked();
void on_closeBu_clicked();
private:
Ui::TcpServer *ui;
};
#include "tcpserver.h" #include "ui_tcpserver.h" #include <QTcpServer>
#include <QTcpSocket>
#include <QString>
TcpServer::TcpServer(QWidget *parent) :
QMainWindow(parent), ui(new Ui::TcpServer)
{
ui->setupUi(this);
tcpServer = new QTcpServer(this);
tcpSocket = new QTcpSocket(this);
//连接信号与槽函数进行绑定
connect(tcpServer,SIGNAL(newConnection()),SLOT(newConnection_Slot()));
}
//连接信号槽函数

void TcpServer::newConnection_Slot(void)
{
//After connecting to the client socket
tcpSocket = tcpServer->nextPendingConnection();
//The socket's receive data signal is connected to the data slot function
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead_Solt()));
}
//Read data
void TcpServer::readyRead_Solt(void)
{
QString buf;
//Read
buf = tcpSocket->readAll();
ui->recvEdit->appendPlainText(buf);
}
TcpServer::~TcpServer()
{
delete ui;
}
//Open
void TcpServer::on_openBu_clicked()
{
//Listen
tcpServer->listen(QHostAddress::Any,ui->portEdit->text().toUInt());
}
//Send data
void TcpServer::on_sendBu_clicked()
{
tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data());
}
//Close
void TcpServer::on_closeBu_clicked()

{
tcpSocket->close();
}
Step 4: Client programming

1. Create a QTcpSocket socket object
2. Use the member function of the socket object to request to connect to the server.
void connectToHost(const QHostAddress &address, quint16 port, openMode mode = ReadWrite);
The first parameter is the server IP address, and the second parameter is the server port number. The third parameter is the opening mode, which is readable and writable by default.
Function: After the request to connect to the server is successfully connected, the connected() signal is issued, and the slot function connected_Solt() is bound to operate the socket.
3. Use the write function to send data to the server. When new data arrives in the socket receiving buffer,
the readyRead() signal will be issued. Add a slot function to read the data for this signal.
4. Disconnect from the server.

class TcpClient : public QMainWindow
{
....... private slots:
void on_openBt_clicked();
void connected_Solt(void);
void readyRead_Solt(void);
void on_sendEdit_2_clicked();
void on_closeBt_clicked();
};

TcpClient::TcpClient(QWidget *parent) :
QMainWindow(parent), ui(new Ui::TcpClient)
{
ui->setupUi(this);
//Create socket object
tcpSocket = new QTcpSocket(this);
}
TcpClient::~TcpClient()
{
delete ui;
}
//Open (connect to server)
void TcpClient::on_openBt_clicked()
{
tcpSocket->connectToHost(ui->ipEdit->text(),ui->portEdit->text().toUInt());
connect(tcpSocket,SIGNAL(connected()),this,SLOT(connected_Solt()));
}
//Wait for data to arrive
void TcpClient::connected_Solt(void)
{
connect(tcpSocket,SIGNAL(readyRead()),this,SLOT(readyRead_Solt()));
}
//Read data
void TcpClient::readyRead_Solt(void)
{
ui->recvEdit->appendPlainText(tcpSocket->readAll());
}
//Send
void TcpClient::on_sendEdit_2_clicked()
{
tcpSocket->write(ui->sendEdit->text().toLocal8Bit().data());
}
//Close
void TcpClient::on_closeBt_clicked()

{
tcpSocket->close();
}
The compilation runs successfully, and the communication between the server and the client is as shown in the figure:

This post is from ARM Technology

Latest reply

A PC is enough for school, this kind of thing doesn't need a board.   Details Published on 2021-5-13 22:46
 

7422

Posts

2

Resources
2
 

A PC is enough for school, this kind of thing doesn't need a board.

This post is from ARM Technology
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list