Aditya Singh
Aditya Singh

Reputation: 2453

Socket connection not happening between computers present on two separate LAN networks

I am trying to make a socket connection between two computers present on two separate LAN networks respectively. So this is how my two computers are set up. My MacBook is on a network where the router's IP is 121.245.152.106. And my Dell PC is present on a network where the router's IP is 27.4.193.156.

I am using my MacBook as my server. This code will just accept a socket connection from the client(Dell PC) and will print it's IP address. This code is present on my MacBook:

LolServer.java:

import java.io.*;
import java.net.*;



public class LolServer {

    public static void main(String[] args) throws IOException {

        ServerSocket server = new ServerSocket(8080); // ServerSocket listening to port 8080 
        Socket socket = server.accept();
       
        System.out.println(socket.getInetAddress().getHostAddress()); // Printing the client IP address.  
       
        socket.close();
        server.close();
    }
}  

When I

telnet 192.168.1.102 8080 
// or 
telnet 127.0.0.1 8080

from my local machine(MacBook), the code works fine and the LolServer program prints

192.168.1.102
// or 
127.0.0.1

respectively as its output. Here, 192.168.1.102 is the local/private IP address of my MacBook provided by the router with IP 121.245.152.106.

Now here lies the problem. When I try to access the program from a remote network, i.e. from my Dell PC, the telnet command keeps searching for the server and doesn't print any output.

This is what I tried from the client side:

telnet 121.245.152.106 8080  

this gives no output. It doesn't even print any output on my Macbook. I suppose my sockets were not even connected. Is 121.245.152.106 the IP to be used for the connection from my Dell PC? It makes no sense to do this:

telnet 192.168.1.102 8080  

as 192.168.1.102 is the private IP address.

Please help me, I really need to get this working. Thanks in advance.

Upvotes: 0

Views: 1357

Answers (1)

GenuinePlaceholder
GenuinePlaceholder

Reputation: 695

Port forwarding, sometimes referred to as port triggering, is a process of configuring a router to make a computer or network device that is connected to it accessible to other computers and network devices from outside of the local network.

Source: https://m.youtube.com/watch?v=-K6jMYBfuIY

This means that the connection is being block by your router. Also port forwarding for the first time is tricky and somewhat beyond my abilities to instruct. Here is a good website to help you: http://portforward.com/english/routers/port_forwarding/ basically you select your router and the site will teach you based on your router.

Upvotes: 1

Related Questions