所以我有两个应用程序,一个是用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
您对我如何解决此问题有任何想法吗?
答案 0 :(得分:0)
因此解决方案是将数据读取到SELECT UnitId, AVG(Rating)
FROM MyTable
GROUP BY UnitId
WHERE AVG(Rating) < 4 -- Bad: WHERE is for the raw values, before they're aggregated
中,然后使用字符串构造函数将其转换为这样的字符串:
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);
答案 1 :(得分:0)
(1)waitForBytesWritten
在Windows上可能会失败-最好使用信号bytesWritten()
(2)循环
while(!sock->waitForBytesWritten(3000) | 1);
对我来说似乎像一个无休止的循环,与waitForBytesWritten的返回代码无关
(3)在您的程序上下文中waitForBytesWritten
的想法是什么?它可能在写入流的第一个字节之后返回!
(4)的想法是
if (tmp.contains(" ")){
break;
对我来说,这似乎在收到任何空白后可能会中断-例如“此”之后的第一个空白。根据readUTF的交付量,它也可能会中断几行(我不是Java专家)。
(5)readUTF
期望使用修改后的UTF8格式的字符串,但是Qt不会发送修改后的UTF8格式的字符串(带有长度的前导16位整数)。因此,Java会将前两个字符('Th')解释为长度,然后彩票便会开始-根据这两个字符,解码将返回看似随意的结果。