I'm wanting to set up a pipe between a Fortran program and a C# program. The Fortran program will be doing the heavy lifting and the C# program will provide a 'view' onto what is being done (using the data sent to it from the Fortran program). To check that this will work, I've written two small programs. (The Fortran code was poached from examples of piping data between C++ and C#.)
Fortran code:
PROGRAM Test
USE kernel32
IMPLICIT NONE
! Data pipe
INTEGER*4 EqnData /100/
! Paths for pipes
CHARACTER*128 dataname /'\\.\pipe\EqnData'/
INTEGER(HANDLE) :: pipe1
pipe1 = CreateNamedPipe('\\\\.\\pipe\\EqnData'C, PIPE_ACCESS_DUPLEX, &
PIPE_WAIT, PIPE_UNLIMITED_INSTANCES, &
1024, 1024, 120 * 1000, NULL)
PRINT*, pipe1
! Open data pipe
OPEN(UNIT=EqnData, FILE=dataname, ACCESS='STREAM', STATUS='OLD')
READ*
! Close pipe
CLOSE(EqnData)
WRITE (*,*) 'end'
END
C# code:
using System;
using System.IO;
using System.IO.Pipes;
class PipeClient
{
static void Main(string[] args)
{
using (NamedPipeClientStream pipeClient
= new NamedPipeClientStream(".",
"EqnData",
PipeDirection.InOut))
{
// Connect to the pipe or wait until the pipe is available.
Console.Write("Attempting to connect to pipe...");
pipeClient.Connect();
Console.WriteLine("Connected to pipe.");
Console.WriteLine("There are currently {0} pipe server instances open.",
pipeClient.NumberOfServerInstances);
using (StreamReader sr = new StreamReader(pipeClient))
{
// Display the read text to the console
string temp;
while ((temp = sr.ReadLine()) != null)
{
Console.WriteLine("Received from server: {0}", temp);
}
}
}
Console.Write("Press Enter to continue...");
Console.ReadLine();
}
}
If I start up both programs, I see that a pipe is created by the Fortran code and it gets down to the READ
statement but the C# code only gets to pipeClient.Connect()
and then just sits there.
Have I set up the C# side of things correctly? Or maybe I don't have things quite right on the Fortran side of things so that the C# client 'sees' the pipe?