2
我正嘗試使用套接字將我的Ada應用程序與node.js服務器連接起來。 我已經設法從Ada發送數據到node.js,但仍然有接收數據的問題。如何使用套接字將node.js與ada連接起來?
這裏是我的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獲得消息)。 感謝您的幫助!