comp.lang.ada
 help / color / mirror / Atom feed
From: Adrien Plisson <aplisson-news@stochastique.net>
Subject: Re: TCP/IP Sockets with GNAT.Sockets
Date: Mon, 02 May 2005 22:44:22 +0200
Date: 2005-05-02T22:44:22+02:00	[thread overview]
Message-ID: <42769099$0$24172$ba620e4c@news.skynet.be> (raw)
In-Reply-To: <020520051956181888%jaco@neottia.net>

Eric Jacoboni wrote:
>>(i'm sorry i'm gonna be a bit rude) beuark !
> 
> Agree. It's not the sort of things i expected to write with Ada... But,
> that's the only code that fits my needs.

i would rather say: that's the only way you found to fit your need. 
remember the old Perl adage: there is more than one way to do it !

>>this is implementation dependant:
>>- what if Character'Size /= Stream_Element'Size ?
>>- what if the memory organization of Stream_Element_Array does not 
>>match the one for a String ?
> 
> For the second point, Stream_Element_Array is defined as an array of
> Stream_Element, so i suppose it match the memory organization of a
> String, isn't it ?

Stream_Element_Array matches the organization of an array, BUT:
- Stream_Element is implementation defined (see ARM95 13.13.1), and this 
does not guarantee Stream_Element'Size = Character'Size (unless i missed 
something). strangely, it is 8 bits wide on every platform i used, but i 
would not bet on it for "exotic" 36 bit platforms for example...

- Stream_Element_Array may, for efficiency reason, use a different 
convention than String, which would render them incompatible.
(pragma Convention( C, Stream_Element_Array) for example)

- the original code was:
for SEA'Address use S(S'First)'Address;
which places SEA at the address of the first component of S. this is 
different from:
for SEA'Address use S'Address;
however, i'm not fond of overlay, so don't ask me to explain why it is 
this way...

> Have you tried this code with a server not Ada aware? Have you tried to
> exchange messages with, say, a POP3 server? (to send and to read) I've
> tried to use Streams for that, but i never succeeded to make it work...
> If a cleaner solution exists, why the hell cannot we find _any_
> example?

this is my Ada client:
=========================================================================
with Ada.Text_IO,
      GNAT.Sockets;
procedure Main is
     use GNAT.Sockets;

     Address  : Sock_Addr_Type := (Family_Inet, Inet_Addr( "127.0.0.1" 
), 5555);
     Socket   : Socket_Type;
     Channel  : Stream_Access;

     Data : String := "hello";

begin
     Initialize;
     Create_Socket( Socket );
     Connect_Socket( Socket, Address );
     Channel := Stream( Socket );

     String'Write( Channel, Data );
     String'Read( Channel, Data );
     Ada.Text_IO.Put_Line( Data );

     Close_Socket( Socket );
     Finalize;
end Main;
=========================================================================

this is my Python server:
=========================================================================
import socket

def main():
     server = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
     server.bind( ('127.0.0.1', 5555) )
     server.listen( 5 )

     client, addr = server.accept()

     data = ''
     while len( data ) != 5:
         data += client.recv( 5 )
     print data
     client.send( "world" )

     server.close()

if __name__ == '__main__':
     main()
=========================================================================

they work together like a charm. ok, ok, ok, i admit it i cheated !!
the ada client sends a 5 character long string and waits for a 5 
character long string, but the server sends a 5 character long string... 
if the server sent "grabuuuu !", the client would only get "grabu".

as i already pointed to you on fr.comp.lang.ada, what you get from a 
read is dependant on the PROTOCOL !

all the problem encountered when using streams with sockets are protocol 
problems: people sends some data on the socket and expect something else 
at the other end. in my example, i used a simple protocol: the client 
send 5 characters on the socket then the server sends back 5 other 
characters. the protocol is the same on both sides.

when trying to sends  strings over the network, you have to take into 
account that no 2 languages uses the same representation for strings: C 
strings are null-terminated, Ada strings are arrays of character with a 
lower and an upper bound, pascal strings are array of chars with only an 
upper bound... if you just send c strings and expect ada strings, you 
only get garbage !

this is the point of knowing ARM95 13.13.2: it allows you to know HOW 
data are pushed onto streams, thus allowing you to know how to read them 
back. eventually, you can write your own 'Read and 'Write procedures so 
that you are sure of what you have on the stream... ultimately, you can 
combine all the techniques to define powerful language-independant 
protocols !

-- 
rien




  parent reply	other threads:[~2005-05-02 20:44 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-05-02  2:42 TCP/IP Sockets with GNAT.Sockets fabio de francesco
2005-05-02  5:58 ` 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 [this message]
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