【发布时间】:2019-08-14 10:22:50
【问题描述】:
所以我有两个应用程序,一个是使用 Qt5 用 C++ 编写的,另一个是用 Java 编写的。 C++ 应用程序充当客户端,连接到 java 应用程序中的 ServerSocket 对象,然后从连接中实例化一个套接字。然后,当我尝试向服务器发送字符串时,我要么必须重复发送字符串(例如 20000 次),要么服务器根本没有收到任何内容。
QString s = " This is a test string that will be sent to the server upon connection\n";
QTcpSocket *sock = new QTcpSocket;
sock->connectToHost(QHostAddress::LocalHost, 5000, QIODevice::ReadWrite);
sock->waitForConnected(5000);
QTextStream out(sock);
while(!sock->waitForBytesWritten(3000)) {
out << s << endl;
}
如果上面的代码不会产生任何结果,下面会产生分段字符串以及服务器接收到的重复字符串:
QString s = " This is a test string that will be sent to the server upon connection\n";
QTcpSocket *sock = new QTcpSocket;
sock->connectToHost(QHostAddress::LocalHost, 5000, QIODevice::ReadWrite);
sock->waitForConnected(5000);
QTextStream out(sock);
do {
out << s << endl;
} while(!sock->waitForBytesWritten(3000) | 1);
服务器是这样一个简单的java程序:
ServerSocket ss = new ServerSocket(5000);
Socket s = ss.accept();
DataInputStream in = new DataInputStream(s.getInputStream());
while (true){
String tmp = in.readUTF();
System.out.println(tmp);
if (tmp.contains(" ")){
break;
}
我希望输出是一个简单的This is a test string that will be sent to the server upon connection;然而,结果要么什么都没有,要么就是这样:
his is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
This is a test string that will be sent to the server upon connection
您知道如何解决这个问题吗?
【问题讨论】:
-
我不知道“Q”,但如果我用 Java 编写客户端,我必须在调用
out后发送一个“flush”,让它真正发送数据包. -
冲洗没有区别
-
刷新会将消息推送出流的缓冲区并进入网络堆栈,但 TCP 不保证网络堆栈不会缓冲它或将其拆分为多个 TCP 数据包。如果要在 TCP 流中发送离散消息,则需要将消息包装在协议中。
-
你知道我将如何在 Qt 中做到这一点吗?
-
如果刷新没有任何区别,那么我认为您的协议堆栈已损坏。最好去问问 QT 的人他们的东西是如何工作的。虽然不能保证“刷新”,但协议栈应该尽最大努力将数据在线发送出去,而且实际上应该总是成功的。