将字符串数据从 Java/Erlang 发送到 Ada
Send string data from Java/Erlang to Ada
我想在 Java 或 Erlang 中创建一些接口,用于处理用户命令、创建字符串并将该字符串发送到 Ada 应用程序。我怎样才能以正确的方式做到这一点?
我试过用套接字做这个但它不起作用?
如果您能提供一些示例代码或一些参考资料,将不胜感激。
这是 Ada 任务服务器代码:
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 代码:
-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代码:
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();
}
}
}
我认为问题在于 Java 和 Erlang 在虚拟环境中工作,我需要一些低级别的 API 到套接字。我说得对吗?
我在 OS X,我在
之前开始工作
(艾达)
GNAT.Sockets.Bind_Socket
(Socket => Receiver,
Address => (Family => GNAT.Sockets.Family_Inet,
Addr => Addresses (Get_Host_By_Name (“localhost"), 1),
Port => 2080));
事实证明,在 OS X 上,Host_Name
returns nidhoggr.local
(我的机器以类似龙的实体命名,而我 运行 短),它有一个网络地址,192.168.1.74
。 ”localhost”
,另一方面,确实给你 127.0.0.1
。
(Java)
do {
input = scanner.nextLine();
output.print(input);
}while (!input.equalsIgnoreCase("EXIT"));
(参见 !
)
我想在 Java 或 Erlang 中创建一些接口,用于处理用户命令、创建字符串并将该字符串发送到 Ada 应用程序。我怎样才能以正确的方式做到这一点? 我试过用套接字做这个但它不起作用? 如果您能提供一些示例代码或一些参考资料,将不胜感激。
这是 Ada 任务服务器代码:
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 代码:
-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代码:
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();
}
}
}
我认为问题在于 Java 和 Erlang 在虚拟环境中工作,我需要一些低级别的 API 到套接字。我说得对吗?
我在 OS X,我在
之前开始工作(艾达)
GNAT.Sockets.Bind_Socket
(Socket => Receiver,
Address => (Family => GNAT.Sockets.Family_Inet,
Addr => Addresses (Get_Host_By_Name (“localhost"), 1),
Port => 2080));
事实证明,在 OS X 上,Host_Name
returns nidhoggr.local
(我的机器以类似龙的实体命名,而我 运行 短),它有一个网络地址,192.168.1.74
。 ”localhost”
,另一方面,确实给你 127.0.0.1
。
(Java)
do {
input = scanner.nextLine();
output.print(input);
}while (!input.equalsIgnoreCase("EXIT"));
(参见 !
)