如何在 QT 中通过C++套接字发送和接收

How to send and receive over sockets in C++ in QT?

本文关键字:套接字 C++ QT      更新时间:2023-10-16

我想这样做

对于服务器:

QTcpServer qtp;
qtp.listen(QHostAddress::Any, 1440);
qtp.readFromClient(); //What is the name of the method to read the byte from a  client ???
qtp.close();

对于客户

QTcpClient client;
client.connect("127.0.0.1", 1440);
client.sendData(myString); // What is the name of the method to do this ???  
client.close();

我在 QTcpServer 中找不到用于检索数据和在 QTcpClient 中用于发送数据的方法名称。执行此操作的方法名称是什么?

您可以在阅读文档时看到所有可用的方法。在此链接中,有QTcpSocket继承的QAbstractSocket类的文档。

无论如何,您要使用的方法是write()发送数据,您可以检查数据是否可用于bytesAvailable()读取数据以及使用read()readData()读取数据。这些 las 方法是从 QIODevice 类继承和重新实现的,您也可以在文档中找到这些方法。