首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >数据不会在QTcpSockets和Java Sockets之间传输

数据不会在QTcpSockets和Java Sockets之间传输
EN

Stack Overflow用户
提问于 2019-03-24 01:22:44
回答 2查看 55关注 0票数 0

所以我有两个应用程序,一个是使用Qt5用C++编写的,另一个是用Java语言编写的。C++应用程序充当连接到java应用程序中的ServerSocket对象的客户端,然后java应用程序从该连接实例化一个套接字。然后,当我尝试向服务器发送字符串时,要么必须重复发送字符串(比如20000次),要么服务器什么也收不到。

代码语言:javascript
复制
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;
}

在上面的代码不会产生任何结果的情况下,下面的代码会产生分段的字符串以及服务器的重复接收字符串:

代码语言:javascript
复制
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程序,如下所示:

代码语言:javascript
复制
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;然而,结果要么是什么都没有,要么是以下内容:

代码语言:javascript
复制
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

你知道我该如何解决这个问题吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-24 06:36:32

因此,解决方案是将数据读取到byte[]中,然后使用字符串构造函数将其转换为字符串,如下所示:

代码语言:javascript
复制
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);

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

代码语言:javascript
复制
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);
票数 0
EN

Stack Overflow用户

发布于 2019-03-24 06:37:59

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

(2)循环

代码语言:javascript
复制
while(!sock->waitForBytesWritten(3000) | 1);

在我看来,这是一个无穷无尽的循环,独立于waitForBytesWritten的返回代码

(3)在您的程序环境中,waitForBytesWritten的概念是什么?它可能会在写入流的第一个字节之后返回!

(4)你的想法是什么?

代码语言:javascript
复制
if (tmp.contains(" ")){
              break;

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55316424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档