我在通过命名管道通信在c#和C ++(QT)应用程序之间传递数据时面临问题。 C#客户端应用程序连接到使用c ++的NamedPipe服务器,但消息未在服务器上正确解析。例如,我将short variable = 2和c ++代码解析字节数组的值发送到unsigned int,但值在服务器中始终为零。能不能让我知道我做错了什么?
客户端和服务器代码:
使用QT库的C ++中的NamedPipe服务器代码
QLocalSocket *local_socket = _local_server->nextPendingConnection();
if (!local_socket->waitForReadyRead(gft::PROC_TIMEOUT)) {
qDebug() << "gft_plugin: timeout while waiting for data";
return;
}
int bytesavailable = local_socket->bytesAvailable();
QByteArray buffer;
QDataStream in(&buffer, QIODevice::ReadOnly);
buffer.append(local_socket->readAll());
unsigned int msg;
in >> msg;
unsigned int a_id = msg;
NamedPipe客户端(C#)
var clientStream = new NamedPipeClientStream("localhost", "gft_plugin_server", PipeDirection.InOut);
clientStream.Connect();
if (clientStream.IsConnected)
{
_stop = false;
UInt16 a_id = 2;
byte[] b = BitConverter.GetBytes(a_id);
clientStream.Write(b, 0, b.Count());
clientStream.Flush();
}
else
{
MessageBox.Show("Could not connected");
}