gpt4 book ai didi

javascript - 如何使用套接字将 node.js 与 ada 连接起来?

转载 作者:搜寻专家 更新时间:2023-11-01 00:30:29 24 4
gpt4 key购买 nike

我正在尝试使用套接字将我的 Ada 应用程序连接到 node.js 服务器。我已经设法将数据从 Ada 发送到 node.js,但在接收数据方面仍然存在问题。

这是我的 Ada 任务:

  task body Send_Task is

use Ada.Numerics.Float_Random;
Next : Ada.Real_Time.Time;
Period : constant Ada.Real_Time.Time_Span := Ada.Real_Time.Milliseconds(5000);
Interval : constant Ada.Real_Time.Time_Span := Ada.Real_Time.Milliseconds(30);
Gen : Generator;

Address : Sock_Addr_Type;
Socket : Socket_Type;
Channel : Stream_Access;

begin
Reset(Gen);
Next := Ada.Real_Time.Clock + Interval;
Address.Addr := Addresses (Get_Host_By_Name (Host_Name), 1);
Address.Port := 9000;
Create_Socket (Socket);
Set_Socket_Option (Socket, Socket_Level, (Reuse_Address, True));
Connect_Socket (Socket, Address);
loop
delay until Next;
Channel := Stream (Socket);
String'Output(Channel, Message'Img);

Put_Line("Input " & String'Input(Channel));
Next := Next + Period;
end loop;

exception
when E:others => Put_Line("Error: Task Errors");
Put_Line(Exception_Name (E) & ": " & Exception_Message (E));
end Send_Task;

这是我的 node.js 服务器:

require('net').createServer(function (socket) {
console.log("connected");

socket.on('data', function (data) {
console.log(data.toString());

});
socket.on('connection', function(){
console.log("test2");
socket.write("900.0");
});
console.log("test1");
socket.write("900.0");

}).listen(9000);

我的 node.js 服务器的控制台输出:

connected
test1
0.00 //value of Message'Img

Ada 的任务没有输出。似乎任务中的循环已停止并等待来自 node.js 的消息。没有行 Put_Line("Input "& String'Input(Channel)); 一切正常(当然除了从 node.js 获取消息)。感谢您的帮助!

最佳答案

您的问题是您的服务器未发送格式正确的数据。 13.13.2(26/3)在引用手册中指定 String'Input 首先读取字符串的边界,然后是其内容。

您可能希望使用 String'Write 发送并使用 Character'Read 接收(除非您有明确定义的消息长度)。

关于javascript - 如何使用套接字将 node.js 与 ada 连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36947187/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com