11 Interoperability with C: Exposing your app to the web

 

This chapter covers

  • Why invoke C code from Fortran?
  • Interfacing with C built-in types, structs, and functions from Fortran
  • Writing a minimal Fortran TCP client and server

Pure Fortran is powerful for the numerical and array-oriented computation that’s ubiquitous in physical sciences and engineering. However, quite a few things aren’t possible in Fortran alone; fortunately, they can be done in a low-level systems programming language such as C. They include reading and writing data to hardware devices, drawing graphics on the screen in real time, and sending data over the internet. Interoperability with C allows a programmer to call C functions from Fortran programs. This is important for two reasons:

  • It enables the above-mentioned low-level functionality and gives Fortran access to the C ecosystem of libraries.
  • C itself is easily called from many popular programming languages today, such as Python, JavaScript, Go, or Rust. By using C as the interfacing language, Fortran code can be invoked from most other languages. In the real world, this allows Fortran code to be used within web servers, databases, and real-time graphics.

11.1 Interfacing C: Writing a minimal TCP client and server

11.1.1 Introducing networking to Fortran

11.1.2 Installing libdill

11.2 TCP server program: Receiving network connections

11.2.1 IP address data structures

11.2.2 Initializing the IP address structure

11.2.3 Checking IP address values

11.2.4 Intermezzo: Matching compatible C and Fortran data types

11.2.5 Creating a socket and listening for connections

11.2.6 Accepting incoming connections to a socket

11.2.7 Sending a TCP message to the client

11.2.8 Closing a connection

11.3 TCP client program: Connecting to a remote server

11.5 Answer key