gpt4 book ai didi

sockets - 流读取阻止UDP GNAT

转载 作者:行者123 更新时间:2023-12-03 11:57:52 25 4
gpt4 key购买 nike

我必须从C++客户端接收UDP数据报。客户端可以正常工作,并将数据报发送到我在其中编译此代码的ip。数据报的大小为800Bytes。当我发送数据报时,读取过程被阻止。我已经根据以下示例创建了此代码:https://en.wikibooks.org/wiki/Ada_Programming/Libraries/GNAT.Sockets_examples,并且在我的主机上运行良好。任何的想法?

procedure RECEIVE_DATA (
DEST_UDP_PORT : In WORD_TYPE;
SRC_UDP_PORT : In WORD_TYPE;
WAIT_TIME : In DURATION;
MESSAGE_ADDRESS : Out System.Address;
WAIT_RESULT : Out Boolean
) is

task Receive is
entry Start;
end Receive;

task body Receive 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 .. 800);
begin
accept Start;
Initialize;
WAIT_RESULT := False;
Create_Socket (Socket, Family_Inet, Socket_Datagram);
Set_Socket_Option (Socket => Socket,
Option => (Gnat.Sockets.Receive_Timeout, Timeout => Receive_Timeout));

Address.Addr := Inet_Addr(DEST_UDP_IP);
Address.Port := Port_Type(DEST_UDP_PORT);
Bind_Socket (Socket, Address);

Address.Addr := Inet_Addr(SRC_UDP_IP);
Address.Port := Port_Type(SRC_UDP_PORT);
Channel := Stream (Socket, Address);

loop
Ada.Streams.Read (Channel.All, Data, Offset);
exit when Offset = 0;
end loop;
Free (Channel);
Close_Socket (Socket);
WAIT_RESULT := True;
MESSAGE_ADDRESS := Data'Address;

exception when E : others =>
Ada.Text_IO.Put_Line
(Exception_Name (E) & ": " & Exception_Message (E));
WAIT_RESULT := False;
Free (Channel);
Close_Socket (Socket);
end Receive;

begin
Initialize (Process_Blocking_IO => False);
Receive.Start;
Finalize;
end RECEIVE_DATA;

最佳答案

您是否尝试过使用GNAT.Sockets.Receive_Socket?
流与TCP一起使用,您不能流式传输数据报。

例子:

客户:

with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure UDP_Client is
use GNAT.Sockets;
Address : Sock_Addr_Type;
Socket : Socket_Type;
Data : constant Ada.Streams.Stream_Element_Array (1 .. 512) := (others => 42);
Last : Ada.Streams.Stream_Element_Offset;
begin
Address.Port := 50001;
Address.Addr := Inet_Addr ("127.0.0.1");
Create_Socket (Socket, Family_Inet, Socket_Datagram);
Send_Socket (Socket, Data, Last, Address);
Ada.Text_IO.Put_Line ("last :" & Last'Img);
end UDP_Client;

服务器 :
with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure UDP_Server is
use GNAT.Sockets;
Server : Socket_Type;
Address, From : Sock_Addr_Type;
Data : Ada.Streams.Stream_Element_Array (1 .. 512);
Last : Ada.Streams.Stream_Element_Offset;
Watchdog : Natural := 0;
begin
Create_Socket (Server, Family_Inet, Socket_Datagram);
Set_Socket_Option
(Server,
Socket_Level,
(Reuse_Address, True));
Set_Socket_Option
(Server,
Socket_Level,
(Receive_Timeout,
Timeout => 1.0));
Address.Addr := Any_Inet_Addr;
Address.Port := 50001;
Bind_Socket (Server, Address);
loop
begin
GNAT.Sockets.Receive_Socket (Server, Data, Last, From);
Ada.Text_IO.Put_Line ("last : " & Last'Img);
Ada.Text_IO.Put_Line ("from : " & Image (From.Addr));
exception
when Socket_Error =>
Watchdog := Watchdog + 1;
exit when Watchdog = 10;
end;
end loop;
end UDP_Server;

关于sockets - 流读取阻止UDP GNAT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40760503/

25 4 0
文章推荐: javascript - 如何在 Google Analytics 中监控外部小部件
文章推荐: architecture - 使用消息传递的微服务和SOA
文章推荐: javascript - 类型错误 : Object # has no method 'Schema'