* TCP Server & Client
@ 2018-03-25 4:04 Andrew Shvets
2018-03-25 8:22 ` Simon Wright
0 siblings, 1 reply; 3+ messages in thread
From: Andrew Shvets @ 2018-03-25 4:04 UTC (permalink / raw)
Hello, I'm trying to write a small client/server example:
This is the client:
==============================================================================
with Ada.Streams;
with Ada.Text_IO;
with GNAT.Sockets;
procedure TCP_Client is
Address : GNAT.Sockets.Sock_Addr_Type;
Socket : GNAT.Sockets.Socket_Type;
Data : constant Ada.Streams.Stream_Element_Array(1 .. 512) := (others => 42);
Last : Ada.Streams.Stream_Element_Offset;
begin
GNAT.Sockets.Initialize;
Address.Port := 50001;
Address.Addr := GNAT.Sockets.Inet_Addr("127.0.0.1");
Ada.Text_IO.Put_Line("Hello 1");
GNAT.Sockets.Create_Socket(Socket, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
Ada.Text_IO.Put_Line("Hello 2");
GNAT.Sockets.Set_Socket_Option(Socket, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
Ada.Text_IO.Put_Line("Hello 3");
GNAT.Sockets.Send_Socket(Socket, Data, Last, Address);
Ada.Text_IO.Put_Line("last :" & Last'Img);
GNAT.Sockets.Finalize;
end TCP_Client;
==============================================================================
This is the server:
==============================================================================
with Ada.Streams;
with Ada.Text_IO;
with GNAT.Sockets;
procedure TCP_Server is
Server : GNAT.Sockets.Socket_Type;
Sock : GNAT.Sockets.Socket_Type;
Address : GNAT.Sockets.Sock_Addr_Type;
From : GNAT.Sockets.Sock_Addr_Type;
Data : Ada.Streams.Stream_Element_Array(1 .. 512);
Last : Ada.Streams.Stream_Element_Offset;
Watchdog : Natural := 0;
begin
GNAT.Sockets.Initialize;
GNAT.Sockets.Create_Socket(Server, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
GNAT.Sockets.Set_Socket_Option(Server, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
Address.Addr := GNAT.Sockets.Any_Inet_Addr;
Address.Port := 50001;
GNAT.Sockets.Bind_Socket(Server, Address);
loop
begin
GNAT.Sockets.Listen_Socket(Server);
GNAT.Sockets.Accept_Socket(Server, Sock, Address);
GNAT.Sockets.Receive_Socket(Server, Data, Last, From);
Ada.Text_IO.Put_Line("last : " & Last'Img);
Ada.Text_IO.Put_Line("from : " & GNAT.Sockets.Image(From.Addr));
exception
when GNAT.Sockets.Socket_Error =>
Ada.Text_IO.Put_Line("ERROR: Socket error caught.");
end;
end loop;
end TCP_Server;
==============================================================================
When I run the client, this is what I see:
Hello 1
Hello 2
Hello 3
raised GNAT.SOCKETS.SOCKET_ERROR : [32] Broken pipe
Why is this happening? If possible, I'd like to use the Send_Socket method in the client. Thanks.
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: TCP Server & Client
2018-03-25 4:04 TCP Server & Client Andrew Shvets
@ 2018-03-25 8:22 ` Simon Wright
2018-03-25 19:17 ` Andrew Shvets
0 siblings, 1 reply; 3+ messages in thread
From: Simon Wright @ 2018-03-25 8:22 UTC (permalink / raw)
Andrew Shvets <andrew.shvets@gmail.com> writes:
> procedure TCP_Client is
[...]
> GNAT.Sockets.Create_Socket(Socket, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
> GNAT.Sockets.Set_Socket_Option(Socket, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
> GNAT.Sockets.Send_Socket(Socket, Data, Last, Address);
If you look at the spec of GNAT.Sockets for a long time you will see
that there are 3 versions of Send_Socket, only 2 of which I would expect
to use, and you've chosen the wrong one (the last, datagram,
version). You need the second:
procedure Send_Socket
(Socket : Socket_Type;
Item : Ada.Streams.Stream_Element_Array;
Last : out Ada.Streams.Stream_Element_Offset;
Flags : Request_Flag_Type := No_Request_Flag);
More importantly, you've left out the Connect_Socket call (which is what
the server Accept_Socket call is waiting for).
Check out the commentary at the beginning of g-socket.ads, look for
'task body ping'.
Also, in TCP_Server, you should be calling Receive_Socket on Sock (the
new socket which is actually connected to TCP_Client).
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: TCP Server & Client
2018-03-25 8:22 ` Simon Wright
@ 2018-03-25 19:17 ` Andrew Shvets
0 siblings, 0 replies; 3+ messages in thread
From: Andrew Shvets @ 2018-03-25 19:17 UTC (permalink / raw)
On Sunday, March 25, 2018 at 4:22:32 AM UTC-4, Simon Wright wrote:
> Andrew Shvets <an....@gmail.com> writes:
>
> > procedure TCP_Client is
> [...]
> > GNAT.Sockets.Create_Socket(Socket, GNAT.Sockets.Family_Inet, GNAT.Sockets.Socket_Stream);
>
> > GNAT.Sockets.Set_Socket_Option(Socket, GNAT.Sockets.Socket_Level, (GNAT.Sockets.Reuse_Address, True));
>
> > GNAT.Sockets.Send_Socket(Socket, Data, Last, Address);
>
> If you look at the spec of GNAT.Sockets for a long time you will see
> that there are 3 versions of Send_Socket, only 2 of which I would expect
> to use, and you've chosen the wrong one (the last, datagram,
> version). You need the second:
>
> procedure Send_Socket
> (Socket : Socket_Type;
> Item : Ada.Streams.Stream_Element_Array;
> Last : out Ada.Streams.Stream_Element_Offset;
> Flags : Request_Flag_Type := No_Request_Flag);
>
> More importantly, you've left out the Connect_Socket call (which is what
> the server Accept_Socket call is waiting for).
>
> Check out the commentary at the beginning of g-socket.ads, look for
> 'task body ping'.
>
>
> Also, in TCP_Server, you should be calling Receive_Socket on Sock (the
> new socket which is actually connected to TCP_Client).
Thank you Simon. That did the trick.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2018-03-25 19:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-25 4:04 TCP Server & Client Andrew Shvets
2018-03-25 8:22 ` Simon Wright
2018-03-25 19:17 ` Andrew Shvets
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox