简体   繁体   中英

How to send true via socket connections in node.js

I have some piece of code in java to get the location from GPS. After writing true to output stream in java dos.writeBoolean(true); i am able to get the data from the GPS. if i pass false then GPS device will not send data. In the same way i am implementing this in node.js but i get error every time when i write true (connection.write(true)) in the connection.

net.js:614
throw new TypeError('invalid data');
      ^
TypeError: invalid data
at Socket.write (net.js:614:11)
at Socket.<anonymous> (/home/daffolap-301/TestProjects/chat-example/server.js:451:24)
at Socket.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:764:14)
at Socket.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:426:10)
at emitReadable (_stream_readable.js:422:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
at TCP.onread (net.js:528:21)

Process finished with exit code 8

How can I write true, should I need to use Buffer or something else?

Thanks in advance.

Below are sample code for both java and Node.js-

Sample Java Block

ServerSocket serverSocket = new ServerSocket(port);
Socket moduleSocket=serverSocket.accept();   
DataInputStream dis = new DataInputStream(moduleSocket.getInputStream());     
DataOutputStream dos = new DataOutputStream(moduleSocket.getOutputStream());
dos.writeBoolean(true);
byte packet[] = ByteWrapper.unwrapFromStream(dis);
if(packet == null){
   System.out.println((new StringBuilder("Closing connection: ")).append(moduleSocket).toString());
   break;
}
AvlData decoder = CodecStore.getInstance().getSuitableCodec(packet);
AvlData decoded[] = decoder.decode(packet);
AvlData aavldata[];
int j = (aavldata = decoded).length;
for(int i = 0; i < j; i++){
    AvlData avlData = aavldata[i];
    System.out.println("i>>"+i+"---"+avlData);
}
dos.writeInt(7);

Sample Node Block

var net = require('net');
net.createServer(function (connection) {
      connection.on('data', function (data) {
      connection.write(true)
   });
connection.on('end', function () {
   });
}).listen(3001);

You do it this way:

var net = require('net');
net.createServer(function (connection) {
    connection.on('data', function (data) {
        connection.write(new Buffer([1]))
    });
    connection.on('end', function () {
    });
}).listen(3001);

See https://docs.nodejitsu.com/articles/advanced/buffers/how-to-use-buffers for more details.

I case you need to send more data types like Integer you need to do similar to what happens in java sources in DataOutputStream ( http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/DataOutputStream.java#DataOutputStream.writeInt%28int%29 ). It means for Integer(30) you use Buffer([0, 0, 0, 30]).

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