TCP通信软件开发–Qt

TCP通信软件
软件功能描述实现步骤创建工程添加clientui设计控制程序
界面展示

软件功能描述

(1)TCP server 实现根据 “IP地址” 和 “端口号” 开启服务器,监听连接,收发数据功能。
(2)TCP client 实现根据 “IP地址” 和 “端口号” 连接服务器,收发数据功能。

目前只实现了一对一通信,只能有一个client,可通过动态创建socket的方式实现多个client。

实现步骤
创建工程

开发环境:Qt Creator 5.12.8 qt-opensource-windows-x86-5.12.8
下图中没展示的步骤使用默认设置即可。

TCP通信软件开发--Qt
TCP通信软件开发--Qt
TCP通信软件开发--Qt
TCP通信软件开发--Qt

添加client

TCP通信软件开发--Qt
TCP通信软件开发--Qt
TCP通信软件开发--Qt
TCP通信软件开发--Qt
现在就有了两个ui
TCP通信软件开发--Qt

ui设计

Server 界面:

TCP通信软件开发--Qt

Client界面:
TCP通信软件开发--Qt

控制程序

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();
}


界面展示

TCP通信软件开发--Qt

关注博主即可阅读全文
TCP通信软件开发--Qt

原创:https://www.panoramacn.com
源码网提供WordPress源码,帝国CMS源码discuz源码,微信小程序,小说源码,杰奇源码,thinkphp源码,ecshop模板源码,微擎模板源码,dede源码,织梦源码等。

专业搭建小说网站,小说程序,杰奇系列,微信小说系列,app系列小说

TCP通信软件开发--Qt

免责声明,若由于商用引起版权纠纷,一切责任均由使用者承担。

您必须遵守我们的协议,如果您下载了该资源行为将被视为对《免责声明》全部内容的认可-> 联系客服 投诉资源
www.panoramacn.com资源全部来自互联网收集,仅供用于学习和交流,请勿用于商业用途。如有侵权、不妥之处,请联系站长并出示版权证明以便删除。 敬请谅解! 侵权删帖/违法举报/投稿等事物联系邮箱:2640602276@qq.com
未经允许不得转载:书荒源码源码网每日更新网站源码模板! » TCP通信软件开发–Qt
关注我们小说电影免费看
关注我们,获取更多的全网素材资源,有趣有料!
120000+人已关注
分享到:
赞(0) 打赏

评论抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

您的打赏就是我分享的动力!

支付宝扫一扫打赏

微信扫一扫打赏