简体   繁体   English

为什么我不能使用QTcpSocket的write()方法发送数据?

[英]Why can't I send data with QTcpSocket's write() method?

I'm trying to learn Qt with the documentation alone, and it's kind of tough. 我试图仅通过文档来学习Qt,这很难。 I managed to get a simple command-line chat client working, but I don't know how to send things. 我设法使一个简单的命令行聊天客户端正常工作,但是我不知道该如何发送消息。 It takes about 4 lines of intuitive code to read from a socket in Qt, but for some reason I can't say 从Qt中的套接字读取大约需要4行直观代码,但是由于某些原因我不能说

QTcpSocket socket;
socket.connectToHost("google.com", 80);
socket.write("hey");
socket.flush();

flush(); flush(); returns false, but I don't see why. 返回false,但我不明白为什么。

I really don't understand why this doesn't work and would appreciate any help. 我真的不明白为什么这行不通,并且不胜感激。 Thanks! 谢谢!

调用connectToHost()您需要具有用于connected()的插槽处理程序,或调用waitForConnected()以等待与主机的连接完成,否则您的代码将在尝试建立连接之前通过尝试编写进行放大。

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

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