Ofek Ron
Ofek Ron

Reputation: 8578

How to establish connections to a ServerSocket from internet?

My ServerSocket listens to LAN Connections and accepts them well, but when I try to connect to the same through my Phone - using the 3G connection - it doesn't seem to connect.

  1. I tried using getMyIP site to get the IP and try to connect to it, it does get the right IP (checked with my router) but then no connections are accepted at all.

  2. I tried opening the port on windows 7 and on my router altogether.

  3. I put those lines in my Server constructor:

    ss = new ServerSocket(port);
    host=ss.getInetAddress().getHostAddress();
    

    and I get the ip on host to 0.0.0.0

Thanks for your help.

Upvotes: 1

Views: 1678

Answers (3)

Santosh
Santosh

Reputation: 17923

  1. Check your firewall if it allows incoming connection. You need to make and exception there.
  2. you need to bind explicitly the IP address on your machine which is allocated for that instance of time by your ISP.
  3. You can get the IP address allocated to you by running ipconfig command on windows command prompt.
  4. Use the following code to bind to a specific IP address

    InetSocketAddress insa = new InetSocketAddress("22.23.23.111", 9090);       
    ServerSocket ss = new ServerSocket();
    ss.bind(insa);
    String host=ss.getInetAddress().getHostAddress();
    System.out.println(host);
    

This prints the IP address allocated to you.

Upvotes: 0

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33544

- While you are at LAN, you can use the Private IP as well as Public IP ranges

- But when you are using the Internet to access the Server which is at your place, then you need to have a static Public IP address.

- You can ask for a static Public IP address from your ISP at some extra cost, there are also some site over net that some how provides a static IP on the basis of your Dynamic IP.

Private IP ranges Can't be used over the Internet.

Class A - 10.0.0.0 - 10.255.255.255  

Class B - 172.16.0.0 - 172.31.255.255

Class C - 192.168.0.0 - 192.168.255.255

Upvotes: 2

Peter Lawrey
Peter Lawrey

Reputation: 533880

You need to have a public IP address. If you have a router it must pass traffic for the port you want to expose to the internet to your machine. If you have a firewall, it must allow external connections to this port.

All the changes you do are the same regardless of language you use and there is nothing you can do from Java to work around needing to do these things.

Upvotes: 1

Related Questions