i'm having a problem when i send data to client using TCP server in node js. this is my code
var net = require('net');
var server = net.createServer(function (socket) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + socket.remoteAddress +':'+ socket.remotePort);
socket.on('data', function(data) {
var datas = JSON.parse(data);
console.log('DATA ' + socket.remoteAddress + ': ' + data);
//socket.write("kenapa ya / HTTP/1.0\r\n\r\n");
}).on('connect', function() {
// Manually write an HTTP request.
var data = {
"name" : "test",
"args" : {
"data":[{
"a":1
}]
}
};
socket.setEncoding("utf8");
socket.write(JSON.stringify(data) + "\r\n\r\n");
}).on('end', function() {
console.log('DONE');
});
});
server.listen(1337, '127.0.0.1');
console.log("server is listen on 1337");
actually that code is work in my client socket, but when i'm try to socket.write(JSON.stringify(data));
without \r\n\r\n
too long response from server and i cannot get the message from server.
anyone know what happen? and what the effect this code \r\n\r\n
?
i'm using php to create socket client.
CRNL twice is the delimiter between a HTTP header and body. See the HTTP RFC.