我使用此server
从C++
UDP Client
接收多个数据包。
当我需要一个包时,我使用Request
发送Client UDP
以让C++
客户端发送包。当我多次执行此过程时,我会错过一些软件包并收到此错误:
GNAT.SOCKETS.SOCKET_ERROR: [11] Resource temporarily unavailable
我认为可能会导致因为客户端在服务器监听之前发送了包,但我不确定。有什么方法可以解决吗?如果这是问题,有没有办法确保在客户端发送UDP
消息之前准备好我的服务器?
procedure RECEIVE_DATA (
DEST_UDP_PORT : In Integer;
SRC_UDP_PORT : In Integer;
WAIT_TIME : In DURATION;
MESSAGE_ADDRESS : Out System.Address;
WAIT_RESULT : Out Integer;
MESSAGE_SIZE : Out Integer
) is
Address : Sock_Addr_Type;
Socket : Socket_Type;
Channel : Stream_Access;
Receive_Timeout : constant Duration := WAIT_TIME;
Offset : Ada.Streams.Stream_Element_Count;
Data : Ada.Streams.Stream_Element_Array (1 .. 10000);
begin
Initialize (Process_Blocking_IO => False);
WAIT_RESULT := 0;
MESSAGE_SIZE := 0;
-- Create Socket
Create_Socket (Socket, Family_Inet, Socket_Datagram);
Set_Socket_Option (Socket => Socket,
Option => (Gnat.Sockets.Receive_Timeout, Timeout => Receive_Timeout));
--Bind Address
Address.Addr := Inet_Addr(DEFINE_IP_ADDR.IP_BOARD_ADDRESS);
Address.Port := Port_Type(DEST_UDP_PORT);
Bind_Socket (Socket, Address);
Channel := Stream (Socket, Address);
-- Receive Socket
Ada.Streams.Read (Channel.All, Data, Offset);
-- Close socket
Free (Channel);
Close_Socket (Socket);
WAIT_RESULT := 1;
MESSAGE_SIZE := Integer(Offset);
MESSAGE_ADDRESS := Data'Address;
Finalize;
exception when E : others =>
Ada.Text_IO.Put_Line
(Exception_Name (E) & ": " & Exception_Message (E));
WAIT_RESULT := 0;
Free (Channel);
Close_Socket (Socket);
Finalize;
end RECEIVE_DATA ;
答案 0 :(得分:0)
进行以下更改
Address.Addr := Inaddr_Any;
这是您收到的地址:不是您的地址。发送时设置地址,接收时设置地址