JiscMail Logo
Email discussion lists for the UK Education and Research communities

Help for COMP-FORTRAN-90 Archives


COMP-FORTRAN-90 Archives

COMP-FORTRAN-90 Archives


COMP-FORTRAN-90@JISCMAIL.AC.UK


View:

Message:

[

First

|

Previous

|

Next

|

Last

]

By Topic:

[

First

|

Previous

|

Next

|

Last

]

By Author:

[

First

|

Previous

|

Next

|

Last

]

Font:

Proportional Font

LISTSERV Archives

LISTSERV Archives

COMP-FORTRAN-90 Home

COMP-FORTRAN-90 Home

COMP-FORTRAN-90  2005

COMP-FORTRAN-90 2005

Options

Subscribe or Unsubscribe

Subscribe or Unsubscribe

Log In

Log In

Get Password

Get Password

Subject:

TCP sockets in Fortran 2003 (Was Re: XML Parser)

From:

Aleksandar Donev <[log in to unmask]>

Reply-To:

Fortran 90 List <[log in to unmask]>

Date:

Sun, 11 Dec 2005 17:25:07 -0500

Content-Type:

Multipart/Mixed

Parts/Attachments:

Parts/Attachments

text/plain (50 lines) , sockets.f90 (70 lines) , server.f90 (29 lines) , client.f90 (29 lines)

Charles W Howard wrote:

