GNAT.Sockets.Receive_Socket vs. String'Input (Channel)?

Hello,

I am attempting to write an mpd frontend. I can connect to the daemon, open the socket, close it, etc.

When I want to receive the data, I can receive the initial connection message from the daemon with GNAT.Sockets.Receive_Socket, but when I try to go with the examples on Wikibooks, or from the example in the comments of g-socket.ads with streams, nothing happens. Here is an example of what I am doing:

-- Initialize sockets
GNAT.Sockets.Initialize;
	
-- Create the client socket
GNAT.Sockets.Create_Socket (Socket => Client);
	
-- Assign address and port to server address
Address.Addr := GNAT.Sockets.Inet_Addr (Image => Server_IP);
Address.Port := Server_Port;
	
-- Connect socket to server
GNAT.Sockets.Connect_Socket (Socket => Client, Server => Address);

-- Receive data from server
GNAT.Sockets.Receive_Socket (Socket => Client,
							 Item   => Data, 
                             Last   => Size);							 
								 
-- Concat buffer into string
for i in 1 .. Size loop
	Ada.Strings.Unbounded.Append 
        (Source   => Response, 
         New_Item => Character'Val (Data (i)));
end loop;
	
-- Print the response
Ada.Text_IO.Put_Line (Item => Ada.Strings.Unbounded.To_String 
                                  (Source => Response));
	
-- Close socket							 
GNAT.Sockets.Close_Socket (Socket => Client);

Now this works, and I get the response expected. If I attempt the following, I get nothing:

-- ... [same code until -- Connect socket to server] ...

-- Create channel for streaming data
Channel := GNAT.Sockets.Stream (Socket => Client);

Ada.Text_IO.Put_Line (String'Input (Channel));

Why is this?

I reckon I have not understood streams, or that I am completely missing something else.

Thank you all very much in advance,

/ vjalmr

Input is gonna look for the string meta data first (things like string length, first index, etc. I don’t recall what exactly is stored), so if your source isn’t sending it, then it might fail depending on what the first part of the string has in it, and even if it doesn’t fail, it will be wrong. If you aren’t sending string meta data, then you’ll need to use String’Read instead of String’Input

13.13.2 (26/3)

If T is an array type, S'Output first writes the bounds, 
and S'Input first reads the bounds. If T has discriminants 
without defaults, S'Output first writes the discriminants 
(using the Write attribute of the discriminant type for each), 
and S'Input first reads the discriminants (using the Read 
attribute of the discriminant type for each).