comp.lang.ada
 help / color / mirror / Atom feed
From: anon@anon.org (anon)
Subject: Simple solution was Re: GNAT.Sockets. Blocking?
Date: Wed, 24 Jun 2009 18:44:26 GMT
Date: 2009-06-24T18:44:26+00:00	[thread overview]
Message-ID: <euu0m.86589$d36.86193@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: 158756da-21b7-4e2d-b288-7ba030f1f35c@25g2000yqb.googlegroups.com

Here a simple correction of your program that can be use to download any 
web site ( 1..8KB ) from any web server that does not request the browser 
information befoere sending data. Then it dumps the web page to the 
Standard_Output.

Please note: the change in the Http request packet.

  String'Write ( Channel, "GET /"                             & CRLF &
                          "Host: " & HOST                     & CRLF &
                          "Content-Type"                      & CRLF & 
                           CRLF ) ;

Also, I limited the initial web page to 1 .. 8192. Easy adaptable to 
increase buffer size before searching buffer for commands or data 
references. That is needed to resend the next request. 

--
-- Socket_Test
--
with Ada.Text_IO;
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.

  exception
    when GNAT.Sockets.Socket_Error => 
      Ada.Text_IO.Put_line ( "Error in connecting to Server" ) ;
  end Connect ;

  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 ;


   Buffer     : String ( 1..8196 ) ;
   Byte_Count : Natural ;

begin

  --  Initialize. No longer used in GNAT 2009
  -- Not really needed in others versions either
--  GNAT.Sockets.Initialize ;

  --  Connect.
  Connect ( Socket, Channel, "www.google.com", HTTP_PORT ) ;

  --  Send HTTP request.
  String'Write ( Channel, "GET /"                             & CRLF &
                          "Host: " & HOST                     & CRLF &
                          "Content-Type"                      & CRLF & 
                           CRLF ) ;

  -- Get Web page
  begin
    Byte_Count := 0 ;
    for Index in Buffer'Range loop
      Byte_Count := Byte_Count + 1 ;
      Character'Read ( Channel, Buffer ( Byte_Count ) ) ;
    end loop ;
  exception
    -- Normal exit if page is less than 8K
    when END_Error =>
       Byte_Count := Byte_Count - 1 ;
  end ;

  if Byte_Count > 0 then
    Ada.Text_IO.Put_Line ( Buffer ( 1 .. Byte_Count ) ) ;
  end if ;

  GNAT.Sockets.Close_Socket ( Socket ) ;

end Socket_Test ;


In <158756da-21b7-4e2d-b288-7ba030f1f35c@25g2000yqb.googlegroups.com>, "Jacob M. H. Smith" <jacob.m.h.smith@googlemail.com> writes:
>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;




  parent reply	other threads:[~2009-06-24 18:44 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-06-23 21:06 GNAT.Sockets. Blocking? Jacob M. H. Smith
2009-06-24  0:14 ` John B. Matthews
2009-06-24  4:55   ` Jacob M. H. Smith
2009-06-24 14:36     ` John B. Matthews
2009-06-24  7:47 ` Dmitry A. Kazakov
2009-06-24 13:32 ` anon
2009-06-24 14:03 ` anon
2009-06-24 15:14 ` Pascal Obry
2009-06-24 18:44 ` anon [this message]
2009-06-24 18:47 ` Per Sandberg
2009-06-24 19:51   ` Pascal Obry
2009-06-24 22:12   ` Jeffrey R. Carter
2009-06-25 17:30     ` Jacob M. H. Smith
replies disabled

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