【问题标题】:How to connect node.js with ada using sockets?如何使用套接字将 node.js 与 ada 连接?
【发布时间】:2016-08-25 03:17:42
【问题描述】:

我正在尝试使用套接字将我的 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 获取消息除外)。 感谢您的帮助!

【问题讨论】:

    标签: javascript node.js sockets server ada


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-27
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2015-03-25
      • 2018-11-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多