3

I am using .net 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.

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? Commented May 12, 2013 at 5:16
  • I need to catch the exception, i am sure of that. How can i do that Commented May 12, 2013 at 8:50

2 Answers 2

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.
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet that worked...Thanks mate..I need to get my head around this thing cause it is pretty darn useful
0

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.

1 Comment

So how can i catch this exception, How can i fix my code to catch the exception?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.