• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use Named Pipe for IPC between Java and C++  RSS feed

 
Nan Ma
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my problem:

we have a legacy C++ program that serves as a backend "web server". It used to communicate with front end IIS server through Named Pipe, i.e. IIS server serializes and sends the client HTTP request into the Name Pipe. The C++ "web server" reads the request from the pipe and generates the response and writes the response bytes into the pipe, then IIS reads the response bytes from the pipe and send it to client browser. The code running in IIS (it's an IIS ISAPI extension) is also written in C++.

Now we want to use Tomcat web server as the front end server. In order to talk to the Named Pipe, we tried using FileInputStream and FileOutputStream around the Named Pipe. Here is the code:

String req_str = getRequestString(request, mapping);
File f = new File("\\\\.\\my_pipe");
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(f);
fos.write(req_str.getBytes());
fos.close();
}
catch(Exception e)
{
e.printStackTrace();
return null;
}

// get the input stream from the pipe
FileInputStream fis = null;
do
{
try
{
fis = new FileInputStream(f);
}
catch (Exception e)
{
try
{
Thread.sleep(1000);
}
catch(Exception e1){}
fis = null;
}
}
while(fis == null);

// read the response bytes
StringBuffer buf = new StringBuffer();
int read_byte = 0;
int cnt = 0;
String header_end = "\r\n\r\n";
try
{
while ((read_byte = fis.read()) != -1)
{
cnt++;
if (cnt >= 4 && buf.indexOf(header_end) >= 0)
break;
buf.append((char) read_byte);
}
}
catch(Exception e)
{
e.printStackTrace();
return null; // to fix
}

// send response bytes to client
...

But I ran into a problem here: The java program creates a FileOutputStream to write the request string into the pipe first, then creates a FileInputStream to read the response back. But this cannot be finished successfully within one session. During the reading, when the FileInputStream is created, it connects to a new "Instance" of the named pipe, which corresponds to a new File handle in the OS, instead of the file handle for the write operation. Therefore the input stream cannot read the correct content returned from the previous write operation. I tried forcing both streams to use the same underlying file handle, but I got Access Denied error since a file cannot be both readable and writable in Java.

Can someone give me some pointers for this problem? I really appreciate it.
 
Stefan Wagner
Ranch Hand
Posts: 1923
Linux Postgres Database Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you see:
java.nio.channels
Class Pipe
?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!