Fortran Coder

查看: 8627|回复: 7
打印 上一主题 下一主题

[子程序] 能否把socket的连接过程变成一个子程序

[复制链接]

23

帖子

10

主题

0

精华

熟手

F 币
145 元
贡献
90 点
跳转到指定楼层
楼主
发表于 2015-5-21 13:19:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
module socketconnect
  contains

  subroutine socketcall(i,A,B,C)
      use KERNEL32
      use WS2_32


         implicit none

         integer WINSOCK_V2_2                        !版本

         integer success
         parameter(WINSOCK_V2_2=X'202',success=0)
         type(T_WSADATA) wsaInfo                     !版本信息结构

         integer receivelen        
         integer sendlen   

         type T_SOCKADDR_UNION
         sequence
             union
                  map
                      type(T_SOCKADDR_IN) sockAddrIn
                  end map
                  map
                      type(T_SOCKADDR) sockAddr
                  end map
             end union
         end type

         type(T_SOCKADDR_UNION) connectionInfo  !Client_add


         type T_client_server_message
                union
                     map
                        character*(500) buffer

                     end map
                     map
                        real A(2,2)
                        real B(2,2)
                        real C(2,2)
                        integer i
                     end map
                end union
         end type

         type(T_client_server_message) ClientServerMessage   

         integer sender       !socket_send
         character*(15) host
         integer*2 port
         integer r
         integer status


         ! Initialize Winsock v2.
         status=WSAStartup(WINSOCK_V2_2,wsaInfo)
         if(status.NE.success)then
            write(*,*)'WSAStartup-',status
            stop
         end if

         ! 创建套接字
         sender=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)   
         if(sender.EQ.INVALID_SOCKET) then
             write(*,*)'socket-',INVALID_SOCKET

             status=WSACleanup()
         end if

         ! 设置连接地址
         host='127.0.0.1'
         port=55000

         connectionInfo%sockAddrIn%sin_family=AF_INET
         connectionInfo%sockAddrIn%sin_port=htons(port)
         connectionInfo%sockAddrIn%sin_addr%s_addr =inet_addr(host(1:LEN_TRIM(host)))

         status=connect(sender,%ref(connectionInfo%sockAddr),SIZEOF(connectionInfo%sockAddr))
         if(status.EQ.SOCKET_ERROR)then
            write(*,*)'连接失败'
         end if


         !进行聊天
         do while(.true.)



                        !发送数据
                          write(*,*)"please enter message:"
                          read(*,*)clientServerMessage%A
                          read(*,*)clientServerMessage%B
                          sendlen=send(sender,clientServerMessage%buffer,500,0)
                          if(sendlen<0)then
                            write(*,*)"发送失败"
                          end if



                         read(*,*)clientServerMessage%i
                          sendlen=send(sender,clientServerMessage%buffer,500,0)
                          if(sendlen<0)then
                            write(*,*)"发送失败"
                          end if
                          !接收数据
                          receivelen=recv(sender,clientServerMessage%buffer,500,0)
                          if(receivelen<0)then
                            write(*,*)"接受失败,程序退出"
                            stop
                          else
                            write(*,*)"server say:"
                            call printmatrix(clientServerMessage%C)
                          end if




                     end do



             status =closesocket(sender)
             status =WSACleanup()

             pause

        stop

end subroutine


subroutine printmatrix(matrix)
        integer i,length
        real matrix(:,:)
        length=size(matrix,1)
        do i=1,length
          write(*,*)matrix(i,:)
        end do
        write(*,*)""
        end subroutine

end module socketconnect


program client
use socketconnect

real ::matrix1(2,2)=reshape((/1,2,1,3/),(/2,2/))
real ::matrix2(2,2)=reshape((/1,2,1,3/),(/2,2/))

real matrix3(2,2)



call socketcall(1,matrix1,matrix2,matrix3)        

end

无法把matrix1的值传到A里面 ·····求大神指教



分享到:  微信微信
收藏收藏 点赞点赞 点踩点踩

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
607 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

沙发
发表于 2015-5-21 16:33:30 | 只看该作者
问题1,socketcall 里没有对 A B C 进行定义
问题2,ClientServerMessage 里的 A B C 并不是虚参的 A B C

23

帖子

10

主题

0

精华

熟手

F 币
145 元
贡献
90 点
板凳
 楼主| 发表于 2015-5-21 16:46:08 | 只看该作者
楚香饭 发表于 2015-5-21 16:33
问题1,socketcall 里没有对 A B C 进行定义
问题2,ClientServerMessage 里的 A B C 并不是虚参的 A B C ...

