|
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里面 ·····求大神指教
|
|