with Ada.Text_IO; use Ada.Text_IO; with Ada.Command_Line; with Interfaces.C; with Interfaces.C.Strings; procedure Client is use Interfaces.C; use Interfaces.C.Strings; subtype U_Long is Unsigned_Long; subtype U_Short is Unsigned_Short; subtype Ssize_T is Int; type Chars_Ptr_Ptr is access all Chars_Ptr; AF_INET : constant := 2; SOCK_STREAM : constant := 1; SOCK_DGRAM : constant := 2; INADDR_ANY : constant := 0; type In_Addr is record S_Addr : U_Long; end record; pragma Convention (C, In_Addr); type In_Addr_Ptr is access all In_Addr; type In_Addr_Ptr_Ptr is access all In_Addr_Ptr; type Sockaddr_In is record Sin_Family : Short; Sin_Port : U_Short; Sin_Addr : In_Addr; Sin_Zero : Char_Array(0..7); end record; pragma Convention (C, Sockaddr_In); type Hostent is record H_Name : Chars_Ptr; H_Aliases : Chars_Ptr_Ptr; H_Addrtype : Int; H_Length : Int; H_Addr_List : In_Addr_Ptr_Ptr; end record; pragma Convention (C, Hostent); type Hostent_Ptr is access all Hostent; type Sockaddr_In_Ptr is access all Sockaddr_In; type Int_Ptr is access all Int; function Socket(Domain, Socket_Type, Protocol : in Int) return Int; pragma Import (C, Socket, "socket"); function GetHostByName(Name : Chars_Ptr) return Hostent_Ptr; pragma Import (C, GetHostByName, "gethostbyname"); function SendTo(S : Int; Msg : Chars_Ptr; Len : Size_T; Flags : Int; Sa : Sockaddr_In_Ptr; Salen : Int ) return Ssize_t; pragma Import (C, SendTo, "sendto"); function Close(S : Int) return Int; pragma Import (C, Close, "close"); Sock : Int; Name : aliased Sockaddr_In; Result : Int; BufArray : Char_Array := "This is a test! "; Buf, HostName : Chars_Ptr; Socket_Error : exception; Hp : Hostent_Ptr; begin Put_Line("Starting client"); Sock := Socket(AF_INET, SOCK_DGRAM, 0); if Sock < 0 then Put_Line("Error opening datagram socket"); raise Socket_Error; end if; Put_Line("Getting host information"); HostName := New_String(Ada.Command_Line.Argument(1)); hp := GetHostByName(HostName); if Hp = null then Put_Line("Unknown Host"); raise Socket_Error; end if; Free(HostName); Put_Line("Setting up Socket Structures"); Name.Sin_Addr := Hp.H_Addr_List.all.all; Name.Sin_Port := U_Short'Value(Ada.Command_Line.Argument(2)); Put("Using Port : "); Put(U_Short'Image(Name.Sin_Port)); New_Line; Name.Sin_Family := AF_INET; Put_Line("Sending datagram"); Buf := New_Char_Array(BufArray); Result := SendTo(sock, Buf, StrLen(Buf), 0, Name'Access, Sockaddr_In'Size / CHAR_BIT); if Result < 0 then Put_Line("Error sending datagram message"); raise Socket_Error; end if; Result := Close(sock); end Client;