1

This is a question about inter process communication via stdin/stdout.

The problem is I have a COM library, which I wasn't able to use with any Java-COM bridge (one particular function always causes core dump). But I was able to use it from a C++ program.

So I decided to make a wrapper server program in C++ to make those calls for me, and communicate with it from Java via stdin/stdout, but I'm facing a problem here. I've decided to use protobufs for communicating messages, the main problem is reading input on the C++ side. I need a method, that will block until a certain amount of bytes is written to stdin for it to read.

The idea was to use google's protobufs, and set up communication like this:

  • C program starts an infinite loop, blocking on STDIN input, waiting to get 4 bytes in, which would be the length of the incoming message.
  • Then it blocks to get the whole message (raw byte count is known)
  • Parse the message with protobuf
  • Do work
  • Write output to stdout (probably in the same manner, prepending the message with the number of bytes incoming)
  • Java clinet reads this using DataStream or something like this and deciphers using protobufs as well

Setting up this two way communication turned out to be quite a lot harder, than I would have thought, thanks to my lack of knowledge of C++ and Windows programming (I compile it using MSVS2013 Community, and there are so many windows specific marcos/typedefs from all this COM code).

Is there some 3rd party lib, that can make creation of such a simple server, well, actually, simple?

PS: can be C, can be C++, I just need it to run on Windows.

Dmitry Avtonomov
  • 8,747
  • 4
  • 32
  • 45
  • If you're happy with C why not just `read(stdin, buffer, bytes)`? – Michael Anderson Jan 19 '15 at 07:24
  • @MichaelAnderson will it block until there is a sufficient amount of bytes available to read in? My biggest problem is to get this blocking, but I'm overwhelmed with the amount of functions that are available for that. I thought that read() was a UNIX system call, but I need it to run on Windows (it's communicating with COM after all) – Dmitry Avtonomov Jan 19 '15 at 07:37
  • The unix version of read does block by default. And according to http://stackoverflow.com/questions/5684679/write-function-requires-unistd-h-on-unix-what-about-windows its available in windows in `` – Michael Anderson Jan 19 '15 at 07:39
  • @MichaelAnderson ok, I'll try io.h version. That's a big part of the problem for me - finding out what works on windows and what's not. – Dmitry Avtonomov Jan 19 '15 at 07:41

1 Answers1

1

A relatively simple message handling loop might look like this. However you should really check the return value of both of the reads and handle errors there.

void read_and_process_message(void) {
  while(true) {
    long nMessageBytes;
    read(stdin, &nMessageBytes, sizeof(long));
    //Convert from network byte-order to local byte order
    nMessageBytes = ntohl(nMessageBytes);
    char * buffer = malloc(nMessageBytes);
    read(stdin, buffer, nMessageBytes);
    // Do something with your buffer and write to stdout.
  }
}
Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
  • I will try that tomorrow (it's already 2:38 in the morning here), I so much hope it works..Thank you for the sample code anyway, I would never have thought of translating from network byte order to local. – Dmitry Avtonomov Jan 19 '15 at 07:39