[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>Qt中使用TCP服务端及客户端
TCP(Transmission Control
Protocol)是一种可靠、高度优化的互联网通信协议,广泛用于各类网络应用程序中。在这篇博客中,我们将介绍如何在Qt中使用TCP服务端及客户端。
<>使用Qt创建TCP服务器
要使用Qt创建TCP服务器,需要完成以下步骤:
<>步骤1:创建QTcpServer对象
QTcpServer *server = new QTcpServer(this);
<>步骤2:监听连接请求
在开始监听之前,您需要使用listen()方法指定服务端口号并监听来自特定IP地址的连接请求。
if (!server->listen(QHostAddress::Any, 12345)) { qDebug() << "Failed to start
server"; return; }
<>步骤3:处理新连接请求
每当一个新连接请求到达服务器时,QTcpServer将通过发射newConnection()信号来提示您。您可以使用accept()方法来接受该连接,并创建一个新的QTcpSocket对象以进行收发消息。
void MyTcpServer::newConnection() { QTcpSocket *socket = server->
nextPendingConnection(); connect(socket, SIGNAL(readyRead()), this, SLOT(
readData())); connect(socket, SIGNAL(disconnected()), socket, SLOT(deleteLater()
)); } void MyTcpServer::readData() { QTcpSocket *socket = static_cast<QTcpSocket
*>(sender()); QByteArray data = socket->readAll(); // 处理接收到的数据 }
<>步骤4:发送数据
使用QTcpSocket的write()方法将您的数据发送到连接的客户端。
socket->write("Hello world");
<>使用Qt创建TCP客户端
要使用Qt创建TCP客户端,需要完成以下步骤:
<>步骤1:创建QTcpSocket对象
QTcpSocket *m_socket = new QTcpSocket(this);
<>步骤2:连接到服务器
使用connectToHost()方法将socket连接到目标主机和端口。连接成功后,QTcpSocket将发送connected()信号。
m_socket->connectToHost(QHostAddress("127.0.0.1"), 12345);
<>步骤3:发送和接收数据
使用write()方法向服务器发送数据,并通过readyRead()信号处理接收到的数据。
void MyTcpClient::sendData(QString data) { m_socket->write(data.toUtf8().
constData()); } void MyTcpClient::readData() { QString data = m_socket->readAll(
); // 处理接收到的数据 }
<>总结
在Qt中使用TCP服务端及客户端是一种可靠、高效的方法来进行网络通信。QTcpServer和QTcpSocket类提供了一个面向对象的解决方案,使得您能够轻松地开发出各种类型的网络应用程序。
然而,请注意因多种原因(例如网络中断、远程主机关闭连接等)导致的时延和异常情况,并确保在代码中处理这些情况以确保应用程序的稳定性。