【问题标题】:Data isn't transmitted between QTcpSockets and Java Sockets数据不在 QTcpSockets 和 Java Sockets 之间传输
【发布时间】: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 的人他们的东西是如何工作的。虽然不能保证“刷新”,但协议栈应该尽最大努力将数据在线发送出去,而且实际上应该总是成功的。

标签: java c++ qt sockets tcp


【解决方案1】:

因此解决方案是将数据读入byte[],然后使用字符串构造函数转换为这样的字符串:

ServerSocket ss = new ServerSocket(5000);

Socket s = ss.accept();

DataInputStream in = new DataInputStream(s.getInputStream());

byte[] b = new byte[1024];
in.read(b);

String tmp = new String(b, "UTF8");
System.out.println(b);
System.out.println(tmp);

我不知道为什么,但这是我得出的解决方案。尽管进行了细微的更改,但客户端几乎保持不变:

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


do {
        sock->write(s.toUtf8());
        sock->flush();

        sock->waitForBytesWritten();
} while (sock->isValid() && sock->bytesToWrite() > 0);

【讨论】:

  • 查看我的注释 (5) 以了解您的解决方案为何有效。
【解决方案2】:

(1) waitForBytesWritten 在 Windows 上可能会失败 - 最好使用信号 bytesWritten()

(2) 循环

while(!sock->waitForBytesWritten(3000) | 1);

在我看来就像一个无限循环,与 waitForBytesWritten 的返回码无关

(3) 在你的程序上下文中waitForBytesWritten 的概念是什么?它可能会在写入流的第一个字节后返回!

(4)

的思想是什么
if (tmp.contains(" ")){
              break;

对我来说,这看起来可能会在收到任何空白后中断 - 例如“This”之后的第一个空格。它可能会在几行之后中断,具体取决于 readUTF 提供了多少(我不是 Java 专家)。

(5) readUTF 需要修改后的 UTF8 格式的字符串,但 Qt 不会发送修改后的 UTF8 格式的字符串(前导 16 位整数包含长度)。因此,Java 会将前两个字符('Th')解释为长度,然后彩票可能会开始 - 根据这两个字符,解码将返回看似任意的结果。

【讨论】:

  • 所以你的意思是,如果我将字符串的长度添加到一个字节数组中,其中包含你认为可以解决的长度?
  • 根据您的字符串,readUTF 可能会产生意想不到的结果,例如Qt中nul和补充字符的编码是不同的。
  • 但是不是UTF标准的字符集所以使用QString方法toUtf8()应该避免吗?
  • UTF 是标准化的,但 readUTF 不会读取标准化的 UTF8 而是“修改后的 UTF8”。 google这个词或者在这里阅读Java的接口说明!
猜你喜欢
  • 2014-10-07
  • 1970-01-01
  • 2011-12-12
  • 1970-01-01
  • 2011-05-03
  • 1970-01-01
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
相关资源
最近更新 更多