Fortran

Guide To Learn

1. Interoperability with C: Exposing your app to the web

Some interesting mixed Fortran-C projects

I’ll provide a list here of some of what I think are interesting and useful mixed Fortran-C projects. I encourage you to play with them, explore the source code, and see other possible ways to interface with C code from Fortran: Now that you’ve gotten this far, what are you going to make?

The complete client program

The following listing provides the complete program for the Fortran TCP client. Listing 11.9 Fortran TCP client–the complete program If you now recompile the client program and run it again (making sure the server is still running in the other terminal), you’ll see a familiar greeting message: The first item in the output (the numeral […]

Receiving a message

In the previous section, you learned how to send a TCP message from the server to a remote client by calling the msend function in libdill. We tested that it worked by communicating to the server with command-line networking tools such as curl, nc, and telnet. What if you wanted to exchange data over the network between two Fortran programs? We […]

Connecting to a remote socket

Like we did on the server, first we need to initialize a data structure to hold the IP address and port number. In this case, they’ll be the IP address and port number of the remote host to which we’ll connect. Here’s the prototype of the ipaddr_remote function in libdill: ❶ This function returns an int. ❷ Pointer to […]

TCP client program: Connecting to a remote server

As I mentioned before, to implement the client, we’ll need Fortran interfaces to a few new libdill functions (figure 11.6): Figure 11.6 The Fortran TCP client program, illustrated Having gone through the server implementation, writing these interfaces for the client should be straightforward.

Closing a connection

There’s only one step left before we wrap up and test our little TCP server program–once we send the message, we want to close the connection gracefully. libdill provides a function to do this–tcp_close. Can you help me implement the interface to it? Try exercise 2 in the sidebar. Exercise 2: Fortran interfaces to suffix_detach and tcp_close You […]

Sending a TCP message to the client

Finally, on each accepted connection, we’ll send a message to the client to let them know the server is alive. Not so fast, though. Recall that the data is just a stream of bytes, and the TCP protocol defines messages that are specially formatted packets of binary data. Fortunately, libdill provides a convenience function, suffix_attach, to append a suffix […]

Accepting incoming connections to a socket

We now have our socket open to the world, and we’re listening for connections. The C code (libdill) and the operating system will do the work of making the connection happen, so we don’t have to worry about that part. Our job here is to accept an incoming connection and do something with it (send […]

Creating a socket and listening for connections

We now have an IP address data structure, and we can initialize its value by calling the ipaddr_local function. Let’s now use this structure to open a socket and listen for incoming connections. To do this, we’ll use the tcp_listen function. Like before, we’ll first look at the interface of the C function from the libdill documentation: If successful, the […]

Intermezzo: Matching compatible C and Fortran data types

Now that you’ve got a taste of interfacing C-structs with functions from Fortran, it’s a good time to go into more detail on compatible C and Fortran data types. I mentioned earlier that C types are different from Fortran types: int is not exactly integer, and float is not exactly real. What matters, however, is that we have a reliable way […]

Scroll to top