Fortran

Guide To Learn

Introducing networking to Fortran

The C programming model is that the programmer knows exactly what they want to do and how to use the language constructs to achieve that goal. The language lets the expert programmer express what they want in the minimum time by staying out of their way.

 –Nick Parlante, Essential C

While there are easier ways to write networking applications, this example demonstrates well the power of invoking C from Fortran. It’s simple enough that we won’t get bogged down in the details, and yet intricate enough that we can’t implement it with Fortran alone. Except for the native parallelism with coarrays that’s built into the language, Fortran offers no networking capability at all. This is where C and its wide array of systems programming libraries come to the rescue.

A fundamental concept in the implementation of computer networks is a socket. A socket is a resource provided by the operating system, and it allows software to access the networking hardware, such as Wi-Fi or Ethernet adapters, to send and receive data across the network. The data that goes through sockets is streams of bytes. A transport layer protocol such as TCP carefully formats packets of bytes into discrete messages. Application layer protocols such as HTTP or websockets are constructed by further formatting of TCP messages. They allow communication with context, which allows for richer applications, such as web pages and chat clients.

For simplicity, we won’t interface with sockets directly on the operating system level; we’ll use an existing high-level C library instead. There are many libraries out there to choose from, offering various levels of abstraction. For this example, let’s use libdill (http://libdill.org), because it’s lightweight and easy to install and use. libdill provides a high-level user interface with low-level operations in the operating system. Described as “structured concurrency for C,” libdill’s main feature is a coroutine, a popular concurrency model in many programming languages. Besides coroutines, libdill also provides easy-to-use interfaces with various networking protocols, including TCP, HTTP, websockets, and others. Perhaps most importantly, libdill is easy to download and compile and has great documentation. In this chapter, we’ll focus on a small subset of libdill–its TCP stack of functions.

Figure 11.1 The hierarchy in our TCP client-server stack

Figure 11.1 illustrates the components of our TCP client-server project. Read this diagram from top left to bottom right. libdill-fortran, the Fortran interface with libdill, will invoke C functions from our Fortran program. The libdill functions will, in turn, make low-level function calls provided by the operating system to send or receive data using the TCP/IP protocol.

As you can probably guess, we’ll focus on implementing the top left component from this diagram: libdill-fortran. The libdill C library is already available, and compiling it gives us all the plumbing we need from the operating system to perform TCP networking operations (figure 11.2).

Figure 11.2 Implementing a minimal TCP server and client programs in Fortran

If the function names in this diagram seem a bit cryptic, I hear you! It will all become much clearer as we work through the steps of implementing the interfaces with each of these functions.

Introducing networking to Fortran

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top