繁体   English   中英

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

[英]How to connect node.js with ada using sockets?

我正在尝试使用套接字将我的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接收(除非您有明确定义的消息长度)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM