簡體   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