comp.lang.ada
 help / color / mirror / Atom feed
From: Andrew Shvets <andrew.shvets@gmail.com>
Subject: TCP Server & Client
Date: Sat, 24 Mar 2018 21:04:37 -0700 (PDT)
Date: 2018-03-24T21:04:37-07:00	[thread overview]
Message-ID: <64541efa-41ea-464b-bb60-06719f3c56ad@googlegroups.com> (raw)

Hello, I'm trying to write a small client/server example:

This is the client:
==============================================================================
with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure TCP_Client is
  Address : GNAT.Sockets.Sock_Addr_Type;
  Socket : GNAT.Sockets.Socket_Type;
  Data : constant Ada.Streams.Stream_Element_Array(1 .. 512) := (others => 42);
  Last : Ada.Streams.Stream_Element_Offset;
begin
  GNAT.Sockets.Initialize;

  Address.Port := 50001;
  Address.Addr := GNAT.Sockets.Inet_Addr("127.0.0.1");
  Ada.Text_IO.Put_Line("Hello 1");
  GNAT.Sockets.Create_Socket(Socket, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
  Ada.Text_IO.Put_Line("Hello 2");
  GNAT.Sockets.Set_Socket_Option(Socket, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
  Ada.Text_IO.Put_Line("Hello 3");
  GNAT.Sockets.Send_Socket(Socket, Data, Last, Address);
  Ada.Text_IO.Put_Line("last :" & Last'Img);

  GNAT.Sockets.Finalize;
end TCP_Client;
==============================================================================

This is the server:
==============================================================================
with Ada.Streams;
with Ada.Text_IO;

with GNAT.Sockets;

procedure TCP_Server is
  Server : GNAT.Sockets.Socket_Type;
  Sock   : GNAT.Sockets.Socket_Type;
  Address : GNAT.Sockets.Sock_Addr_Type;
  From : GNAT.Sockets.Sock_Addr_Type;
  Data : Ada.Streams.Stream_Element_Array(1 .. 512);
  Last : Ada.Streams.Stream_Element_Offset;
  Watchdog : Natural := 0;
begin
  GNAT.Sockets.Initialize;

  GNAT.Sockets.Create_Socket(Server, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
  GNAT.Sockets.Set_Socket_Option(Server, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
  Address.Addr := GNAT.Sockets.Any_Inet_Addr;
  Address.Port := 50001;
  GNAT.Sockets.Bind_Socket(Server, Address);

  loop
    begin
      GNAT.Sockets.Listen_Socket(Server);
      GNAT.Sockets.Accept_Socket(Server, Sock, Address); 
      GNAT.Sockets.Receive_Socket(Server, Data, Last, From);
      Ada.Text_IO.Put_Line("last : " & Last'Img);
      Ada.Text_IO.Put_Line("from : " & GNAT.Sockets.Image(From.Addr));
    exception
      when GNAT.Sockets.Socket_Error =>
        Ada.Text_IO.Put_Line("ERROR: Socket error caught.");
    end;
  end loop;
end TCP_Server;
==============================================================================


When I run the client, this is what I see:
Hello 1
Hello 2
Hello 3

raised GNAT.SOCKETS.SOCKET_ERROR : [32] Broken pipe




Why is this happening?  If possible, I'd like to use the Send_Socket method in the client.  Thanks.


             reply	other threads:[~2018-03-25  4:04 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-03-25  4:04 Andrew Shvets [this message]
2018-03-25  8:22 ` TCP Server & Client Simon Wright
2018-03-25 19:17   ` Andrew Shvets
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox