我找不到类似的问题,所以这里是这样的:
我跨两个应用程序从QLocalSocket向QLocalServer发送QString。接收(QLocalServer)应用程序确实接收到了消息,但是编码似乎完全错误。
如果我从QLocalSocket (客户端)发送一个QString = "x“,我得到一个外国的(中文?)QLocalServer中的符号。我的代码实际上是从Nokia Developer website复制过来的
如果我通过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();
}任何帮助都是非常感谢的。
发布于 2012-10-25 12:35:19
客户端正在序列化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;https://stackoverflow.com/questions/13061079
复制相似问题