I am attempting to use the net lib in Node.js to do simple message passing. In the example on Nodejs.org they provide the following code as a basic echo server:
var net = require('net');
var server = net.createServer(function(c) { //'connection' listener
console.log('server connected');
c.on('end', function() {
console.log('server disconnected');
});
c.write('hello\r\n');
c.pipe(c);
});
server.listen(8124, function() { //'listening' listener
console.log('server bound');
});
and a client for said server:
var net = require('net');
var client = net.connect({port: 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');
});
The example above works, however, if you remove the client.end(); from the client code and force close the script on the client end (simulating a client crashing) the server crashes as well with the error:
Error: read ECONNRESET
at errnoException (net.js:904:11)
at TCP.onread (net.js:558:19)
I attempted to catch the error in the server code with c.on("error",function(){}) as well as server.on('error', function (e) {}); but in both cases the server still crashes when the client disconnects without using client.end()
What is the propper way of checking for this error so that the server does a console.log('connection reset by peer') instead of crashing?
UPDATE:
I tried this same code on my linux box and it seems to work just fine ... so why does it work in linux but not in windows?
you can catch that error by adding this code inside yours 'createServer' function:
process.on('uncaughtException', function (err) {
console.error(err.stack);
console.log("Node NOT Exiting...");
});
Related
I am developing socket application in my server with node js. I am just listening 9000 port. I am checking the data that client sent to this tcp port, if a client made http request, I kick client from server. Because some bots in the internet does that and I dont want them in my system. Due to test purposes, I try to connect that port with a browser, hold down F5 refresh button continuously, then application crashes immediately. I am simulating the DDOS attacks in my port by this way. The error message as follows:
events.js:183
throw er; // Unhandled 'error' event
^
Error: read ECONNRESET
at _errnoException (util.js:992:11)
at TCP.onread (net.js:618:25)
And here is my basic TCP listener code
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
});
server.listen(9000, function()
{
console.log("I am listening.");
});
What can be done to save TCP port from HTTP connections and internet bots?
Put this under socket.end():
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
full code:
var net = require("net");
var server = net.createServer();
server.on("error", err =>
{
console.log("error handled, error is : %s",err);
});
server.on("connection",function(socket)
{
var remoteAddress = socket.remoteAddress;
console.log("new client connection %s",remoteAddress);
socket.end();
socket.on('error', function(error) {
console.log('Socket got problems: ', error.message);
});
});
server.listen(9000, function()
{
console.log("I am listening.");
});
I'm listening all the connections from a GPS device who is send the data to a our server. So I create a small NodeJs app to retrieve that information.
var net = require('net');
var server = net.createServer(function(connection) {
connection.on('drain', function(){
console.log('drain');
});
connection.on('data', function(data){
var dataGps = data.toString('ascii');
var fecha = new Date();
console.log(String(fecha));
var dataArray = dataGps.split(',');
});
connection.on('end', function() {
//console.log('Client disconnected'.magenta);
});
connection.on('error', function(err) {
//console.log("Connection error: " + err+" ... ".red);
//console.log('Closing connection....'.red);
//connection.destroy();
});
connection.on('close', function(){
console.log('closed event fired');
});
});
server.on('connection', function(){
console.log('new connection');
});
server.getConnections(function(err, count){
if (err) {
console.log('# Error: '+err);
}
console.log('Count: '+count);
});
server.listen(3001, function() {
console.log('Server is listening on port 3001...');
});
To keep running this App, I'm using pm2 (pm2 website).
Everything is fine except that sometimes this app becomes inactive and don't do anything until I restart it with pm2.
This app is in Amazon EC2 (t2.medium instance) Ubuntu 16.04.
I don't know what I'm doing wrong. Any help would be really helpful.
Following is a net tcp client code
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
client.write('Hello server');
});
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.write('Hello server') is written inside the client.connect scope.
Is there a way to use it outside the client.connect scope.
I tried the following code but failed to send anything to server. However the return value of client.write returned true.
var net = require('net');
var client = new net.Socket();
client.connect(1337, '127.0.0.1', function() {
console.log('Connected');
});
client.write('Hello server'); //Did not work
I have set up a node program (actually two, one for the server and one for the client) but I get this error from my client every time I run it:
events.js:85
throw er; // Unhandled 'error' event
^
Error: connect ECONNREFUSED
The code for the server:
var net = require('net');
var server = net.createServer(function (socket) {
console.log('Connection from ' + socket.remoteAddress);
socket.end('hello world')
});
server.listen(7000, '0.0.0.0')
This works fine. As for my client code, not so much. Here is my client code:
var net = require('net');
var client = new net.Socket();
client.connect(7000, 'IP of server here'); // in my actual code, I used the actual ip, of course
client.on('data', function(data) {
console.log('Data: ' + data);
client.destroy();
});
client.on('close', function () {
console.log('Connection closed');
});
This is one of my first node programs, and it is my first using TCP, so expect a newbie mistake. Thanks in advance for the help.
You need to handle the "error" event to avoid the default exception throw:
client.on('error', (error) => {
// treat error here
})
I try to setup two node.js servers communication with each other over socket.io. The node servers use SSL, but I don't get it running. I do not get any feedback, its close to this:
Node.js socket.io-client connect_failed / connect_error event
This will not work. No response.
var clientio = require('socket.io-client');
console.log('Trying stuff ...');
// the channel does not exist
var socket = clientio.connect( 'http://localhost:4000/news' );
// I expect this event to be triggered
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
socket.on('connect', function(){
console.log('Connected');
});
socket.on('disconnect', function () {
console.log('Disconnected');
});
but if I try:
// Bind to the news namespace, also get the underlying socket
var ns_news = clientio.connect( 'https://localhost:9000' );
var socket = ns_news.socket
// Global events are bound against socket
socket.on('connect_failed', function(){
console.log('Connection Failed');
});
socket.on('connect', function(){
console.log('Connected');
});
socket.on('disconnect', function () {
console.log('Disconnected');
});
// Your events are bound against your namespace(s)
ns_news.on('myevent', function() {
// Custom event code here
});
I can see that ns_news has no element socket, so I get:
TypeError: Cannot call method 'on' of undefined
So how do I connect these two servers with feedback if the connection is successful or not?
And my following question would be:
How can these two servers authenticate to each other?
Means: Server A says to server B:
- hey, gimme that secret string
And Server B checks the certificate of server A and if it's ok
- here's the string
How do I do it with node?