module socketconnect
  contains

  subroutine rpccall(A,B,i,C)
      use KERNEL32
      use WS2_32
         
         
         implicit none
         real A(2,2)
         real B(2,2)
         real C(2,2)
         integer i



         integer WINSOCK_V2_2                        !版本
         
         integer success
         parameter(WINSOCK_V2_2=X'202',success=0)
         type(T_WSADATA) wsaInfo                     !版本信息结构

         integer receivelen        
         integer sendlen   
         
         type T_SOCKADDR_UNION
         sequence
             union
                  map
                      type(T_SOCKADDR_IN) sockAddrIn
                  end map
                  map
                      type(T_SOCKADDR) sockAddr
                  end map
             end union
         end type

         type(T_SOCKADDR_UNION) connectionInfo  !Client_add
                           

         type T_client_server_message
                union
                     map
                        character*(500) buffer
                        
                     end map
                     map
                        real A(2,2)
                        real B(2,2)
                        real C(2,2)
                        integer i
                     end map
                end union
         end type

         type(T_client_server_message) ClientServerMessage   

         integer sender       !socket_send
         character*(15) host
         integer*2 port
         integer r
         integer status


         ! Initialize Winsock v2.
         status=WSAStartup(WINSOCK_V2_2,wsaInfo)
         if(status.NE.success)then
            write(*,*)'WSAStartup-',status
            stop
         end if

         ! 创建套接字
         sender=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP)   
         if(sender.EQ.INVALID_SOCKET) then
             write(*,*)'socket-',INVALID_SOCKET

             status=WSACleanup()
         end if

         ! 设置连接地址
         host='127.0.0.1'
         port=55000

         connectionInfo%sockAddrIn%sin_family=AF_INET
         connectionInfo%sockAddrIn%sin_port=htons(port)
         connectionInfo%sockAddrIn%sin_addr%s_addr =inet_addr(host(1:LEN_TRIM(host)))

         status=connect(sender,%ref(connectionInfo%sockAddr),SIZEOF(connectionInfo%sockAddr))
         if(status.EQ.SOCKET_ERROR)then
            write(*,*)'连接失败'
         end if

         
         !进行聊天
         do while(.true.)
                  


                        !发送数据
                          write(*,*)"please enter message:"
                        
                           write(*,*)clientServerMessage%A
                           write(*,*)clientServerMessage%B

                           write(*,*)clientServerMessage%i
                          sendlen=send(sender,clientServerMessage%buffer,500,0)
                          if(sendlen<0)then
                            write(*,*)"发送失败"
                          end if
                          !接收数据
                          receivelen=recv(sender,clientServerMessage%buffer,500,0)
                          if(receivelen<0)then
                            write(*,*)"接受失败,程序退出"
                            stop
                          else
                            write(*,*)"server say:"
                            call printmatrix(clientServerMessage%C)
                          end if

                          
                       

                     end do
              

            
             status =closesocket(sender)
             status =WSACleanup()

             pause

        stop

end subroutine


subroutine printmatrix(matrix)
        integer i,length
        real matrix(:,:)
        length=size(matrix,1)
        do i=1,length
          write(*,*)matrix(i,:)
        end do
        write(*,*)""
        end subroutine

end module socketconnect


program client
use socketconnect

real ::matrix1(2,2)=reshape((/1,2,1,3/),(/2,2/))
real ::matrix2(2,2)=reshape((/1,2,1,3/),(/2,2/))

real ::matrix3(2,2)



call rpccall(matrix1,matrix2,1,matrix3)        
      
end

大师我修改了下但是程序还是不能读取到我在主程序中定义的数值·····

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
607 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

地板
发表于 2015-5-21 16:52:30 | 只看该作者
看上面回答的第二条

23

帖子

10

主题

0

精华

熟手

F 币
145 元
贡献
90 点
5#
 楼主| 发表于 2015-5-21 18:31:13 | 只看该作者
楚香饭 发表于 2015-5-21 16:52
看上面回答的第二条

不知道如何解决···没有思路···求大神再指点下····

1963

帖子

12

主题

5

精华

论坛跑堂

臭石头雪球

F 币
1357 元
贡献
574 点

美女勋章热心勋章星光勋章新人勋章贡献勋章管理勋章帅哥勋章爱心勋章规矩勋章元老勋章水王勋章

6#
发表于 2015-5-21 18:37:25 | 只看该作者
虚参是虚参,你定义的结构体是结构体。
结构体可以有多个,但虚参只有一个。

所以虚参的 A B C 和结构体里的 A B C 成员,是不同的。
简单的办法就是赋值
clientServerMessage%A = A

23

帖子

10

主题

0

精华

熟手

F 币
145 元
贡献
90 点
7#
 楼主| 发表于 2015-5-21 19:48:19 | 只看该作者
fcode 发表于 2015-5-21 18:37
虚参是虚参,你定义的结构体是结构体。
结构体可以有多个,但虚参只有一个。

恩恩·····谢谢大师····你真的很耐心,这个问题解决了···新问题出现了··我发现我的程序疯狂重复运转中····

712

帖子

4

主题

0

精华

大师

农村外出务工人员

F 币
607 元
贡献
311 点

新人勋章爱心勋章水王勋章元老勋章热心勋章

8#
发表于 2015-5-21 21:53:36 | 只看该作者
因为你写了
  do while(.true.)
您需要登录后才可以回帖 登录 | 极速注册

本版积分规则

捐赠本站|Archiver|关于我们 About Us|小黑屋|Fcode ( 京ICP备18005632-2号 )

GMT+8, 2024-5-10 10:35

Powered by Tencent X3.4

© 2013-2024 Tencent

快速回复 返回顶部 返回列表