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=0.1 required=5.0 tests=BAYES_00,FREEMAIL_FROM, PDS_OTHER_BAD_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,8a39a73d0242ca42,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!25g2000yqb.googlegroups.com!not-for-mail From: "Jacob M. H. Smith" Newsgroups: comp.lang.ada Subject: GNAT.Sockets. Blocking? Date: Tue, 23 Jun 2009 14:06:35 -0700 (PDT) Organization: http://groups.google.com Message-ID: <158756da-21b7-4e2d-b288-7ba030f1f35c@25g2000yqb.googlegroups.com> NNTP-Posting-Host: 141.31.188.43 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Trace: posting.google.com 1245791197 9381 127.0.0.1 (23 Jun 2009 21:06:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 23 Jun 2009 21:06:37 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: 25g2000yqb.googlegroups.com; posting-host=141.31.188.43; posting-account=lGLE9AkAAABZOvwB3iPLYCKw3E6P3RUe User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_5_7; en-us) AppleWebKit/530.18 (KHTML, like Gecko) Version/4.0.1 Safari/530.18,gzip(gfe),gzip(gfe) Xref: g2news2.google.com comp.lang.ada:6580 Date: 2009-06-23T14:06:35-07:00 List-Id: Hi, I'm kind of new to socket programming and I need some help here. I wrote a simple little program that just fetches Google's index page. The posted code works, but only because I used a "Connection: close" header in the HTTP request. How would set up my code responsible for reading the data from the stream so that I can read the page while keeping the connection open? If I remove the header the program seems to stop when calling the Ada.Streams.Read function if there is less than BUFFER_SIZE data in the streams buffer. Can't I just read as much as is available? Thanks in advance. Here's the code: ------------------- with Ada.Text_IO; with Ada.Streams; with GNAT.Sockets; procedure Socket_Test is procedure Connect ( Socket : out GNAT.Sockets.Socket_Type; Channel : out GNAT.Sockets.Stream_Access; Host : in String; Port : in GNAT.Sockets.Port_Type) is HOST_ENTRY : constant GNAT.Sockets.Host_Entry_Type := GNAT.Sockets.Get_Host_By_Name (Host); Address : GNAT.Sockets.Sock_Addr_Type; begin -- Prepare the address structure. TODO: make a loop Address.Addr := GNAT.Sockets.Addresses (HOST_ENTRY, 1); Address.Port := Port; -- Set up the connection. GNAT.Sockets.Create_Socket (Socket); GNAT.Sockets.Connect_Socket (Socket, Address); Channel := GNAT.Sockets.Stream (Socket); -- TODO: error handling. end Connect; use type Ada.Streams.Stream_Element_Count; BUFFER_SIZE : constant Ada.Streams.Stream_Element_Count := 256; HTTP_PORT : constant GNAT.Sockets.Port_Type := 80; CRLF : constant String := (ASCII.CR, ASCII.LF); HOST : constant String := "www.google.com"; Socket : GNAT.Sockets.Socket_Type; Channel : GNAT.Sockets.Stream_Access; Byte_Count : Ada.Streams.Stream_Element_Count; Buffer : Ada.Streams.Stream_Element_Array (1 .. BUFFER_SIZE); begin -- Initialize. GNAT.Sockets.Initialize; -- Connect. Connect (Socket, Channel, "www.google.com", HTTP_PORT); -- Send HTTP request. String'Write (Channel, "GET / HTTP/1.1" & CRLF & "Host: " & HOST & CRLF & "Connection: close" & CRLF & CRLF); -- Read incoming data. loop Ada.Streams.Read (Channel.all, Buffer, Byte_Count); exit when Byte_Count = 0; for I in 1 .. Byte_Count loop Ada.Text_IO.Put (Character'Val (Buffer (I))); end loop; end loop; end Socket_Test;