0

I was trying the very basic socket communication between node.js and a java program here.

When I tried the code provided on my localhost everything works fine. But when I use another laptop to connect, I wasn't able to get any message shown in the server side.

Basically I changed the "localhost" into the ip address, as shown in the following:

var net = require('net');

var client = net.connect(4444, myIp, function() {
    console.log("connected!");
});
client.setEncoding('utf8');
setInterval(function() {
  console.log("Writing....")
  var ret = client.write('Hello from node.js\n');
  console.log("Wrote", ret)
}, 1000);

And the code for server remains unchanged. But I didn't see any output in server side, and the "connected" message was also not shown in client.

I tried modifying code in server as well. I changed server = new ServerSocket(4444) to server = new ServerSocket(4444, 50, InetAddress.getByName(myIp)):

public class SocketServer {

    public static final int port = 12345;
    private ServerSocket server;

    public void listen() {
        try {
            // server = new ServerSocket(4444);
            server = new ServerSocket(4444, 50, InetAddress.getByName(myIp));
        } catch (IOException e) {
            System.out.println("Could not listen on port 4444");
            System.exit(-1);
        }
        while(true){
             ... Same code here
        }
    }

    /* creating new server and call it's listen method */
    public static void main(String[] args) {
        new SocketServer().listen();
    }

But got IoException that can't bind to that ip.

Any suggestions on how to modify the code to build socket connection between two laptops with different ips?

update
(1) I personally don't think I'm asking about the same thing as this post. I don't have problem working on localhost, but have issue when using two machines with different ips.
(2) I've checked that my laptop's firewall is off.

Patrick
  • 555
  • 1
  • 6
  • 25
  • If you want to support connections from other networks, your server needs to bind to your **internal** IP address. You can verify that, after you've done that, that your port is open using https://canyouseeme.org/ – Jacob G. Feb 22 '19 at 21:14
  • Note this part appears to be a syntax error, did you miss the `new` keyword? `server = ServerSocket(4444, 50, InetAddress.etc...` – markspace Feb 22 '19 at 21:36
  • Even if you're communicating on the same lan, the "server" could have a firewall that's blocking incoming connections. Can you check that? Also a full stack trace might be more helpful rather than just a single println. – markspace Feb 22 '19 at 21:38
  • Possible duplicate of [Java server JavaScript client WebSockets](https://stackoverflow.com/questions/41470482/java-server-javascript-client-websockets) – Ayoub Benayache Feb 22 '19 at 21:39
  • @markspace thanks for the reply! I did miss the `new` keyword sorry about that! As for the firewall, I've checked that my firewall is off (I'm using Mac), and the message shown is "This computer's firewall is currently turned off. All incoming connections to this computer are allowed". – Patrick Feb 22 '19 at 21:52
  • @JacobG. Thanks for the information! I found my internal ip in System and Preferences and change the code to `server = new ServerSocket(4444, 50, InetAddress.getByName("172.XX.XXX.XX"))`. This time I didn't get exception on server side. But when I use canyouseeme.org to test it, the ip shown there seems to be my public ip and when I check port 4444, it timed out. – Patrick Feb 22 '19 at 22:18

0 Answers0