qlocalsocket到qlocalserver消息在转移期间被损坏
QLocalSocket to QLocalServer message being corrupted during transfer
我找不到类似的问题,所以这里是:
我正在通过两个应用程序将QString从Qlocalsocket发送到Qlocalserver。接收(qlocalserver)应用程序确实会收到消息,但似乎编码是完全错误的。
如果我从qlocalsocket(客户端)发送QString =" X",我将在Qlocalserver中获得外国(中文?)符号。我的代码实际上是从诺基亚开发人员网站
复制的如果我通过qdebug打印消息,我会得到"?"。如果我在消息框中发射它,则打印中文字符。我尝试将收到的消息重新编码给UTF-8,Latin1等,没有运气。
代码如下:
//Client
int main(int argc, char *argv[])
{
QLocalSocket * m_socket = new QLocalSocket();
m_socket->connectToServer("SomeServer");
if(m_socket->waitForConnected(1000))
{
//send a message to the server
QByteArray block;
QDataStream out(&block, QIODevice::WriteOnly);
out.setVersion(QDataStream::Qt_4_7);
out << "x";
out.device()->seek(0);
m_socket->write(block);
m_socket->flush();
QMessageBox box;
box.setText("mesage has been sent");
box.exec();
...
}
//Server - this is within a QMainWindow
void MainWindow::messageReceived()
{
QLocalSocket *clientConnection = m_pServer->nextPendingConnection();
while (clientConnection->bytesAvailable() < (int)sizeof(quint32))
clientConnection->waitForReadyRead();
connect(clientConnection, SIGNAL(disconnected()),
clientConnection, SLOT(deleteLater()));
QDataStream in(clientConnection);
in.setVersion(QDataStream::Qt_4_7);
if (clientConnection->bytesAvailable() < (int)sizeof(quint16)) {
return;
}
QString message;
in >> message;
QMessageBox box;
box.setText(QString(message));
box.exec();
}
任何帮助都将受到高度赞赏。
客户端正在序列化const char*
,而服务器则对QString
进行序列化。这些不兼容。前者从字面上写了字符字节,后者首先编码为UTF-16。因此,我猜在服务器端,原始的字符串数据" FFF"被解码为QString,好像是UTF-16数据一样...也许导致字符u 6666,晦。
尝试更改客户端以序列化QString,即
// client writes a QString
out << QString::fromLatin1("fff");
// server reads a QString
QString message;
in >> message;
相关文章:
- 为什么会发生堆损坏
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 平均图像时图像损坏
- 如何针对特定情况调试和修复此双自由内存损坏问题
- 我是否需要在下一次转移时将所有权*转移回转移队列
- 为什么C中的通用链表中存储的数据已损坏
- gdb错误:Backtrace已停止:上一帧与此帧相同(堆栈已损坏?)
- C++双重释放或损坏(out)
- 捕获标准输出以压缩并使用 CTRL-C 中断会给出损坏的 zip 文件
- 使用全局声明的向量时,C++双重释放错误/损坏
- 如何将元素从向量转移到新数组?
- 为什么系统函数总是在C++中返回已转移的退出状态?
- 变量周围的堆栈'...'已损坏
- 运行时检查失败 #2 变量"A"周围的堆栈已损坏
- 检测到堆损坏:在正常块 c++ 动态 2D 数组之后
- 删除字符串后C++检测到堆损坏
- 两个垫子的 OpenCV 数据是相同的,但使用 Mat::at 检索时的值已损坏
- 我可以写入关闭的套接字并强制纠正损坏的管道错误吗?
- 损坏的结构字符数组 - sqlite C++
- qlocalsocket到qlocalserver消息在转移期间被损坏