From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM, WEIRD_QUOTING autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,8f6dc5bfebaf357f,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!f14g2000cwb.googlegroups.com!not-for-mail From: "fabio de francesco" Newsgroups: comp.lang.ada Subject: "broken pipe" while reading/writing stream-based sockets Date: 10 May 2005 16:02:59 -0700 Organization: http://groups.google.com Message-ID: <1115766179.505983.40960@f14g2000cwb.googlegroups.com> NNTP-Posting-Host: 80.181.51.89 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1115766184 6582 127.0.0.1 (10 May 2005 23:03:04 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 10 May 2005 23:03:04 +0000 (UTC) User-Agent: G2/0.2 Complaints-To: groups-abuse@google.com Injection-Info: f14g2000cwb.googlegroups.com; posting-host=80.181.51.89; posting-account=Lp02jQ0AAABMd3TAghNf0TM2YBZqD_JE Xref: g2news1.google.com comp.lang.ada:10988 Date: 2005-05-10T16:02:59-07:00 List-Id: Ciao, I'm still having problems while doing sockets read/write. I have a server that often crashes with error "Broken Pipe". The server works always with the same clients and many times it doesn't crash until something happens that I don't understand. This is the server code: -- file tcp_server.adb with GNAT.Sockets; use GNAT.Sockets; with Ada.Text_IO; use Ada.Text_IO; procedure TCP_Server is Socket : Socket_Type; Client : Socket_Type; Sock_Addr : Sock_Addr_Type; Cli_Addr : Sock_Addr_Type; L : constant Positive := 5; Channel : Stream_Access; S : String( 1 .. 80 ); begin Initialize; Create_Socket( Socket ); Set_Socket_Option( Socket, Socket_Level, ( Reuse_Address, True ) ); Sock_Addr.Addr := Inet_Addr( "127.0.0.1" ); Sock_Addr.Port := 9876; Bind_Socket( Socket, Sock_Addr ); Listen_Socket( Socket, L ); Put_Line( "Server in attesa di connessioni..." ); loop Accept_Socket( Socket, Client, Cli_Addr ); Channel := Stream( Client ); String'Read( Channel, S ); String'Write ( Channel, ( "Ricevuto " & S ) ); Put_Line( "Client ha inviato codice """ & S & """" ); Free( Channel ); Close_Socket( Client ); if S( 1 .. 4 ) = "kill" then Put_Line( "Sono stato ucciso" ); exit; end if; end loop; Close_Socket( Socket ); Finalize; end TCP_Server; And the following is client code: -- file tcp_client.adb with GNAT.Sockets; use GNAT.Sockets; with Ada.Text_IO; use Ada.Text_IO; with Ada.Strings; use Ada.Strings; with Ada.Strings.Fixed; use Ada.Strings.Fixed; with Ada.Command_Line; use Ada.Command_Line; procedure TCP_Client is Socket : Socket_Type; Address : Sock_Addr_Type; Channel : Stream_Access; S : String( 1 .. 80 ); begin if Argument_Count < 1 then return; end if; Initialize; Create_Socket( Socket ); Address.Addr := Addresses( Get_Host_By_Name( "localhost" ), 1 ); Address.Port := 9876; Connect_Socket( Socket, Address ); Channel := Stream( Socket ); Move( Argument( 1 ), S ); if S( 1 .. 4 ) = "kill" then Put_Line( "Sto ordinando a Server di fermarsi!" ); else Put_Line( "Sto inviando """ & Trim( S, Both ) & """" ); end if; String'Write( Channel, S ); String'Read( Channel, S ); Put_Line( "Server ha risposto """ & Trim( S, Both ) & """" ); Free( Channel ); Close_Socket( Socket ); Finalize; end TCP_Client; I usually test the server this way: $ ./tcp_client abc & ./tcp_client def & ./tcp_client xyz & ./tcp_client kill 1) Why does this server sometimes crash with "Broken Pipe"? 2) It seems that using Input/Output instead of Read/Write never causes "Broken Pipe". Why? Thanks to all of you who will reply, fabio de francesco