Demo of a Fortran code communicating through a socket with a client

Purpose of this repo

I wanted to be able to make a Fortran code communicate with an other program. Sockets were the simplest solution to achieve this. Being no Fortran programmer, I struggled to figure out how to do it. I was expecting to find some sort of example code on the web but I failed to. The web contains many examples of server/client programming in C and higher level languages. I was not able to find such an example in Fortran. So, now that I achieved my goal, I put a simple example on the web in the hope that this may help people out there.

Difficulties

Server/client communications is part of the basic training of a student in computer science at master level. Usually, this is put into practice in C or some other higher level language (usually in Python these days).

I am a C programmer (and other languages) but I am no Fortran programmer. So the biggest difficulty turned out for me to understand the equivalence between Fortran types and C types, and how to call (C) system functions passing the correctly typed parameters in the Fortran code.

The content of this repo

This repo contains:

Usage

git clone https://github.com/ph-preux/sockets-in-Fortran.git
cd sockets-in-Fortran
make

This should create two executable files, one named server the second one named client.
./server
in an other window, type ./client.
There is goes: the two are exchanging a couple of mundanities and stop.

Licence

MIT License

Copyright (c) 2020 Philippe Preux

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Feedback

If you find some bug, or if you can provide a better coded source, or a more portable code, please email me.

Ack

While developing this demo, I benefited from a couple of helps:

Moreover, Luke Rodgers proposed me to use a module to define character strings functions instead of declaring them in server.f90. The idea is to use the c_interface_module.f90. It is pretty easy to update server.f90 to use it, so I let the interested reader do it.