aws_21.0.0_57fddf8f/workspace/sbreak.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
--  Test break socket operation by close.

with Ada.Exceptions;
with Ada.Streams;
with Ada.Text_IO;


with GNAT.Sockets;

procedure SBreak is

   use GNAT;

   Server  : Sockets.Socket_Type;
   Peer    : Sockets.Socket_Type;
   Address : Sockets.Sock_Addr_Type;
   Port    : constant := 8800;
   Count   : Natural  := 0;

   Indicator : array (0 .. 3) of Character := "|/-\";

   task Client is
      entry Start;
      entry Sent;
   end Client;

   task body Client is
      Socket  : Sockets.Socket_Type;
      Address : Sockets.Sock_Addr_Type;
      Last    : Ada.Streams.Stream_Element_Offset;
   begin
      accept Start;

      Address.Addr := Sockets.Inet_Addr ("127.0.0.1");
      Address.Port := Port;

      loop
         Sockets.Create_Socket (Socket);
         Sockets.Connect_Socket (Socket, Address);
         Sockets.Send_Socket (Socket, (1 .. 32 => 22), Last);

         accept Sent;

         Sockets.Shutdown_Socket (Peer);
         Sockets.Close_Socket (Peer);

         --  Just close, do not shutdown, because opposite
         --  peer is already closed.

         Sockets.Close_Socket (Socket);
      end loop;
   exception
      when E : others =>
         Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
   end Client;

begin
   Sockets.Initialize;

   Sockets.Create_Socket (Server);

   Sockets.Bind_Socket
     (Server,
      (Sockets.Family_Inet,
       Sockets.Any_Inet_Addr,
       Port));

   Sockets.Listen_Socket (Server);

   Client.Start;

   loop
      Sockets.Accept_Socket (Server, Peer, Address);

      declare
         use Ada.Streams;
         Data : Stream_Element_Array (1 .. 256);
         Last : Stream_Element_Offset;
      begin
         Client.Sent;

         loop
            Sockets.Receive_Socket (Peer, Data, Last);

            if Last < Data'First then
               Ada.Text_IO.Put_Line ("closed on opposite.");
            end if;
         end loop;
      exception
         when Sockets.Socket_Error =>
            Count := Count + 1;
            Ada.Text_IO.Put (Indicator (Count mod 4) & ASCII.CR);

            if Count rem 100 = 0 then
               Ada.Text_IO.Put (Count'Img & ASCII.CR);
            end if;
      end;
   end loop;

exception
   when E : others =>
      Ada.Text_IO.Put_Line (Ada.Exceptions.Exception_Information (E));
end SBreak;