3

I am facing issue in passing data between c# and C++ (QT) application through named pipe communication. C# client application connects to NamedPipe server which is in c++ but message is not being parsed correctly on server. Example, I am sending value of short variable = 2 and c++ code parse byte array into unsigned int but value is coming always zero in server. Could you please let me know what I am doing wrong?

Client and server code:

NamedPipe Server code in C++ using QT Library

  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 Client (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");
        }
Neeraj Kaushik
  • 354
  • 1
  • 5
  • 20
  • 3
    First make sure the bytes sent/arriving are intact before parsing. Then, using "unsigned int" for serialization is a bad idea, because its size is platform-dependent, and probably 32bit, not 16bit, on your computer. Use quint16 instead. – Frank Osterfeld Mar 09 '14 at 09:23
  • Hi Frank, Thanks for giving me hints. It is working now. – Neeraj Kaushik Mar 09 '14 at 09:41

0 Answers0