分类:
2008-10-17 12:56:14
--file server.adb, this program receive the message from client
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use GNAT.Sockets;
procedure Server is
Address : Sock_Addr_Type;
Socket : Socket_Type;
Server : Socket_Type;
Channel : Stream_Access;
begin
-- Create socket and listen input from client
Initialize;
Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
--Address.Addr := Inet_Addr("172.0.0.1");
Address.Port := 2543;
Create_Socket (Server);
Bind_Socket (Server, Address);
Listen_Socket (Server);
Accept_Socket (Server, Socket, Address);
-- A client has been accepted, get the stream connected to the socket
Channel := Stream (Socket);
for K in 1 .. 4 loop
declare
Message : String := String'Input (Channel);
begin
Address := Get_Address (Channel);
Ada.Text_IO.Put_Line (Message & " from " & Image (Address));
Ada.Text_IO.Put_Line (Message);
end;
end loop;
Close_Socket (Server);
Close_Socket (Socket);
end Server; |
--file client.adb, this program send a message to server
with Ada.Text_IO; use Ada.Text_IO;
with GNAT.Sockets; use GNAT.Sockets;
procedure Client is
Address : Sock_Addr_Type;
Socket : Socket_Type;
Channel : Stream_Access;
i : Integer := 1;
Message : string := "hello world";
begin
Initialize;
Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
--Address.Addr := Inet_Addr("172.0.0.1");
Address.Port := 2543;
Create_Socket (Socket);
Connect_Socket (Socket, Address);
-- Connected, use a stream connected to the socket
Channel := Stream (Socket);
loop
Put_Line ("(client) O1 size " & Integer'Image (i));
String'Output (Channel, Message);
delay 3.0;
i := i + 1;
exit when i = 3;
end loop;
Close_Socket(Socket);
end Client; |
|
|