套接字编程gfortran [英] sockets programming gfortran

查看:183
本文介绍了套接字编程gfortran的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在我的Fortran应用程序中调用网络功能。我的老板希望我在Fortran中做所有事情,而不是使用C和Fortran。我们已经在Windows上使用PGI的Fortran编译器完成了应用程序的一个版本。我们正在将它转移到Linux,我们可能会使用它们的编译器。现在,我正在使用gfortran。



我为这些网络调用创建了一个接口,并且所有内容都编译和链接。下面的代码与我正在做的类似,除了接口和常量在模块中。

  PROGRAM MAIN 

INTEGER,PARAMETER :: AF_INET = 2
INTEGER,PARAMETER :: SOCK_STREAM = 1
INTEGER,PARAMETER :: IPPROTO_TCP = 6

INTERFACE
函数socket(domain,type,protocol)
INTEGER :: socket,domain,type,protocol
END FUNCTION
END INTERFACE

sock = socket(AF_INET, SOCK_STREAM,IPPROTO_TCP)
WRTIE(*,*)Socket returned:,sock

END PROGRAM

运行程序时,套接字函数无法返回-1。我真的不知道发生了什么事。我不在命令行中添加任何库,所以我猜想它正确地链接到默认库。我使用

  gfortran -o MAIN_PROGRAM MAIN_PROGRAM.F90 -fno-underscoring 


解决方案

您可以使用Fortran 2003中引入的ISO_C_Binding来访问C库功能,这是最干净和最便携式选件。 Gfortran文档有一些详细信息它,像任何其他供应商手册一样。还有一些项目旨在实现用于Fortran 90的POSIX接口: fortranposix posix90 。但正如我所说的,使用F2003功能的适当的C-Binding接口可能是最干净的选项,另请参阅 fortranwiki

编辑:
这里是您添加ISO-C-Binding胶水的代码gfortran 4.4.5):

 程序testsocket 
use,intrinsic :: iso_c_binding

隐式无

接口
函数socket(域,类型,协议)bind(c,name =socket)
use,intrinsic :: iso_c_binding
integer kind = c_int):: socket
整数(kind = c_int),value :: domain,type,protocol
结束函数套接字
结束接口

整数:: sock
$ b $ sock = socket(2_c_int,1_c_int,6_c_int)

write(*,*)Socket returned:,sock

end program testsocket


I want to be able to call networking functions in my Fortran application. My boss wants me to do everything in Fortran instead of using C and Fortran. We have already done a version of the application using PGI's Fortran compiler on Windows. We are moving it to Linux where we will probably use their compiler. Right now, I'm using gfortran.

I have created an interface for these networking calls, and everything compiles and links. The code below is something similar to what I'm doing except the interfaces and constants are in a module.

PROGRAM MAIN

    INTEGER,PARAMETER  ::AF_INET = 2
    INTEGER,PARAMETER  ::SOCK_STREAM = 1
    INTEGER,PARAMETER  ::IPPROTO_TCP = 6

    INTERFACE
      FUNCTION socket(domain,type,protocol)
        INTEGER  ::socket,domain,type,protocol
      END FUNCTION
    END INTERFACE

    sock = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)
    WRTIE(*,*)"Socket returned: ",sock

END PROGRAM

When I run the program, the socket function fails returning -1. I don't really know what's going on. I don't add any libraries on the command line so I'm guess it is linking with the default libraries correctly. I compile using

gfortran -o MAIN_PROGRAM MAIN_PROGRAM.f90 -fno-underscoring

解决方案

You can make use of the ISO_C_Binding introduced in Fortran 2003 to access C library functionality, this is the cleanest and most portable option you have. The Gfortran documentation has some details on it, as any other vendor manual. There are also some projects aiming to implement interfaces to POSIX for Fortran 90: fortranposix and posix90. But as I said a proper C-Binding interface using the F2003 capabilities is probably the cleanest option, see also the fortranwiki.

Edit: Here is your code with the ISO-C-Binding glue added (tested with gfortran 4.4.5):

program testsocket
  use, intrinsic ::  iso_c_binding

  implicit none

  interface
    function socket(domain, type, protocol) bind(c, name="socket")
      use, intrinsic :: iso_c_binding
      integer(kind=c_int) :: socket
      integer(kind=c_int), value :: domain, type, protocol
    end function socket
  end interface

  integer :: sock

  sock = socket(2_c_int, 1_c_int, 6_c_int)

  write(*,*) "Socket returned: ", sock

end program testsocket

这篇关于套接字编程gfortran的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