• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Use Named Pipe for IPC between Java and C++

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you see:

java.nio.channels
Class Pipe

?
 
I promise I will be the best, most loyal friend ever! All for this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic