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 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,8183b99d3c85c57b,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-11-04 13:13:36 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.mathworks.com!wn13feed!wn11feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi_feed4!attbi.com!attbi_s51.POSTED!not-for-mail From: Freejack Subject: Handling Exceptions? Newsgroups: comp.lang.ada Message-ID: User-Agent: Pan/0.11.4 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Comment-To: ALL NNTP-Posting-Host: 12.245.85.50 X-Complaints-To: abuse@comcast.net X-Trace: attbi_s51 1067980415 12.245.85.50 (Tue, 04 Nov 2003 21:13:35 GMT) NNTP-Posting-Date: Tue, 04 Nov 2003 21:13:35 GMT Organization: Comcast Online Date: Tue, 04 Nov 2003 21:13:35 GMT Xref: archiver1.google.com comp.lang.ada:2059 Date: 2003-11-04T21:13:35+00:00 List-Id: I've recently switched back to using Adasockets for my programming needs. I never bothered to catch exceptions before, since none of my apps were really that important. So, to get a little practice I slung together the following program(in about 5 minutes.) using Adasockets. The problem is that the compiler will let me use the exceptions declared in the first declaritive part but not the second declaritive part. Any pointers would be appreciated. Freejack. with Sockets; with Sockets.Naming; with Ada.Text_IO; with Ada.Numerics.Discrete_Random; procedure newconn is -- Or "Learning to use exceptions the right way." -- package TIO renames Ada.Text_IO; package SockNames renames Sockets.Naming; NameQuery : String := "www.yahoo.com"; -- Line Filler -- HostIP : SockNames.Address; HostImage : String(1..14); LocalHost : String := "127.0.0.1"; LocalIP : String := ""; begin -- This code left over from an cut+paste operation -- -- And it still works. -- HostIP := SockNames.Address_Of(NameQuery); HostImage := SockNames.Image(HostIP); TIO.Put_Line(HostImage); ----------------------------------------------------- declare Site: Sockets.Socket_FD; use Sockets; begin Socket(Site, AF_INET, SOCK_STREAM); Connect(Site, LocalHost, 9); -- Discard service -- exception when Connection_Refused => TIO.Put_Line("Connection Refused."); when others => TIO.Put_Line("Something else went wrong"); Sockets.Shutdown(Site, Both); end; declare Echo : Sockets.Socket_FD; EchoString : String(1..10); EchoResponse : String(1..10); -- Could get less. Wont get more than 10 -- use Sockets; -- This may look a little fruity, but I just wanna see if it'll work. -- package RandChar is new Ada.Numerics.Discrete_Random(Character); use RandChar; G : Generator; begin Socket(Echo, AF_INET, SOCK_STREAM); Connect(Echo, LocalIP, 7); exception when Connection_Refused => TIO.Put_Line("Connection Refused"); exit; when others => Sockets.Shutdown(Echo, Both); TIO.Put_Line("There was a problem connecting to Echo"); exit; -- Loop a bunch of times and send a bunch of shit to Echo. -- -- And get a bunch of shit back. -- EchoString := "1234567890"; Reset(G); for X in 1..EchoString'Length loop -- First we send the String. -- Put(Echo, EchoString); exception when Connection_Closed => TIO.Put_Line("Connection Closed Prematurely"); TIO.Put_Line("Terminating Application."); Shutdown(Echo, Both); exit; -- This should break us out of the loop? -- -- Then we get it back. -- EchoResponse := Get_Line(Echo, 10); exception when Connection_Closed => TIO.Put_Line("Peer Closed Connection before sending whole line"); TIO.Put_Line("Got characters" & EchoResponse &"."); TIO.Put_Line("Terminating Application."); Shutdown(Echo, Both); exit; -- Now we modify our string a bit(or a byte, pun intended) -- EchoString(X) := Random(G); end loop; Shutdown(Echo, Both); end; end newconn;