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.
- Is there any better approach to achieve this apart from socket communication?
- 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?
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
and write to it using statements like
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.