当前位置: 首页 > wzjs >正文

360免费建站连接外贸网站做开关行业的哪个好

360免费建站连接,外贸网站做开关行业的哪个好,外链图片 wordpress,什么是网页布局一、TCP 通信原理简介 TCP(Transmission Control Protocol)是一种面向连接的可靠通信协议,主要特性如下: [!NOTE] 三次握手建立连接 可靠传输:顺序、无丢包 面向流:数据无结构边界 适用场景&#xff1a…

一、TCP 通信原理简介

TCP(Transmission Control Protocol)是一种面向连接的可靠通信协议,主要特性如下:

[!NOTE]

  • 三次握手建立连接

  • 可靠传输:顺序、无丢包

  • 面向流:数据无结构边界

  • 适用场景:聊天、网页、文件传输


二、Qt 网络模块及常用类

类名功能说明
QTcpServer监听端口,接受客户端连接
QTcpSocket用于客户端和服务端的数据收发
QHostAddress表示 IP 地址(支持 IPv4/IPv6)

使用前需在 .pro 文件中添加:

QT += network

三、TCP 服务端代码

📄 MyTcpServer.h

#ifndef MYTCPSERVER_H
#define MYTCPSERVER_H#include <QTcpServer>   // TCP服务端类
#include <QTcpSocket>   // TCP套接字类// 自定义类继承自 QTcpServer
class MyTcpServer : public QTcpServer
{Q_OBJECT  // Qt 元对象宏,必须有才能使用信号槽public:explicit MyTcpServer(QObject *parent = nullptr);  // 构造函数void startServer(quint16 port);  // 启动服务器private slots:void onNewConnection();       // 有新连接时处理void onClientReadyRead();     // 客户端发来数据时处理void onClientDisconnected();  // 客户端断开时处理private:QTcpSocket *clientSocket;  // 当前连接的客户端 socket(简化版)
};#endif // MYTCPSERVER_H

📄 MyTcpServer.cpp

