简体   繁体   English

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

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

I want to do this我想做这个

For the server:对于服务器:

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

For the client为客户

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

I did not find a method name in QTcpServer to retrieve data and in QTcpClient to send data.我在 QTcpServer 中没有找到方法名来检索数据,在 QTcpClient 中没有找到方法名来发送数据。 What are the methods names to do that ?这样做的方法名称是什么?

You can see all the methods available reading the documentation.您可以通过阅读文档查看所有可用的方法。 In this link there is the documentation for QAbstractSocket Class inherited by QTcpSocket.此链接中有 QTcpSocket 继承的 QAbstractSocket 类的文档。

Anyway the methods you want to use are write() to send data and you can check if data is available to read with bytesAvailable() and read data with read() or readData() .无论如何,您要使用的方法是write()发送数据,您可以检查是否可以使用bytesAvailable()读取数据并使用read()readData()读取数据。 These las methods are inherited and reimplemented from QIODevice Class, that you can also find in the documentation .这些 las 方法是从 QIODevice 类继承和重新实现的,您也可以在文档中找到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM