TCP通信軟體
- 軟體功能描述
- 實作步驟
- 創建工程
- 添加client
- ui設計
- 控制程式
- 界面展示
軟體功能描述
(1)TCP server 實作根據 “IP地址” 和 “埠號” 開啟服務器,監聽連接,收發資料功能,
(2)TCP client 實作根據 “IP地址” 和 “埠號” 連接服務器,收發資料功能,
目前只實作了一對一通信,只能有一個client,可通過動態創建socket的方式實作多個client,
實作步驟
創建工程
開發環境:Qt Creator 5.12.8 qt-opensource-windows-x86-5.12.8
下圖中沒展示的步驟使用默認設定即可,




添加client




現在就有了兩個ui

ui設計
Server 界面:

Client界面:

控制程式
TCP.pro
QT += core gui network
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
client.cpp \
main.cpp \
server.cpp
HEADERS += \
client.h \
server.h
FORMS += \
client.ui \
server.ui
CONFIG += c++11
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
namespace Ui {
class Client;
}
class Client : public QWidget
{
Q_OBJECT
public:
explicit Client(QWidget *parent = nullptr);
~Client();
private:
Ui::Client *ui;
//QTcpServer* server;//監聽的套接字
QTcpSocket* client;//通信的套接字
};
#endif // CLIENT_H
server.h
#ifndef SERVER_H
#define SERVER_H
#include <QWidget>
#include <QTcpServer>
#include <QTcpSocket>
QT_BEGIN_NAMESPACE
namespace Ui { class Server; }
QT_END_NAMESPACE
class Server : public QWidget
{
Q_OBJECT
public:
Server(QWidget *parent = nullptr);
~Server();
private:
Ui::Server *ui;
QTcpServer* server;//監聽的套接字
QTcpSocket* conn;//通信的套接字
};
#endif // SERVER_H
client.cpp
#include "client.h"
#include "ui_client.h"
Client::Client(QWidget *parent) :
QWidget(parent),
ui(new Ui::Client)
{
ui->setupUi(this);
//ui init
ui->IP->setText("192.168.50.116");
ui->PORT->setText("12321");
ui->state->setText("狀態:未連接");
connect(ui->Connect, &QPushButton::clicked, this, [=]()
{
// init
client = new QTcpSocket(this);
//連接服務器
client->connectToHost(QHostAddress(ui->IP->text()),ui->PORT->text().toInt());
ui->state->setText("狀態:已連接");
ui->record->append("已連接到服務器---IP:" + ui->IP->text() + " 埠:" + ui->PORT->text());
//接收資料
connect(client, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = client->readAll();
ui->record->append("Server: " + array);
});
//發送
connect(ui->send, &QPushButton::clicked, this, [=]()
{
//發送資料
client->write(ui->msg->toPlainText().toUtf8());
ui->record->append("Client: " + ui->msg->toPlainText());
//clear
//ui->textEdit_2->clear();
});
});
}
Client::~Client()
{
delete ui;
}
server.cpp
#include "server.h"
#include "ui_server.h"
Server::Server(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Server)
{
ui->setupUi(this);
//ui init
ui->IP->setText("192.168.50.116");
ui->PORT->setText("12321");
ui->state->setText("狀態:未開啟");
connect(ui->OpenServer, &QPushButton::clicked, this, [=]()
{
//實體化
server = new QTcpServer(this);
//監聽
server->listen(QHostAddress(ui->IP->text()),ui->PORT->text().toInt());
ui->state->setText("狀態:已開啟");
//新的連接
connect(server, &QTcpServer::newConnection, this, [=]()
{
//接收客戶端的套接字物件accept
// sock_addr 結構體 == 類 QTcpSocket
conn = server->nextPendingConnection();
QString str;
ui->record->append("客戶端接入---IP: " + conn->peerAddress().toString() +" 埠:"+ str.sprintf("%d", (conn->peerPort())));
//接收資料
connect(conn, &QTcpSocket::readyRead, this, [=]()
{
QByteArray array = conn->readAll();
ui->record->append("Client: " + array);
});
});
//發送
connect(ui->send, &QPushButton::clicked, this, [=]()
{
//發送資料
conn->write(ui->msg->toPlainText().toUtf8());
//char data[1024];data[0]=0xeb;data[1]=0x90;
//conn->write(data,1024);
ui->record->append("Server: " + ui->msg->toPlainText());
//clear
//ui->textEdit_2->clear();
});
});
}
Server::~Server()
{
delete ui;
}
main.cpp
#include "server.h"
#include "client.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Server w;
w.setWindowTitle("TCP Server");
w.show();
Client c;
c.setWindowTitle("TCP Client");
c.show();
return a.exec();
}
界面展示

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/165233.html
標籤:python