> Is there a set of code available for FORTRAN to read TCP/IP sockets?
While this may not answer your question per se, since you asked for 
FORTRAN (77?...you certainly cannot write such code in standard Fortran 
if that is what you were asking for), attached are Fortran 2003 codes 
that use the simple socket library libmsock 
(http://www.muquit.com/muquit/software/libmsock/) and C Interop 
features of Fortran 2003 in order to use sockets for interprocess/host 
communication. It literally took more time to google for a simple 
socket library than to write the Fortran codes.

The library libsockm is much easier for me to interface to then the full 
BSD socket interface. In general it is often easier/better to do the 
bulk of the work inside C and then just provide a simple interface that 
does whatever the actual need of the Fortran program are, rather than 
bind to the full C interface with all its complexity. Ian Bush 
mentioned not long ago that he has a module to give Fortran access to 
Unix shared memory segments---there also the majority of the "work" is 
in C using standard unix interfaces, and the Fortran codes only provide 
a top-level interface sufficient for the needs of the Fortran programs.

The attached codes are aasy to compile and run. On my system the server 
program hangs at program exit, but so does the C example provided on 
the libmsock webpage, so the problem is not in the Fortran program. I 
don't know enough (or care enough) about sockets to fix that, but the 
authors of the library can probably help:

f95 -c sockets.f90
f95 server.f90 -o server.x -L./libmsock -lmsock
f95 client.f90 -o client.x -L./libmsock -lmsock
__________
server.x # Start this first
 Client connected!
 Client said: Hello!
 Client said: My name is Aleksandar
 Client disconnected
# It hangs--the child process exits but the parent does not???
__________
client.x # Then run this
 Enter what you want to tell the server:
Hello!
My name is Aleksandar
quit
__________

Best,
Aleks



MODULE MSockets    ! Interface to libmsock    ! A library for TCP/IP client-server applications on Unix by Muhammad A Muquit    ! http://www.muquit.com/muquit/software/libmsock/libmsock.html    USE ISO_C_BINDING    IMPLICIT NONE    PUBLIC        INTEGER, PARAMETER :: C_SIZE_T=C_INT        INTERFACE       ! Interfaces for the main functions              ! int close(int fildes)       ! This one is not in libmsock technically but is needed to close sockets:       FUNCTION CloseSocket(fildes) BIND(C,NAME="close") RESULT(error)          IMPORT          INTEGER(C_INT), VALUE :: fildes          INTEGER(C_INT) :: error       END FUNCTION       ! int ServerSocket(u_short port,int max_servers);       FUNCTION ServerSocket(port,max_servers) BIND(C,NAME="ServerSocket") RESULT(sockfd)          IMPORT          INTEGER(C_SHORT), VALUE :: port          INTEGER(C_INT), VALUE :: max_servers          INTEGER(C_INT) :: sockfd       END FUNCTION       ! int ClientSocket(char *netaddress,u_short port);       FUNCTION ClientSocket(netaddress,port) BIND(C,NAME="ClientSocket") RESULT(sockfd)          IMPORT          CHARACTER(C_CHAR), DIMENSION(*), INTENT(IN) :: netaddress          INTEGER(C_SHORT), VALUE :: port          INTEGER(C_INT) :: sockfd       END FUNCTION       ! int sockGets(int sockfd,char *str,size_t count);       FUNCTION sockGets(sockfd,str,count) BIND(C,NAME="sockGets") RESULT(length)          IMPORT          INTEGER(C_INT), VALUE :: sockfd          CHARACTER(C_CHAR), DIMENSION(*), INTENT(IN) :: str          INTEGER(C_SIZE_T), VALUE :: count          INTEGER(C_INT) :: length ! Bytes read       END FUNCTION       ! int sockRead(int sockfd,char *str,size_t count);       FUNCTION sockRead(sockfd,str,count) BIND(C,NAME="sockRead") RESULT(error)          IMPORT          INTEGER(C_INT), VALUE :: sockfd          INTEGER(C_SIZE_T), VALUE :: count          CHARACTER(C_CHAR), DIMENSION(count), INTENT(OUT) :: str          INTEGER(C_INT) :: error       END FUNCTION       ! int sockPuts(int sockfd,char *str);       FUNCTION sockPuts(sockfd,str) BIND(C,NAME="sockPuts") RESULT(error)          IMPORT          INTEGER(C_INT), VALUE :: sockfd          CHARACTER(C_CHAR), DIMENSION(*), INTENT(IN) :: str ! NULL terminated          INTEGER(C_INT) :: error       END FUNCTION       ! int sockWrite(int sockfd,char *str,size_t count);       FUNCTION sockWrite(sockfd,str,count) BIND(C,NAME="sockWrite") RESULT(error)          IMPORT          INTEGER(C_INT), VALUE :: sockfd          INTEGER(C_SIZE_T), VALUE :: count          CHARACTER(C_CHAR), DIMENSION(count), INTENT(IN) :: str          INTEGER(C_INT) :: error       END FUNCTION    END INTERFACE        END MODULE
PROGRAM MSockets_Server    USE ISO_C_BINDING    USE MSockets    IMPLICIT NONE        INTEGER(C_SHORT) :: port    INTEGER(C_INT) :: sockfd    CHARACTER(KIND=C_CHAR, LEN=1024) :: buffer    INTEGER(C_SIZE_T) :: count    INTEGER(C_INT) :: length, error          port=HUGE(port) ! Most likely unused port    sockfd=ServerSocket(port,2_c_int) ! It will return only once a connection is made    IF(sockfd<0) STOP "Failed to open server socket"        WRITE(*,*) "Client connected!"    DO       length=sockGets(sockfd,buffer,INT(LEN(buffer),C_SIZE_T))       IF(length<0) EXIT       IF(length>0) WRITE(*,*) "Client said: ", buffer(1:length)    END DO    WRITE(*,*) "Client disconnected"           error=CloseSocket(sockfd)    IF(error<0) STOP "Failed to close server socket"     END PROGRAM
PROGRAM MSockets_Server    USE ISO_C_BINDING    USE MSockets    IMPLICIT NONE        INTEGER(C_SHORT) :: port    INTEGER(C_INT) :: sockfd    CHARACTER(KIND=C_CHAR, LEN=1024) :: buffer    INTEGER(C_SIZE_T) :: count    INTEGER(C_INT) :: length, error          port=HUGE(port) ! Most likely unused port    sockfd=ClientSocket("atom.princeton.edu",port)    IF(sockfd<0) STOP "Failed to open client socket"        WRITE(*,*) "Enter what you want to tell the server:"    DO       READ(*,"(A)") buffer       IF(buffer=="quit") EXIT ! Last input       error=sockPuts(sockfd,TRIM(buffer)//C_NEW_LINE)       IF(error<0) STOP "Server died!"    END DO    error=CloseSocket(sockfd)    IF(sockfd<0) STOP "Failed to close client socket"     END PROGRAM

Top of Message | Previous Page | Permalink

JiscMail Tools


RSS Feeds and Sharing


Advanced Options


Archives

December 2023
February 2023
November 2022
September 2022
February 2022
January 2022
June 2021
November 2020
September 2020
June 2020
May 2020
April 2020
December 2019
October 2019
September 2019
March 2019
February 2019
January 2019
November 2018
October 2018
September 2018
August 2018
July 2018
May 2018
April 2018
March 2018
February 2018
January 2018
December 2017
November 2017
October 2017
September 2017
August 2017
July 2017
June 2017
May 2017
April 2017
March 2017
January 2017
December 2016
November 2016
October 2016
September 2016
August 2016
July 2016
June 2016
May 2016
April 2016
March 2016
February 2016
December 2015
November 2015
October 2015
September 2015
August 2015
June 2015
April 2015
March 2015
January 2015
December 2014
November 2014
October 2014
August 2014
July 2014
May 2014
April 2014
March 2014
February 2014
January 2014
December 2013
November 2013
July 2013
June 2013
May 2013
April 2013
February 2013
January 2013
December 2012
November 2012
October 2012
September 2012
August 2012
July 2012
June 2012
April 2012
March 2012
February 2012
January 2012
December 2011
November 2011
October 2011
September 2011
August 2011
July 2011
June 2011
May 2011
April 2011
March 2011
February 2011
January 2011
December 2010
November 2010
October 2010
August 2010
July 2010
June 2010
March 2010
February 2010
January 2010
December 2009
October 2009
August 2009
July 2009
June 2009
March 2009
February 2009
January 2009
December 2008
November 2008
October 2008
September 2008
August 2008
July 2008
June 2008
May 2008
April 2008
March 2008
February 2008
December 2007
November 2007
October 2007
September 2007
August 2007
July 2007
June 2007
May 2007
April 2007
March 2007
January 2007
2006
2005
2004
2003
2002
2001
2000
1999
1998
1997


JiscMail is a Jisc service.

View our service policies at https://www.jiscmail.ac.uk/policyandsecurity/ and Jisc's privacy policy at https://www.jisc.ac.uk/website/privacy-notice

For help and support help@jisc.ac.uk

Secured by F-Secure Anti-Virus CataList Email List Search Powered by the LISTSERV Email List Manager