Last active
February 15, 2017 15:11
-
-
Save alukinykh/043b67579758632ca54b522ef6928560 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Server | |
var net = require('net'); | |
var server = net.createServer(function(connection) { | |
console.log('client connected'); | |
connection.on('data', function(data) { | |
console.log(data.toString()); | |
}); | |
connection.on('end', function() { | |
console.log('client disconnected'); | |
}); | |
connection.write(connection.remoteAddress + ':' + connection.remotePort + ' connected to server! \r\n'); | |
connection.pipe(connection); | |
}); | |
server.listen(8080, function() { | |
console.log('server is listening'); | |
}); | |
//Client | |
var net = require('net'); | |
var client = net.connect({port: 8080}, function() { | |
console.log('connected to server!'); | |
client.write('123456'); | |
}); | |
client.on('data', function(data) { | |
console.log(data.toString()); | |
client.end(); | |
}); | |
client.on('end', function() { | |
console.log('disconnected from server'); | |
}); | |
//not work | |
var net = require('net'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
}); | |
var params = {}; | |
rl.on('line', function(line){ | |
if (!line.charCodeAt(0)) { | |
var client = new net.Socket(); | |
client.connect(params.port, params.ip, function() { | |
client.write('GET / HTTP/1.1'); | |
}); | |
client.on('data', function(data) { | |
console.log('Received: ' + data); | |
client.destroy(); // kill client after server's response | |
}); | |
client.on('close', function() { | |
console.log('Connection closed'); | |
}); | |
client.on('error', function(err) { | |
console.log(err); | |
}) | |
} | |
}); | |
rl.question('Enter ip-address please: ', function (answer) { | |
params.ip = answer; | |
rl.question('Enter port please: ', function (answer) { | |
params.port = answer; | |
}); | |
}); | |
var net = require('net'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout, | |
terminal: false | |
}); | |
var params = {}; | |
var arg =[]; | |
var client = new net.Socket(); | |
process.argv.forEach(function (val) { | |
arg.push(val); | |
}); | |
var ip = arg[2]; | |
var port = arg[3]; | |
client.connect(port, ip, function() { | |
client.write('GET / HTTP/1.1/n/n'); | |
}); | |
client.on('data', function(data) { | |
console.log('Received: ' + data); | |
client.destroy(); // kill client after server's response | |
}); | |
client.on('close', function() { | |
console.log('Connection closed'); | |
}); | |
client.on('error', function(err) { | |
console.log(err); | |
}); | |
// rl.on('line', function(line){ | |
// if (!line.charCodeAt(0)) { | |
// | |
// | |
// } | |
// }); | |
// rl.question('Enter ip-address please: ', function (answer) { | |
// params.ip = answer; | |
// rl.question('Enter port please: ', function (answer) { | |
// params.port = answer; | |
// }); | |
// }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment