[Fortran] 纯文本查看 复制代码
type T_client_server_message
union
ma ...[/quote]
你好,感谢你的解答,我这样修改了之后,好像我把A=abcd的值send后,server端只能接收到A一个,即a同样,我用来接收server段的数据的C也是没有的。请问这还有什么问题呢,也许我没有完全明白。
[mw_shl_code=fortran,true]module socketconnect
contains
subroutine rpccall(A,C)
use KERNEL32
use WS2_32
implicit none
character::A
character::C
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
sequence
union
MAP
character*4 A
character*4 C
ENDMAP
map
character*4 buffer
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=5000
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(*,*)A
clientServerMessage%A=A
sendlen=send(sender,clientServerMessage%BUFFER,sizeof(clientservermessage%a),0)
if(sendlen<0)then
write(*,*)"发送失败"
end if
!接收数据
receivelen=recv(sender,clientServerMessage%buffer,sizeof(clientservermessage%buffer),0)
if(receivelen<0)then
write(*,*)"接受失败,程序退出"
stop
else
write(*,*)"server say:"
C=clientServerMessage%C
print*, C
end if
end do
status =closesocket(sender)
status =WSACleanup()
pause
stop
end subroutine
end module socketconnect
program client
use socketconnect
character:: aarray2
character:: aarray1
call rpccall(aarray1,aarray2)
end
。