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

How to connect node.js with ada using sockets?

本文关键字:ada 连接 起来 js node 何使用 套接字      更新时间:2023-09-26

我正在尝试使用套接字将我的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

阿达的任务没有输出。任务中的循环似乎已经停止,等待来自node.js的消息。如果没有Put_Line("Input " & String'Input(Channel));行,一切都很好(当然除了从node.js获取消息)。谢谢你的帮助!

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

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