File: sock.adb

package info (click to toggle)
libaws 2.2dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,624 kB
  • ctags: 1,173
  • sloc: ada: 61,829; ansic: 6,483; makefile: 1,282; xml: 196; sh: 119; java: 112; python: 66; sed: 40
file content (124 lines) | stat: -rw-r--r-- 2,897 bytes parent folder | download | duplicates (3)
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

--  Check heavy loaded server and how a client reuse a socket or not.
--  There is a problem here in the client API, after some time the program
--  fails because a socket is reused it seems to be closed but it can't be
--  opened. Anyway this problem will have to be sorted out...

with Ada.Exceptions;
with Ada.Text_IO;

with Sockets;
with Sockets.Thin;

procedure Sock is

   N_Client : constant := 20;
   N_Mess   : constant := 500;

   use Ada;

   task Server is
      entry Start;
   end Server;

   task type Client is
      entry Start;
   end Client;

   task body Server is
      K : Natural := 0;
      Accepting_Socket : Sockets.Socket_FD;
      New_Socket       : Sockets.Socket_FD;
   begin
      Sockets.Socket
        (Accepting_Socket,
         Sockets.AF_INET,
         Sockets.SOCK_STREAM);

      Sockets.Bind (Accepting_Socket, 8786);

      Sockets.Listen (Accepting_Socket, 5064);

      accept Start;

      loop
         begin
            Sockets.Accept_Socket (Accepting_Socket, New_Socket);

            Sockets.Put_Line (New_Socket, "STOP");

            declare
               Res : constant String := Sockets.Get_Line (New_Socket);
            begin
               if Res /= "END" then
                  Text_IO.Put_Line ("S] error, res is " & Res);
               end if;
            end;

            Sockets.Shutdown (New_Socket);

            K := K + 1;

            if K mod 100 = 0 then
               Text_IO.Put_Line ("S " & K'Img);
            end if;

            exit when K = N_Client * N_Mess;

         exception
            when E : others =>
               Text_IO.Put_Line ("S] " & Exceptions.Exception_Information (E));
         end;
      end loop;
   end Server;

   task body Client is
      Socket : Sockets.Socket_FD;
   begin
      accept Start;
      for K in 1 .. N_Mess loop
         begin
            Sockets.Socket (Socket, Sockets.AF_INET, Sockets.SOCK_STREAM);

            Sockets.Setsockopt
              (Socket,
               Sockets.SOL_SOCKET,
               Sockets.SO_REUSEADDR,
               1);

            Sockets.Connect (Socket, "localhost", 8786);

            declare
               Res : constant String := Sockets.Get_Line (Socket);
            begin
               if Res /= "STOP" then
                  Text_IO.Put_Line ("C] error, res is " & Res);
               end if;
            end;

            Sockets.Put_Line (Socket, "END");

            Sockets.Shutdown (Socket);
            delay 0.5;

         exception
            when E : others =>
               Text_IO.Put_Line ("C] " & Exceptions.Exception_Information (E));
         end;
      end loop;
   end Client;

   Clients : array (1 .. N_Client) of Client;

begin
   -- let some time for server to start..
   delay 2.0;

   Server.Start;

   -- then launch clients

   for K in Clients'Range loop
      Clients (K).Start;
   end loop;
end Sock;