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-Thread: 103376,7db5fb0599fd4b76 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII Path: g2news1.google.com!news1.google.com!proxad.net!teaser.fr!titine.scrogneugneu.org!jaco From: Eric Jacoboni Newsgroups: comp.lang.ada Subject: Re: TCP/IP Sockets with GNAT.Sockets Date: Mon, 02 May 2005 07:58:06 +0200 Organization: Rogntudju & Associates Message-ID: <020520050758066354%jaco@neottia.net> References: <1115001752.291144.218410@z14g2000cwz.googlegroups.com> NNTP-Posting-Host: 192.168.2.3 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: titine.scrogneugneu.org 1115013624 11523 192.168.2.3 (2 May 2005 06:00:24 GMT) X-Complaints-To: usenet@titine.scrogneugneu.org NNTP-Posting-Date: Mon, 2 May 2005 06:00:24 +0000 (UTC) User-Agent: Thoth/1.7.2 (Carbon/OS X) Xref: g2news1.google.com comp.lang.ada:10864 Date: 2005-05-02T07:58:06+02:00 List-Id: In article <1115001752.291144.218410@z14g2000cwz.googlegroups.com>, fabio de francesco wrote: > I hadn't too many problems with exchanging messages and everything > works. The problem is that I was only able to use Streams for > communicating between them and I can't understand at all how to use > Receive_Socket() and Send_Socket() because they use > "Stream_Element_Array" and "Stream_Element _Offset" in place of > Strings. I know that by using Streams they can't communicate with C > programs and I've read that Streams are everything but efficient. Streams are ok if you manage both the server and the client program. Otherwise, for talking to a server already written, send and receive are ok. > (2) The following is an extract from the client program. It compiles > and works as it is. May you please explain how to modify it in order to > use the above-mentioned functions in place of Input() and Output()? If > the problem can be solved by copying Strings to Stream_Element_Array > and back, how can I do that? Here is a simple test code that use send/receive to talk to a POP3 server : with Gnat.Sockets; use Gnat.Sockets; with Ada.Text_Io; use Ada.Text_Io; with Ada.Exceptions; use Ada.Exceptions; with Ada.Streams; use Ada.Streams; with Ada.Command_Line; use Ada.Command_Line; procedure GetPop3 is -- Constantes globales BUFSIZ : constant := 256; PORT_DEFAUT : constant Port_Type := 110; Protocol_Error : exception; -- Wrapper pour Send_Socket procedure Send(Sock : in Socket_Type; S : in String) is subtype SEA is Stream_Element_Array(1..S'Length); My_SEA : SEA; for My_SEA'Address use S(S'First)'Address; Trash : Stream_Element_Offset; begin Send_Socket(Sock, My_Sea, Trash); end Send; -- Wrapper pour Receive_Socket procedure Receive(Sock : in Socket_Type; S : out String; Taille : out Natural) is subtype SEA is Stream_Element_Array(1..S'Length); My_SEA : SEA; for My_SEA'Address use S(S'First)'Address; Trash : Stream_Element_Offset; begin Receive_Socket(Sock, My_Sea, Trash); Taille := Natural(Trash); end Receive; function Get_Reponse (Sock : Socket_Type) return String is Tampon : String(1..BUFSIZ); Dernier : Natural; begin Receive(Sock, Tampon, Dernier); return Tampon(1..Dernier); end Get_Reponse ; procedure Is_Ok (Reponse : in String) is begin if Reponse(1..3) /= "+OK" then raise Protocol_Error; end if; end Is_Ok; procedure Connexion (Sock : out Socket_Type; Serveur, User, Pass : in String; Port : in Port_Type := PORT_DEFAUT) is Adresse : Sock_Addr_Type; begin -- Construction de l'adresse Adresse.Addr := Addresses(Get_Host_By_Name (Serveur)); Adresse.Port := PORT_DEFAUT; -- Creation et connexion de la socket � l'adresse Initialize; Create_Socket(Sock); Connect_Socket(Sock, Adresse); Is_Ok(Get_Reponse(Sock)); Send(Sock, "user " & User & ASCII.CR & ASCII.LF); Is_Ok(Get_Reponse(Sock)); Send(Sock, "pass " & Pass & ASCII.CR & ASCII.LF); Is_Ok(Get_Reponse(Sock)); end Connexion; procedure Commande (Sock : in Socket_Type; Cde : in String) is begin Send(Sock, Cde & ASCII.CR & ASCII.LF); end Commande; Sock : Socket_Type; begin -- Prog principal if Argument_Count /= 3 then Put_Line("Usage: " & Command_Name & " "); else begin Connexion(Sock, Argument(1), Argument(2), Argument(3)); Put_Line("Connexion ok"); Commande(Sock, "stat"); Put_Line(Get_Reponse(Sock)); Close_Socket(Sock); exception when Protocol_Error => Put_Line(Current_Error, "Requete incorrecte"); when E: others => Put_Line(Exception_Name(E) & ": " & Exception_Message(E)); end; end if; end GetPop3; Hope this helps, -- jaco