2

I have a C++ application and FORTRAN application which are communicating via file. (Both direction)

FORTRAN application is writing data to file and C++ is reading the data from the file. Now a new requirement has come wherein I have to directly communicate (as file operations are expensive) with C++ code form FORTRAN and from FORTRAN to C++. I implemented socket on C++ side and tried to implement the socket on FORTRAN side. I am not getting enough information on internet about sockets on FORTRAN.

The FORTRAN legacy code is written in g77. C++ is in MFC.

  1. Is there any better approach to achieve this apart from socket communication?
  2. Any links about sockets on FORTRAN using g77 will be helpful.

I know, I should not be asking for links on this forum. If this question doesn’t belong to this forum, where can I ask this kind of question?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
StackIT
  • 1,172
  • 2
  • 13
  • 25
  • 6
    Hmmm, sockets and Fortran, good luck with that. Perhaps you'd be better off using (modern) Fortran's *interoperability with C* features. Do be careful, your legacy code is NOT written in `g77`. I imagine your legacy code might be written in FORTRAN77 but `g77` is the name of a compiler not a language variant. The point of picking you up on this is that a modern Fortran compiler (not `g77`) should compile your code + interoperability with C. – High Performance Mark May 23 '14 at 09:51
  • 3
    Start with reading this http://stackoverflow.com/questions/10305689/sockets-programming-gfortran – Vladimir F Героям слава May 23 '14 at 11:10
  • I rewrote your question, to make it ask for one specific thing. Ask the other questions in separate posts. My personal advice is to use named pipes, you should be able to open them as regular files from your fortran program. – sashoalm May 23 '14 at 13:01
  • @sashoalm: Thanks... Will try to implement named pipes and also try interoperability. – StackIT May 23 '14 at 13:03

1 Answers1

0

If the communication is one-way only, you can use named pipes or even an anonymous pipe.

As with all inter-process communication, there are a few gotchas.

Your best bet is to use unformatted stream. This is a relatively new feature, introduced in Fortran 2003, but all major compilers support it. So, open your file with

  OPEN(20,file="myfifo",access="stream",form="unformatted",status="old",action="write")

and write to it using statements like

    WRITE (20) A

Why unformatted stream?

First, unformatted: You wrote that speed matters in your case. Converting numbers to decimal and back again costs a lot of CPU cycles, and may also lose accuracy.

Second, stream: The usual implementation of unformatted I/O with Fortran uses record markers - a marker before the record showing how long the record is, a marker behind the record showing the same information.

How does the runtime system know how long the record it is going to write will be? Well, it doesn't. So a common implementation is to write a dummy record marker, write the record (and learn how long it is), write the trailing marker, seek to the first marker, overwrite the dummy record marker, and continue. This doesn't work for a named pipe - you cannot seek there. You may be lucky if you are still within the buffer, but you cannot depend on it.

Regarding the compiler: If compatibility to old g77 code is an issue, use gfortran. It supports ACCESS="STREAM" and has most of the extensions from g77. g77 is old and hasn't been supported for quite a few years.