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.