michalk93 michalk93 - 7 months ago 27
Java Question

Send string data from Java/Erlang to Ada

I would like to create some interface in Java or Erlang, that process user commads, create string and send this string to Ada application. How I can do that in proper way?
I tried do this with sockets but it doesn't work?
It will be grateful, if you could provide me some sample code or some references.

Here is Ada task Server code:

task body ConfigServer is
Receiver : GNAT.Sockets.Socket_Type;
Connection : GNAT.Sockets.Socket_Type;
Client : GNAT.Sockets.Sock_Addr_Type;
Channel : GNAT.Sockets.Stream_Access;
S : Unbounded_String;
C : Character;
begin
Put_Line("SERVER CONFIG STARTED...");
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 => Addresses (Get_Host_By_Name (Host_Name), 1),
Port => 2080));
GNAT.Sockets.Listen_Socket (Socket => Receiver);
loop
GNAT.Sockets.Accept_Socket
(Server => Receiver,
Socket => Connection,
Address => Client);
Ada.Text_IO.Put_Line
("Client connected from " & GNAT.Sockets.Image (Client));
Channel := GNAT.Sockets.Stream (Connection);
begin
loop
C := Character'Input (Channel);
if C /= '.' then
Append(S, C);
else
Put_Line("Received: " & S);
Set_Unbounded_String(S, "");
end if;
end loop;
exception
when Ada.IO_Exceptions.End_Error =>
null;
end;
GNAT.Sockets.Close_Socket (Connection);
end loop;
end ConfigServer;


Erlang code:

-module (client).
-compile([export_all]).

sendMsg() ->
{ok, Socket} = gen_udp:open(2560),
gen_udp:send(Socket, {127,0,0,1}, 2080, "Hello"),
gen_udp:close(Socket).


Java code:

import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Socket socket = null;
try {
socket = new Socket("127.0.0.1", 2080);
} catch (IOException e) {
e.printStackTrace();
}

PrintStream output = null;

try {
output = new PrintStream(socket.getOutputStream());
} catch (IOException e) {
e.printStackTrace();
}

String input;
Scanner scanner = new Scanner(System.in);
do {
input = scanner.nextLine();
output.print(input);
}while (input.equalsIgnoreCase("EXIT"));


try {
output.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}


I think that's problem is that Java and Erlang working in virtual environemnt and I need some low level API to sockets. Am I right?

Answer

I’m on OS X, and I got this to start working by

(Ada)

GNAT.Sockets.Bind_Socket
  (Socket  => Receiver,
   Address => (Family => GNAT.Sockets.Family_Inet,
               Addr   => Addresses (Get_Host_By_Name (“localhost"), 1),
               Port   => 2080));

It turns out that, on OS X, Host_Name returns nidhoggr.local (my machines are named after dragon-like entities, and I was running short), which has one network address, 192.168.1.74. ”localhost”, on the other hand, does give you 127.0.0.1.

(Java)

do {
    input = scanner.nextLine();
    output.print(input);
}while (!input.equalsIgnoreCase("EXIT"));

(see the !)