2

I have a server on node.js and client on Java. When client of Java connects to Nodejs server then it shows me error which is given below.

I have a Server on node.js

const net = require('net');
const server = net.createServer((c) => {
// 'connection' listener
console.log('client connected');
c.on('end', () => {
console.log('client disconnected');
});
c.write('hello from server\r\n');
c.pipe(c);
});

server.on('data', ()=>{
Console.log(data); 
});
server.on('error', (err) => {
throw err;
});
server.listen(6969, () => {
console.log('server bound');
});

I hava a java code.

public class NodeJsEcho { 
// socket object
private Socket socket = null; 
public static void main(String[] args) throws UnknownHostException,    
IOException, ClassNotFoundException { 
    // class instance 
    NodeJsEcho client = new NodeJsEcho(); 
    // socket tcp connection 
    String ip = "127.0.0.1"; 
    int port = 6969; 
    client.socketConnect(ip, port); 
    // writes and receives the message

    String message = "message123"; 
    System.out.println("Sending: " + message); 
    String returnStr = client.echo(message); 
    System.out.println("Receiving: " + returnStr); 
    } 
// make the connection with the socket 
private void socketConnect(String ip, int port) throws UnknownHostException,    
 IOException{
    System.out.println("[Connecting to socket...]"); 
    this.socket = new Socket(ip, port); 
    }
// writes and receives the full message int the socket (String) 
public String echo(String message) 
{
    try {
        // out & in 
        PrintWriter out = new PrintWriter(getSocket().getOutputStream(),    
        true); 
        BufferedReader in = new BufferedReader(new   
        InputStreamReader(getSocket().getInputStream()));
        // writes str in the socket and read 
        out.println(message); 
        String returnStr = in .readLine();
        return returnStr; 
        } catch (IOException e) 
    {
            e.printStackTrace();
            }
    return null; 
    } // get the socket instance 
private Socket getSocket() 
{
    return socket; 
    }

}

Its shows me the error on server side of:

 client connected
  events.js:141
  throw er; // Unhandled 'error' event
  ^

Error: read ECONNRESET
at exports._errnoException (util.js:870:11)
at TCP.onread (net.js:550:26)
Done.
4
  • Do you see any error on the client side? Does it connect at all? Commented May 8, 2016 at 12:37
  • No no error on client side. Commented May 8, 2016 at 12:38
  • The client side is: [Connecting to socket...] Sending: message123 Receiving: hello from server Commented May 8, 2016 at 12:39
  • Yes it's on the client side output.. Commented May 8, 2016 at 12:41

1 Answer 1

1

You are not closing the socket on the client side - causing connection reset.

// In echo()
in.close();
out.close();

// In main()
getSocket().close();
Sign up to request clarification or add additional context in comments.

1 Comment

@QandeelHaider - Try closing the streams as well.

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.