comp.lang.ada
 help / color / mirror / Atom feed
From: "fabio de francesco" <fmdf@tiscali.it>
Subject: TCP/IP Sockets with GNAT.Sockets
Date: 1 May 2005 19:42:32 -0700
Date: 2005-05-01T19:42:32-07:00	[thread overview]
Message-ID: <1115001752.291144.218410@z14g2000cwz.googlegroups.com> (raw)

Hello,

Today is the first time I try to use sockets in Ada programs, even if I
have some practical knowledge of programming with TCP/IP sockets in C.
Because I didn't find any better tutorial on working with GNAT.Sockets
I am relying on comments in file g-socket.ads from GCC/GNAT sources
where there is not a complete and detailed coverage of the subject.

(1) Do you know any better tutorial?

Then I wrote two little programs in order to try the package. A server
waits for connections and a client sends a string and receive an echo
from the first one. The client can also shutdown the server by sending
a predefined string.

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.

(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?

-- file client.adb

with GNAT.Sockets, Ada.Text_IO,
Ada.Strings.Unbounded, Ada.Command_Line;
use GNAT.Sockets, Ada.Text_IO,
Ada.Strings.Unbounded, Ada.Command_Line;

procedure Client is

    Socket : Socket_Type;
    Address : Sock_Addr_Type;
    Channel : Stream_Access;
    Reply : Unbounded_String;

begin

    Initialize;
    Create_Socket( Socket );
    Address.Addr := Inet_Addr( "127.0.0.1" );
    Address.Port := 9876;
    Connect_Socket( Socket, Address );
    Channel := Stream( Socket );

    if Argument_Count > 0 then
        if Argument(1) = "kill" then
            Put_Line( "asking server to shutdown" );
            String'Output( Channel, "kill" );
        else
            Put_Line( "sending """ & Argument( 1 ) & """" );
            String'Output( Channel, Argument( 1 ) );
        end if;
        Reply := Unbounded_String'Input( Channel );
        Put_Line( "server echos """ & To_String( Reply ) & """" );
    end if;

    Free( Channel );
    Close_Socket( Socket );
    Finalize;

end Client;

-- end of client.adb

Thanks to all of you who will reply.

fabio de francesco




             reply	other threads:[~2005-05-02  2:42 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-02  2:42 fabio de francesco [this message]
2005-05-02  5:58 ` TCP/IP Sockets with GNAT.Sockets Eric Jacoboni
2005-05-02 12:11 ` Adrien Plisson
2005-05-02 14:55   ` fabio de francesco
2005-05-02 16:10     ` Adrien Plisson
2005-05-02 17:56       ` Eric Jacoboni
2005-05-02 18:30         ` Poul-Erik Andreasen
2005-05-02 19:10           ` Simon Wright
2005-05-03 13:00             ` Poul-Erik Andreasen
2005-05-03 21:48               ` Simon Wright
2005-05-04  8:01               ` Character'First, ASCII.NUL and others (Was: Re: TCP/IP Sockets with GNAT.Sockets) Adrien Plisson
2005-05-04 13:40                 ` Poul-Erik Andreasen
2005-05-02 20:37           ` TCP/IP Sockets with GNAT.Sockets fabio de francesco
2005-05-02 20:52             ` Adrien Plisson
2005-05-03 12:04               ` fabio de francesco
2005-05-03 12:22                 ` Adrien Plisson
2005-05-03 13:17             ` Poul-Erik Andreasen
2005-05-02 20:44         ` Adrien Plisson
2005-05-02 22:10           ` Eric Jacoboni
2005-05-02 23:42             ` tmoran
2005-05-02 19:39     ` Björn
2005-05-02 20:22       ` fabio de francesco
2005-05-09  4:03         ` Dave Thompson
replies disabled

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