-1

I want to create a server-client connection where a client send a request packet to the server then return a value back to the user. I can get the server to read the incoming packet from the client, but when it write back to the client, the client is not receiving the packet.

I have the size of string in Client and Server matches to ensure the read is not waiting for more incoming packet.

Server

Buffer : Ada.Streams.Stream_Element_Array (1 .. 10);
Offset : Ada.Streams.Stream_Element_Offset;
...
GNAT.Sockets.Create_Socket (Socket => Receiver);
GNAT.Sockets.Set_Socket_Option
    (Socket => Receiver,
    Option => (Name    => GNAT.Sockets.Reuse_Address, Enabled => True));
GNAT.Sockets.Bind_Socket
    (Socket  => Receiver,
        Address => (Family => GNAT.Sockets.Family_Inet,
        Addr   => GNAT.Sockets.Inet_Addr ("127.0.0.1"),
        Port   => 12321));
GNAT.Sockets.Listen_Socket (Socket => Receiver);
GNAT.Sockets.Accept_Socket
    (Server  => Receiver,
     Socket  => Connection,
     Address => Client);
Channel := GNAT.Sockets.Stream (Connection);
Ada.Streams.Read(Stream => Channel.all,
                  Item => Buffer,
                  Last => Offset);
for J in 1..Offset loop
    Ada.Text_IO.Put_Line(Character'Val(Integer (Buffer (J)))'Img);
end loop;
String'Write(GNAT.Sockets.Stream (Connection), "1234567890");
GNAT.Sockets.Close_Socket (Connection);

Client

input : String(1..10);
output : String(1..10);
...
Initialize;
Create_Socket  (Socket => Client);
Connect_Socket (Socket   => Client,
Server   => (Family => Family_Inet,
    Addr   => Inet_Addr ("127.0.0.1"),
    Port   => 12321));
String'Write (Stream (Client), Input);
String'Read (Stream (Client), output); --hanging right here
Close_Socket (Client);
user1713114
  • 19
  • 1
  • 1
  • 6

1 Answers1

0

For some reason, the client is getting the message now. The code above works as intended

user1713114
  • 19
  • 1
  • 1
  • 6
  • Just speculating : after you fixed an earlier problem, it may be that the socket connection wasn't instantly dropped when the faulty process exited; when it became available a minute or so later, your code "suddenly" started working. –  Jun 07 '18 at 10:33