#include "MyTcpServer.h"
#include <QDebug>  // 用于调试输出// 构造函数:初始化 clientSocket
MyTcpServer::MyTcpServer(QObject *parent): QTcpServer(parent), clientSocket(nullptr) {}// 启动服务器监听指定端口
void MyTcpServer::startServer(quint16 port)
{if (listen(QHostAddress::Any, port)) {// QHostAddress::Any 表示监听所有本机地址qDebug() << "✅ Server started on port" << port;// 当有客户端连接时,发出 newConnection() 信号connect(this, &QTcpServer::newConnection,this, &MyTcpServer::onNewConnection);} else {qDebug() << "❌ Server failed to start!";}
}// 有客户端连接时调用
void MyTcpServer::onNewConnection()
{// 获取客户端 socket(自动生成)clientSocket = nextPendingConnection();qDebug() << "🔌 Client connected from"<< clientSocket->peerAddress().toString();// 接收数据信号绑定槽函数connect(clientSocket, &QTcpSocket::readyRead,this, &MyTcpServer::onClientReadyRead);// 客户端断开信号绑定槽函数connect(clientSocket, &QTcpSocket::disconnected,this, &MyTcpServer::onClientDisconnected);
}// 客户端有数据时读取
void MyTcpServer::onClientReadyRead()
{QByteArray data = clientSocket->readAll();  // 读取所有数据qDebug() << "📨 Received:" << data;clientSocket->write("✅ Server received: " + data);  // 回传消息
}// 客户端断开连接处理
void MyTcpServer::onClientDisconnected()
{qDebug() << "⚠️ Client disconnected.";clientSocket->deleteLater();  // 延迟释放 socket 对象
}

四、TCP 客户端代码(逐行注释)

📄 MyTcpClient.h

#ifndef MYTCPCLIENT_H
#define MYTCPCLIENT_H#include <QObject>
#include <QTcpSocket>  // TCP 客户端通信类class MyTcpClient : public QObject
{Q_OBJECTpublic:explicit MyTcpClient(QObject *parent = nullptr);  // 构造函数void connectToServer(const QString &host, quint16 port);  // 连接服务器void sendMessage(const QString &message);  // 发送消息private slots:void onConnected();      // 连接成功void onReadyRead();      // 收到服务器数据void onDisconnected();   // 连接断开private:QTcpSocket *socket;      // 客户端 socket 对象
};#endif // MYTCPCLIENT_H

📄 MyTcpClient.cpp

#include "MyTcpClient.h"
#include <QDebug>MyTcpClient::MyTcpClient(QObject *parent): QObject(parent)
{socket = new QTcpSocket(this);  // 创建 socket,父对象托管内存// 信号槽绑定connect(socket, &QTcpSocket::connected,this, &MyTcpClient::onConnected);connect(socket, &QTcpSocket::readyRead,this, &MyTcpClient::onReadyRead);connect(socket, &QTcpSocket::disconnected,this, &MyTcpClient::onDisconnected);
}// 发起连接
void MyTcpClient::connectToServer(const QString &host, quint16 port)
{qDebug() << "🌐 Connecting to server:" << host << ":" << port;socket->connectToHost(host, port);  // 异步连接(不会阻塞 UI)
}// 发送消息
void MyTcpClient::sendMessage(const QString &message)
{if (socket->state() == QAbstractSocket::ConnectedState) {socket->write(message.toUtf8());  // QString 转 QByteArrayqDebug() << "➡️ Sent to server:" << message;} else {qDebug() << "⚠️ Not connected.";}
}// 连接成功
void MyTcpClient::onConnected()
{qDebug() << "✅ Connected to server.";
}// 收到服务器消息
void MyTcpClient::onReadyRead()
{QByteArray data = socket->readAll();  // 读取服务器发送内容qDebug() << "📩 Server response:" << QString(data);
}// 断开连接
void MyTcpClient::onDisconnected()
{qDebug() << "❌ Disconnected from server.";
}

五、main.cpp 示例调用(测试)

MyTcpServer *server = new MyTcpServer();
server->startServer(12345);  // 启动服务端MyTcpClient *client = new MyTcpClient();
client->connectToServer("127.0.0.1", 12345);  // 连接服务端
client->sendMessage("Hello from Client!");   // 发送消息

六、总结

  • Qt 中的 TCP 通信机制与基本原理
  • QTcpServer 如何监听 + 接收连接 + 获取 QTcpSocket
  • QTcpSocket 如何连接服务器 + 发送接收数据
  • C++ 构造函数、QObject 父子机制、信号槽绑定细节

文章转载自:

http://K5UoSTrp.wnjsp.cn
http://uJanYCoa.wnjsp.cn
http://OT4J2DRb.wnjsp.cn
http://joIkcmvJ.wnjsp.cn
http://tzB7MTK2.wnjsp.cn
http://FYj3wiWj.wnjsp.cn
http://NXqhPkIN.wnjsp.cn
http://ICfqYp2B.wnjsp.cn
http://n4Y259kh.wnjsp.cn
http://MYBAawl5.wnjsp.cn
http://6Ipo3Ge8.wnjsp.cn
http://cJMs62Ii.wnjsp.cn
http://sx2JApe0.wnjsp.cn
http://7qarJJzt.wnjsp.cn
http://WVofGiW7.wnjsp.cn
http://h42Nr5yH.wnjsp.cn
http://KKxBStKN.wnjsp.cn
http://7DXIHQds.wnjsp.cn
http://dLZMMl04.wnjsp.cn
http://mlrbAnaE.wnjsp.cn
http://LFf0SIFu.wnjsp.cn
http://3ICmSYmW.wnjsp.cn
http://YDWyg1BB.wnjsp.cn
http://ibb2vjU0.wnjsp.cn
http://6A1it5Va.wnjsp.cn
http://kniPwYzK.wnjsp.cn
http://A5lr69Mh.wnjsp.cn
http://MxcCgyRz.wnjsp.cn
http://BQMbiiEf.wnjsp.cn
http://3vQHraLn.wnjsp.cn
http://www.dtcms.com/wzjs/669310.html

相关文章:

  • 建设了网站后怎么用谷歌引流皮具制品 东莞网站建设
  • 方便做流程图的网站大连网站制作建设
  • 廊坊网站制作建设网站开发电脑配置要求
  • 高端网站开发程电子商务是干什么工作的
  • 何做好网站建设销售重庆网站空间主机评价
  • 企业网站员工园地建设Wordpress 建立学生档案
  • 销售网站开发的背景管理软件是什么
  • 网站上截小屏幕 怎么做网站制作台州
  • 网站建设维护是什么岗位福建省建设注册管理中心网站
  • 温州大凯工艺品有限公司英文网站官方网站下载微博
  • 自己做聊天背景网站成都网站线上公司
  • 国外设计网站怎么打开wordpress后台超慢
  • 企业网站建设与推广多少钱做摘抄的网站
  • 徐州模板开发建站百度seo流量
  • 网站顶部广告代码怎么做p2p的网站
  • 2017年做网站好难织梦网站内部优化
  • 网站留言板html代码百度推广费用可以退吗
  • 在那个上面做网站都能搜到小程序快速开发
  • 石家庄手机网站湖北智能网站建设找哪家
  • 如何让网站被百度收录怎样在工商局网站做申请登记
  • 菏泽市住房和城乡建设局网站做网站备案成功后怎么办
  • meta 手机网站越秀高端网站建设
  • 中国十大旅游网站宽带哪家好
  • 北京正规网站建设比较专业的广州手机网站
  • 建设银行网站会员怎么注册linux下用python做网站
  • 做兼职拍照片传网站wordpress本地图片
  • 网站网页制作公司100个常用的关键词
  • 中文儿童网站模板wordpress 网站收录
  • 门户网站系统程序常用网店系统
  • 摄影网站 蜂鸟加盟平台网站怎么做