comp.lang.ada
 help / color / mirror / Atom feed
From: "Alex R. Mosteo" <devnull@mailinator.com>
Subject: Re: Question about Streams and UDP sockets using GNAT.Sockets
Date: Tue, 18 Jul 2006 12:25:16 +0200
Date: 2006-07-18T12:25:16+02:00	[thread overview]
Message-ID: <4i3r3nF215adU1@individual.net> (raw)
In-Reply-To: 1fpj192j49rf4$.ma23qkoukku3.dlg@40tude.net

Dmitry A. Kazakov wrote:

> On 18 Jul 2006 02:41:06 -0700, lekktu@gmail.com wrote:
> 
>> I'm using GNAT GPL 2006 (20060522-34) on Windows XP.
>> 
>> I'm trying to broadcast an UDP packet, with the following test code:
>> 
>> ----------------------------------------------------------------------
>> with GNAT.Sockets; use GNAT.Sockets;
>> procedure Test is
>>    Local_Port  : constant Port_Type := 20769;
>>    Remote_Port : constant Port_Type := 20770;
>>    Address    : Sock_Addr_Type;
>>    Socket     : Socket_Type;
>>    Channel    : Stream_Access;
>>    Local_Host : String := "127.0.0.1";
>> begin
>>    Initialize;
>>    Create_Socket (Socket, Family_Inet, Socket_Datagram);
>>    Set_Socket_Option (Socket, Socket_Level, (Broadcast, True));
>>    Address.Addr := Inet_Addr (Local_Host);
>>    Address.Port := Local_Port;
>>    Bind_Socket (Socket, Address);
>>    Address.Addr := Broadcast_Inet_Addr;
>>    Address.Port := Remote_Port;
>>    Channel := Stream (Socket, Address);
>>    String'Write (Channel, "TEST");         -- sends "T", "E", "S", "T".
>>    Free (Channel);
>>    Close_Socket (Socket);
>>    Finalize;
>> end Test;
>> ----------------------------------------------------------------------
>> 
>> The trouble I'm having is not about sockets, but the streams vs.
>> sockets interaction. The above code does not send one UDP packet, but
>> four, one for each byte of the test message. What I'm doing wrong?
> 
> Don't use String'Write. GNAT implementation treats strings as array so
> characters don't coalesce. It is unexpected, but legal. You might wish to
> define your own type of the packet and implement a suitable write (and
> read) for it.

I've workarounded this in past versions of GNAT using a direct call to the
Write procedure in Ada.Streams, though you must use a Stream_Element_Array
instead of a String. Doing so your array is transferred in a single call
and packet. (Contrarily to using the 'Write attribute of
Stream_Element_Array, that will also make a call for every element, which
is very CPU expensive for large arrays).



  reply	other threads:[~2006-07-18 10:25 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-18  9:41 Question about Streams and UDP sockets using GNAT.Sockets lekktu
2006-07-18  9:55 ` Dmitry A. Kazakov
2006-07-18 10:25   ` Alex R. Mosteo [this message]
2006-07-18 11:02     ` lekktu
2006-07-18 14:25       ` Alex R. Mosteo
2006-07-18 14:58         ` lekktu
2006-07-18 20:52       ` Samuel Tardieu
2006-07-18 20:58     ` Randy Brukardt
2006-07-19 10:36       ` Alex R. Mosteo
2006-07-19 18:50       ` Simon Wright
2006-07-18 19:07   ` Jeffrey R. Carter
2006-07-18 20:13     ` Dmitry A. Kazakov
2006-07-19  0:32       ` Jeffrey R. Carter
2006-07-19  8:12         ` Dmitry A. Kazakov
2006-07-19 18:59 ` Simon Wright
2006-07-19 20:54   ` lekktu
replies disabled

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