2

I'm programming a client-server app, my client being an android phone, my server being my laptop.

So my issue is that this one line of code:

Socket connectionToServerSocket = new Socket(hostName, portNumber);

works perfectly fine when my cellular phone connects to the Internet with my home wifi connection, and simply times out when connected through 3G (cellular provider) eg it blocks for a while then throws a timeout exception.

The funky thing is,that I can see (using OS Monitor) that some apps are connected through very common ports, for example port #80, but switching portNumber (as well as the port that the server is listening to) to 80 doesn't help (eg it still times out), and I've tried many different ports-same result.

DNS works fine (eg it translates the logical String which I gave hostName to the correct IP) but it doesn't send the server anything... I'm lost,what could be the reason? How can I check and resolve it?

Yoni Keren
  • 1,170
  • 2
  • 13
  • 24
  • 2
    Is your laptop's IP public (e.g., reachable from outside of your network)? Looks like you have a private IP assigned to it which is not reachable from the mobile phone's 3G network. Check if the IP shown here matches with the IP you are using as host IP of the socket: https://www.whatismyip.com/https://www.whatismyip.com/ – Hungry Coder May 02 '16 at 04:52

3 Answers3

0

I've run into this issue as well doing a similar application.

Your laptop and phone can connect to each other while on the same network because they share a IP address lookup through your router.

When the device is connected to the WIFI, it's request get passed through the router to check for IP addresses, it will find your laptops IP and save a request to a DNS because it can find the laptops IP already. The laptop works the same way, it finds the Ip address of the client through the router as well.

However, when your phone is on 3G, it has no way of knowing exactly where your laptop's IP address is. That's why it times out: it goes from your router to your nearest DNS (where it tries to resolve the correct IP Address), if it cannot find a domain or IP that matches it will fail.

Some steps to fix this . . .

  1. Depending on your router you can set up port forwarding for your laptop's IP. This means incoming requests to your router can be piped to your laptop's server implementation.

  2. Then go to any site like this http://touch.whatsmyip.org/ on your laptop to get your laptops IP. Save this to add to your clients Socket set up.

  3. For debugging until your laptop server is visible for DNS lookup, go into your client code and add this.

    Socket debugSocket = new Socket("the.laptop.ip", 80);

Some warnings:

  • Depending on your Router, your ip may change during restarts
  • With port-forwarding any browser with your ip, ex) 178.12.434.01 can log onto your laptops personal server

Future Changes:

  • Once a dedicated server is up and running, registered with a domain you can change the above ip parameter to "your.domain.com", and behind the scenes the actual IP address to your server will be found via DNS lookup.
Chris Sullivan
  • 576
  • 5
  • 10
  • "they share a IP address lookup through your router." I don't know what that means, but the problem is simply that your phone is on a totally different network when it's using your cellular connection vs your wifi. With wifi, it's on your LAN. With 3G, it's on your cellular provider's network an may or may not be able to receive incoming connections directly. – xaxxon May 02 '16 at 03:57
  • I added some clarification, I hope it makes more sense xaxxon :) – Chris Sullivan May 02 '16 at 04:45
  • Your clarifications may cause more confusion than anything.. When connecting to an IP address on your LAN, the router sees the destination IP address is in a range the router knows to route locally. I don't know what "and save a request to a DNS" even means. Nothing at connection time should cause changes to DNS entries. Also "it finds the Ip address of the client through the router" is odd. I mean, everything goes through the router, I suppose... but routers don't necessarily have anything to do with finding IP addresses. There are many other inaccuracies in your answer. – xaxxon May 02 '16 at 05:28
  • 1
    Also, in your port forwarding instructions, "Socket debugSocket = new Socket("the.laptop.ip", 80);" is wrong. You would need to use your router's external IP address. The laptop's IP address is going to be in an RFC1918 unroutable range. "Depending on your Router, your ip may change during restarts" - your external IP address is determined by your ISP, not your router. – xaxxon May 02 '16 at 05:31
  • @jankigadhiya I'm not sure which parts you're referring to, but I'm pretty sure everything I pointed out is a correction. I'm not sure who should "try to understand once". – xaxxon May 02 '16 at 06:50
  • Plus 1 to xaxxon for the Router's IP addition. – Chris Sullivan May 02 '16 at 22:05
  • xaxxon, please modify the above answer if your willing. – Chris Sullivan May 05 '16 at 08:18
0

case 1: It is working when your laptop and your phone is connected to wifi right ?

Try this once

case 2 :

  1. connect your phone to 3G.
  2. enable hotspot on phone.
  3. connect your laptop to your phone's hotspot.
  4. check the IP of your laptop if it is changed replace that in Socket object. Socket connectionToServerSocket = new Socket(newIP, portNumber);
  5. Run your project.

Just try this once you will get what i am trying to say.

You are getting the timeout exception because your server that is having the service is not reachable from the external network.

Hope this will help :)

Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
0

the reason for that is that the server in your laptop is closed to external network by default, what you need to do is something called port forwarding *take note: port forwarding put your server in a cyber security risk, make sure you make the right adjustments to keep your server safe.