简体   繁体   中英

How to connect node.js with ada using sockets?

I'm trying to connect my Ada application with node.js server using sockets. I've already managed to send data from Ada to node.js but still have problems with receiving datas.

Here's my Ada task:

  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:

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:

connected
test1
0.00 //value of Message'Img

There's no output in Ada's task. It seems like loop in task has stopped and waits for message from node.js. Without the line Put_Line("Input " & String'Input(Channel)); everything works fine (except of course getting messages from 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.

You probably want to send using String'Write and receive using Character'Read (unless you have a well-defined message length).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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