简体   繁体   English

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

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

I'm trying to connect my Ada application with node.js server using sockets. 我正在尝试使用套接字将我的Ada应用程序与node.js服务器连接。 I've already managed to send data from Ada to node.js but still have problems with receiving datas. 我已经设法将数据从Ada发送到node.js,但是在接收数据时仍然遇到问题。

Here's my Ada task: 这是我的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;

this is my node.js server: 这是我的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);

My console output for node.js server: 我的node.js服务器控制台输出:

connected
test1
0.00 //value of Message'Img

There's no output in Ada's task. Ada的任务没有任何输出。 It seems like loop in task has stopped and waits for message from node.js. 任务中的循环似乎已停止,正在等待来自node.js的消息。 Without the line Put_Line("Input " & String'Input(Channel)); 没有一行Put_Line("Input " & String'Input(Channel)); everything works fine (except of course getting messages from node.js). 一切正常(当然要从node.js获取消息除外)。 Thanks for help! 感谢帮助!

Your problem is that your server isn't sending correctly formatted data. 您的问题是服务器未发送正确格式的数据。 13.13.2(26/3) in the reference manual specifies that String'Input first reads the bounds of the string, and then its contents. 参考手册中的13.13.2(26/3)指定String'Input首先读取字符串的边界,然后读取其内容。

You probably want to send using String'Write and receive using Character'Read (unless you have a well-defined message length). 您可能希望使用String'Write发送并使用Character'Read接收(除非您有明确定义的消息长度)。

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

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