Topic: Fortran TCP/IP sockets

Hi There!
I wanted to write a pair of Fortran programs that would communicate to each other over a Network. As such, I had a good look around the internet but couldn't find much at all. Do you know of a Fortran Sockets implementation?
Further to that, I did find an antique Intel Fortran implementation of a baby sized "Proof of Concept" Fortran program that used Sockets. However, it depends on the Visual Studio library "Wininit.lib" to define the sockets etc.  I did get my hands on a copy of the *.lib file, but I have no idea how to link it it. Can you help me with that too?
Any help is really appreciated.
Regards,
Pete

Re: Fortran TCP/IP sockets

Using the older library almost certainly won't work with Simply Fortran.  As an exercise, though, I've written a browser in pure Fortran for the Gemini protocol, a sort of simpler-than-the-web Internet experience.  In doing so, I wrote a pretty minimal socket interface that works on Windows and Linux, shown here in network.F90 that wraps socket functions in Fortran.  Windows has the added step of initializing any application that will use TCP/IP, so be careful.

The sockets are then used pretty much as you would in C.  In that project, the open_connection function in request.f90 shows how the socket would be handled at a low level.

Hopefully some of this is helpful.

Jeff Armstrong
Approximatrix, LLC

Re: Fortran TCP/IP sockets

Hi Jeff.
Thank you for replying so quickly. The code that you posted was very helpful in understanding the area. However, when I put it together to simply test if I could open a socket, I got linker errors for the underlying C functions ie "undefined reference to `socket' ".
I can easily see the bindings from Fortran to the underlying C functions in the code but I seem to be missing the C functions. Is there another library that I need to link it? (If so, how?) I've poured over the whole project but can't see anything.
Regards,
Peter

Re: Fortran TCP/IP sockets

Yes, you need to link with the Windows Sockets library.  In Project Options, you would have to add the following:

-lws2_32

to the Linker box under "Compiler Flags" to use sockets.  This library is included in our distribution.

Also, make sure, on Windows, that you call WSAStartup very early in your program or all your socket operations will fail.  This function is Windows-specific, but everything else that was network-related in the linked project is completely portable to Linux, macOS, BSD, etc.  My wrapper for WSAStartup is in wsa.f90.

Jeff Armstrong
Approximatrix, LLC

Re: Fortran TCP/IP sockets

Hi Jeff,
That worked great but it brought another raft of problems in the linking. In your example files there was a "use jessl" statement. I found jessl and included it in my little test piece but it refers to a lot of SSL functions. Thinking they were useful too, I downloaded the SSL library that you recommended in that project. Now the issue I have is (of course) undefined references to the SSL functions and I don't know how to link the SSL library into my program. I looked in the documentation,but it's not covered. I'm sorry to keep asking newbie questions, but in all my Fortran work, I have never had to link to an external library of any sort and it's a mystery to me. Can you help with this too? Or maybe point me towards doumentation that I may have overlooked?
Regards,
Pete

Re: Fortran TCP/IP sockets

Pete,

If you want to use SSL as well, you need to add these flags to the Linker box in Compiler Flags:

-lssl -lcrypto -lws2_32 -lcrypt32

You might also need to tell the compiler where these libraries are unless you extracted that distribution of OpenSSL directly in your project directory.  You'll need to add them using the File Locations section in Project Options.

If you're just experimenting with sockets, though, you might want to stay away from SSL.  It simply adds another layer of complexity that you probably don't need right now.

Jeff Armstrong
Approximatrix, LLC