I need to write socket in nodejs such as in PHP. In PHP language I do something like the following:
$http_request = "POST $path HTTP/1.0\r\n";
$http_request .= "Host: $host\r\n";
$http_request .= "User-Agent: Picatcha/PHP\r\n";
$http_request .= "Content-Length: " . strlen($data) . "\r\n";
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n";
$http_request .= "\r\n";
$http_request .= $data;
$response = '';
$fs = @fsockopen($host, $port, $errno, $errstr, 10)
if (FALSE == $fs) {
die('Could not open socket');
}
fwrite($fs, $http_request);
How can I do thing above in nodejs server?
Take a look at the documentation for the net
module.
net.connect(arguments...)
Construct a new socket object and opens a socket to the given location.
The function returns a Socket
.
There's a small example snippet on the page to demonstrate its use:
var net = require('net');
var client = net.connect(8124, function() { //'connect' listener
console.log('client connected');
client.write('world!\r\n');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('client disconnected');
});
It's been a while since I wrote PHP, but I would try this as a translation of your code:
var net = require('net');
var http_request;
http_request = "POST " + path + " HTTP/1.0\r\n";
http_request += "Host: " + host + "\r\n";
http_request += "User-Agent: Picatcha/PHP\r\n";
http_request += "Content-Length: " + data.length + "\r\n";
http_request += "Content-Type: application/x-www-form-urlencoded;\r\n";
http_request += "\r\n";
http_request += data;
var client = net.connect(80, host, function() {
client.end(data);
});
It's worth nothing that, unless there's a reason not to, you can use the request
method of the http
module to make HTTP requests.
In NodeJS we have some modules for socket programming but the most popular is net
.
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 6969;
// Create a server instance, and chain the listen function to it
// The function passed to net.createServer() becomes the event handler for the 'connection' event
// The sock object the callback function receives UNIQUE for each connection
net.createServer(function(sock) {
// We have a connection - a socket object is assigned to the connection automatically
console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
// Add a 'data' event handler to this instance of socket
sock.on('data', function(data) {
console.log('DATA ' + sock.remoteAddress + ': ' + data);
// Write the data back to the socket, the client will receive it as data from the server
sock.write('You said "' + data + '"');
});
// Add a 'close' event handler to this instance of socket
sock.on('close', function(data) {
console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
});
}).listen(PORT, HOST);
console.log('Server listening on ' + HOST +':'+ PORT);
You can easily install net
module by running npm install net
on terminal.
Reference: http://www.hacksparrow.com/tcp-socket-programming-in-node-js.html