如何发送然后从Ada中的Client-Server接收数据包

时间:2018-06-05 11:34:55

标签: sockets tcp client-server ada gnat

我想创建一个服务器 - 客户端连接,其中客户端向服务器发送请求数据包,然后将值返回给用户。我可以让服务器从客户端读取传入的数据包,但是当它写回客户端时,客户端没有收到数据包。

我在客户端和服务器匹配中具有字符串大小,以确保读取不等待更多传入数据包。

服务器

Buffer : Ada.Streams.Stream_Element_Array (1 .. 10);
Offset : Ada.Streams.Stream_Element_Offset;
...
GNAT.Sockets.Create_Socket (Socket => Receiver);
GNAT.Sockets.Set_Socket_Option
    (Socket => Receiver,
    Option => (Name    => GNAT.Sockets.Reuse_Address, Enabled => True));
GNAT.Sockets.Bind_Socket
    (Socket  => Receiver,
        Address => (Family => GNAT.Sockets.Family_Inet,
        Addr   => GNAT.Sockets.Inet_Addr ("127.0.0.1"),
        Port   => 12321));
GNAT.Sockets.Listen_Socket (Socket => Receiver);
GNAT.Sockets.Accept_Socket
    (Server  => Receiver,
     Socket  => Connection,
     Address => Client);
Channel := GNAT.Sockets.Stream (Connection);
Ada.Streams.Read(Stream => Channel.all,
                  Item => Buffer,
                  Last => Offset);
for J in 1..Offset loop
    Ada.Text_IO.Put_Line(Character'Val(Integer (Buffer (J)))'Img);
end loop;
String'Write(GNAT.Sockets.Stream (Connection), "1234567890");
GNAT.Sockets.Close_Socket (Connection);

客户端

input : String(1..10);
output : String(1..10);
...
Initialize;
Create_Socket  (Socket => Client);
Connect_Socket (Socket   => Client,
Server   => (Family => Family_Inet,
    Addr   => Inet_Addr ("127.0.0.1"),
    Port   => 12321));
String'Write (Stream (Client), Input);
String'Read (Stream (Client), output); --hanging right here
Close_Socket (Client);

1 个答案:

答案 0 :(得分:0)

出于某种原因,客户端现在正在收到消息。上面的代码按预期工作