Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29273 Discussions

How do I build a project using sockets?

oddstray
Beginner
1,397 Views

Hi,

I'm trying to create a TCP/IP client and server in FORTRAN. Socket programming does seem to be supported based on some code snippets I've seen. But I can't find anything in the documentation that tells me how to do it. For example, I know I need to call WSAGetLastError() for error-handling. But the compiler doesn't know what that is. Are there include files and libraries or modules I need to include in my project?

And, does anyone have any complete examples? Thanks.

0 Kudos
2 Replies
Jugoslav_Dujic
Valued Contributor II
1,397 Views
Visual Fortran offers a vast number of interfaces to system routines, but not all of them can possibly be accompanied by Fortran samples. You need to check 3rd-party tutorials. For sockets, I find the following site an excellent resource:

http://www.sockaddr.com/ExampleSourceCode.html

For using Winsock with Fortran, you should USE WSOCK32 or USE WS2_32 to get the interfaces correct (those two are almost the same -- I'm not sure which is the "current" version). In addition, you should link your application with ws2_32.lib.

Here's a code sample in Fortran. Apparently, it dates sometime before the above modules were written, so it does a lot of "do-it-yourself" declarations (i.e. its own module Winsock).

Hope that helps,
0 Kudos
joerg_kuthe
Novice
1,397 Views

The module ws2_32.f90 delivered with Intel Visual Fortran provides you with the INTERFACEs to call those socket functions. Put

USE IFWIN

in the header of your program and those INTERFACEs are available. To open a streaming TCP socket you might code:

FUNCTION mySocket( iErrorCode ) RESULT(iSocket)

USE IFWIN

INTEGER(POINTER_LEN) :: iSocket

iSocket = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP )

IF ( iSocket == INVALID_SOCKET ) THEN

! ... handle error

iSocket = -1

ELSE

! ok

! ...

END IF

END FUNCTION

Hope this helps.

Jrg Kuthe

www.qtsoftware.de

0 Kudos
Reply