最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why am i getting this error running 'net' module node.js - Stack Overflow

programmeradmin0浏览0评论

I am using modular and opening tcp port on 6112.

var net = require('net');
    var server = net.createServer(function (socket) { //'connection' listener
    });
server.listen(6112, function () { //'listening' listener
    console.log('server started');
});

On the same machine i start a java socket in main.

public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            System.out.println("Connecting...");
            Socket socket = new Socket("localhost", 6112);
            System.out.println("Connected");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

I get this exception,

C:\Users\Mustafa\WebstormProjects\Node.Js>node hello.js
server started

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:884:11)
    at TCP.onread (net.js:539:19)

Is this like a bug or something, cause if once i get through this bug, I will be good thanks.

I haven't used the debugger cause as Ryan said it him self a year ago that it is still shitt.

I am using modular and opening tcp port on 6112.

var net = require('net');
    var server = net.createServer(function (socket) { //'connection' listener
    });
server.listen(6112, function () { //'listening' listener
    console.log('server started');
});

On the same machine i start a java socket in main.

public static void main(String[] args) {
        // TODO Auto-generated method stub

        try {
            System.out.println("Connecting...");
            Socket socket = new Socket("localhost", 6112);
            System.out.println("Connected");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

I get this exception,

C:\Users\Mustafa\WebstormProjects\Node.Js>node hello.js
server started

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: read ECONNRESET
    at errnoException (net.js:884:11)
    at TCP.onread (net.js:539:19)

Is this like a bug or something, cause if once i get through this bug, I will be good thanks.

I haven't used the debugger cause as Ryan said it him self a year ago that it is still shitt.

Share Improve this question asked May 12, 2013 at 4:53 MustafaMustafa 1,7762 gold badges24 silver badges36 bronze badges 2
  • 2 In your example, you're not even trying to send anything via the socket. The Java app instantiates the socket and then exits, and maybe it's possible that node is interpreting that as an unexpected connection resest. Have you tried sending anything and does the connection listener ever get called? Also what do you mean the debugger is 'shitt'? I've used the Node.js/V8 debugging features a ton both via node-inspector/Chrome and WebStorm's interface and it's been nothing but helpful... maybe give it a try? – Bret Copeland Commented May 12, 2013 at 5:16
  • I need to catch the exception, i am sure of that. How can i do that – Mustafa Commented May 12, 2013 at 8:50
Add a ment  | 

2 Answers 2

Reset to default 8

You need to listen for errors on the socket. Node has the default behavior that when something does .emit('error'), if there are no error handlers attached, it will throw the error instead, thus crashing the application.

var server = net.createServer(function (socket) {
    socket.on('error', function(err){
        // Handle the connection error.
    });
});

You are creating a socket and connecting from it, but not closing it. So when the program finishes, to node.js it looks like connection is reset (closed abruptly). Call socket.close(); before program finishes.

You can structure your code in this way :

try {
    tryStatements      //your code that is causing exceptions
}
catch(exception){
    catchStatements    //handle caught exceptions
}
finally {
    finallyStatements  //execute it anyways
}

Or if you like to catch uncaught exceptions from runtime, use this (main process won't exit on exceptions)

process.on('uncaughtException', function(err) {
    console.log('Caught exception: ' + err);
    console.log(err.stack);
});

The problem is in java code which is causing node.js to exit on exception. So be sure to add socket.close();. Above is just error handling on node.js part.

发布评论

评论列表(0)

  1. 暂无评论