Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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.

share|improve this question
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 May 12 at 5:16
 
I need to catch the exception, i am sure of that. How can i do that –  Mohammad Mustafa Hossaini May 12 at 8:50

2 Answers

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.
    });
});
share|improve this answer
 
Sweet that worked...Thanks mate..I need to get my head around this thing cause it is pretty darn useful –  Mohammad Mustafa Hossaini May 12 at 22:48

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.

share|improve this answer
 
So how can i catch this exception, How can i fix my code to catch the exception? –  Mohammad Mustafa Hossaini May 12 at 8:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.