12

I am trying to develop an Android (server) app that will accept TCP connections directly from the Internet. I tried this a couple of years ago with no joy. Is this even possible?

I asked this question differently way back then, on StackOverflow and elsewhere, and never got a satisfactory answer. The answers fell into two categories:

  • No, you can't
    Most authoritative answer given: Most carriers won't route connection requests to mobile devices.

  • Yes, you can
    These answers did not provide details or examples to help me.

What I Tried

  • I can successfully get my mobile device's external IP through various means. But this is only part of the puzzle. See Note 1 below.

  • I created a ServerSocket that listened on a port (e.g., 2000). But the app would not accept connections from the Internet on this socket. Apparently, this socket was listening on the local (internal) network of the device (i.e., 192.168.1.0).

  • I was able to connect from other apps on the device (which are on the same subnet).

  • I was also able to connect through a NAT router in my shop that translated Internet addresses to my device's local net. But this doesn't help me in the field (where no router is available). See Note 2 below.

  • I tried asking AT&T customer-support-type people but I don't think they understood my question.

Notes:

    1. Yes, the device's IP address can vary with time or location, but I can always get the correct and current IP through various means (e,g., whatmyip.com, etc.). Nontheless using the current device IP doesn't work either. BTW - my public IP has never changed. Thus, for all intents and purposes, my IP is static. In any case, the IP issue is a well-flogged horse, I am fairly sure that this is a ROUTING issue, not an IP address issue.
    1. I can connect via WiFi because my Internet modem/router offers DHCP connections on the back side via Wifi on the device's local subnet (192.168.1.). My device has address 192.168.1.65, therefore I can connect to it without ever going through my carrier.
    1. AT&T tells me that I cannot buy a static IP for a mobile device (update 9/23/19).

My Current Theory (9/23/19)

It is my current humble belief that there is a NAT-like entity at play here. Either:

  • The public IP actually points to a NAT router at AT&T HQ.
  • The device has a built-in NAT-like router

In either case, under this theory, inbound TCP connections are not forwarded to the device's local net, but outbound connections pass through just fine.

Summary

Maybe the answer's still the same, but I thought I'd ask again in case things have changed or somebody's got some better insight nowadays,

DontPanic
  • 2,164
  • 5
  • 29
  • 56
  • If you understand how NAT works, what makes you believe that a non-mobile initiated connection would be accepted by carriers? While not related, I believe gives a good description of the operating environment: [JDBC vs Web Service for Android](https://stackoverflow.com/a/15853566/295004) – Morrison Chang Sep 14 '19 at 18:57
  • Communicating two smartphones on the network is obvious that you can do .... otherwise how would messaging services work? ... there is a server in the middle that sends and receives packets from both one and the other .. .not capsicum what you ask ... explain yourself better. – Bronz Sep 15 '19 at 02:48
  • Morrison: Re "non-mobile connections...": Ref my 4th bullet above, my shop's modem/router provides DHCP connections via WiFi on local subnet 192.168.1.0. My phone connects as 192.168.1.65. So I can connect to my app from the Internet through my router *without* going through any carrier. – DontPanic Sep 15 '19 at 17:05
  • 1
    Marios: My humble *belief* is that messaging services, etc. operate by periodically polling (syncing with) servers using *outgoing* connections NOT by connecting to my device. – DontPanic Sep 15 '19 at 17:10
  • 1
    What I mean by "non-mobile initiated" is "not phone initiated". You have two types of test cases described in your question. One is when the phone is connected to the carrier directly and only listens (doesn't send) which doesn't work, no one can get your IP as its dynamic, i.e. drive from one side of town to another. Other is when the phone is connected to WiFi router going through your DSL,Fiber,Cable internet provider, IP is persistent and where you've port forward through the modem, router to the phone in listen mode (possible PnP behavior with your router or the phone is the DMZ Host). – Morrison Chang Sep 16 '19 at 02:26
  • Morrison: Yes, I realize the (dynamic) IP may change, but that is NOT the problem. Even if I get the current, correct external IP, it still does not work. – DontPanic Sep 16 '19 at 14:18
  • What you want requires a special data plan with **public IP** (without NAT, some providers have such contracts). Those data plans are usually for business use cases where you have to connect from the Internet to the device. Because of the scarceness of IPv4 addresses contracts with public IPs got very uncommon and expensive. – Robert Sep 23 '19 at 13:33

6 Answers6

1

It's true that you can't establish any TCP or UDP connection which is listening behind the main network (i.e. behind the NAT). Nowadays all the ISP allocate a private IP to every device (due to shortage of IP) connected to it hence creating a private network of devices behind NAT. (After the complete adoption of IPv6 the situation will change with every device becoming the part of main network)

Since NAT doesn't allow an inbound connection, the problem is solved using some of the NAT traversal technique such as:

  • Port forwarding
  • TCP/UDP Hole punching

In the Port forwarding process, we need to deal with the router setting which will forward the connection on a specific port to the same port on the internal network node. It is mainly used in gaming, VoIP, and p2p application.

In Hole punching technique (no need to deal with the router setting) we use an external server which is not behind any NAT to get the IP address (both public and private) of the devices which are behind NAT. Further, the two hosts create there own outbound session among each other in their respective NAT devices. Since the NAT devices allow the outbound session, the session is stored, and this same session is used for further two-way communication. More information about Hole punching.

Saurav Kumar
  • 891
  • 8
  • 14
1

(UPDATED 9/26/19)

I am posting this "answer" not so much as an answer per se but rather to solicit constructive criticism and/or affirmation. Any statements I make below should definitely be implicitly prefixed by "I humbly believe that...".

Yesterday, I had a personal epiphany. I finally got it through my thick skull what some responders were saying. I present my new understanding below.

Heretofore I believed that AT&T's Mobile Data Network was just a direct extension to the Internet implemented with cell sites and trunk lines. Now I realize that it is a closed, private data transport mechanism. Since it is owned by AT&T, they can implement whatever protocols they choose to, including "private" data connections from their head-end to my registered phone. These connections are analogous to the LAN-side ports on a typical desktop NAT router. The head-end (i.e., their NAT router) is the only entity that can be accessed from the Internet.

The head-end connections between the Internet and my phone are over these AT&T-private connections. The "public IP" reported for my device (and, I believe, nearby devices) actually points to the AT&T head-end, in my case a 150 miles away in Miami). The head-end fowards outbound connections from my phone to the Internet, but does not forward inbound connection request.

About my "Public IP"

I believe that this doesn't exist and is, in fact, a meaningless concept. What would be the point of such an address if you cannot connect to it.

Summary

I now believe that the answer to my original question is "No way, Jose". This is not due to an addressing issue, but rather AT&T will not route incoming connections to devices. They also do not offer static IPs for devices over their private network (this wouldn't make much sense anyway given the above).

IF ANY STATEMENTS IN THIS PROTO-ANSWER ARE INCORRECT, PLEASE PLEASE LET ME KNOW WHICH ONE AND WHY IT IS INCORRECT

DontPanic
  • 2,164
  • 5
  • 29
  • 56
0

It depends on what type of IP your mobile phone provider assigns you

For your mobile device to accept TCP connections from the outside, it is only possible if your provider assigns you a public IP, but this is increasingly unlikely since given the shortage of available IPs many providers assign a private IP, like if your phone were inside a LAN, in this case your TCP port is unreachable from the outside.

Websites like whatsmyip tells you the public IP with which your provider finally connects you to the internet but it may be different from the one your provider assigns to your phone.

To know if you are in a private network install from Play Store an app called "PingTools Network Utilities". When you start it you will see the IP of your phone, if it is different from the whatsmyip IP or is an IPV6 one that means that your provider assigns you a private IP, an unreachable one.

... always talking about connections through the cellular network and not through WiFi

from56
  • 3,976
  • 2
  • 13
  • 23
  • Ping Tools shows the *same* address as whatsmyip. Also, I don't know what you mean by a "private IP". – DontPanic Sep 23 '19 at 13:31
  • 1
    @DontPanic: A private IP is an IP that is not routed in the Internet (not used in public traffic). Usually you get such an IP by a DHCP server behind a NAT router. Typical private IP ranges are 192.168.x.x or 10.x.x.x. – Robert Sep 23 '19 at 13:36
  • I mean the IP shown in top left on Ping Tools screen. The phone IP. If the provider assigns you a public IP it should be the same than whatsmyip. – from56 Sep 23 '19 at 14:33
  • The public IP is always the same (and has not changed in years). BTW - I've tried many tools like Ping Tools and they *all* say the same thing. – DontPanic Sep 24 '19 at 14:14
  • What matters is that the IP of the phone to be a public IP, regardless of whether it is static or dynamic, or change or not change. If your phone is within a private network, as a majority of providers currently do, your ports will not be accessible. And I mean the phone's IP, not whatsmyip IP – from56 Sep 24 '19 at 14:30
  • I mean de IP in the top left corner of Ping Tools app – from56 Sep 24 '19 at 14:33
  • I withdraw any statements I made about "my public IP". I now believe that this is a meaningless concept. See my proto-answer. – DontPanic Sep 26 '19 at 12:48
  • You can also see your IP address in Android by navigating to _Settings > About Device > Status > IP Address_. As Robert said, a prefix of 10.*, 172.16-31.* or 192.168.* indicates a private address. – TheOperator Apr 04 '20 at 21:14
0

Write a simple client for DynDNS or alike, in order to provide it with name resolution. Then you can serve pages with NanoHTP - or raw sockets. If you're behind a DSL router, you may need to read the external IP with TR-604 (unless DynDNS might eventually return the source IP) and setup a NAT port-forward for the incoming traffic on port :80. At home this is easier to setup than through UMTS, because some UMTS networks don't have public IP addresses, but may have private IP addresses.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • AGAIN, this is NOT AN ADDRESS ISSUE but rather a ROUTING issue. – DontPanic Sep 24 '19 at 19:50
  • You don't have to shout and don't have to repeat, because I'm neither deaf nor stupid. while when you're getting a natted private IP by your ISP - this will not be route-able in a public network. eg. Comcast even has it setup this way for DSL - while for UMTS subscriptions it's more common. In fact, when you have an address in the wrong range, this will be a certain routing problem. An external IP address not necessarily always is a publicly route-able IP address. – Martin Zeitler Sep 24 '19 at 21:54
  • Apologies. I didn't mean to shout - only emphasize. I repeat because everyone keeps talking about a "static" IP. My external IP never changes, so it is, in effect, a static IP up til this point in time. I guess I'm ignorant: I don't understand what a "natted private IP". Heretofore, I believed there were only two types of addresses: (1) a public IP and (2) a private IP (e.g. 192.168.X.X or 10.X.X.X) on the LAN-side of a router. – DontPanic Sep 24 '19 at 22:17
  • Continuing - Are you saying that there is another ISP-proprietary type of address that is routeable over their mobile data network, but *not* over the Internet? Pardon my ignorance but I'd love to be enlightened. – DontPanic Sep 24 '19 at 22:25
  • @DontPanic that ISP provide NAT is rather the proprietary kind of connection; ordinary it's just: router external IP (public) -> router internal IP (private) -> host IP (private). The NAT you get from the local router can be configured, but provider-side one has zero influence over it. Just check on your router what the external IP is - if it's a public one, one can port-forward to the IP of the mobile device. DynDNS is only useful for dynamic external IP, because it provides name-resolution for ever-changing IP addresses (whenever the DHCP lease runs out or when it re-connects). – Martin Zeitler Sep 24 '19 at 22:41
  • The joke is, that they came up with IPv6, because of address shortage... and then instead produce such NAT workarounds on the IPv4 network, which would only make sense if there was no IPv6. That may be, because they have to lease the address ranges from ARIN, no matter the protocol version in use - unless it's a private network. – Martin Zeitler Sep 24 '19 at 22:51
  • Martin, I am starting to understand what you are saying. Please see my "answer" posted today. Tell me if I'm getting close... – DontPanic Sep 25 '19 at 13:54
  • I withdraw any statements I made about "my public IP". I now believe that this is a meaningless concept. See my proto-answer. – DontPanic Sep 26 '19 at 12:49
0

I want to give a non - technical answer as I myself am not very proficient with network theory.

One thing is that Internetproviders usually expect extra cash if you want to run a server - hence the forced disconnects on consumer subscriptions like DSL(changing IP in the process).

Next thing is that establish a connection and send data out to a server is very unreliable on a mobile device. You can have android check if you have network. If it says no you don't need to try it - no means no. But yes doesn't mean a thing!

I write software for asset tracking using android. Usualy I want to send like one location per minute to that server. So I try to open a port. Android blocks on new Socket and it can go for like 2 Minutes until a timeout occurs even if I checked with ConnectivityManager right beforehand and it got me isConnected() == true.

And if I have a socket and even received something a second ago: I can write to that socket and get no error but get no answer, write again, no error no anser again and again.. And after like 2 minutes I get a whole bunch of errors in the log all at the same time.

When sitting in the bus visiting my mother is like driving through a 3rd world country judging by the internet connection. It might say 4g and 3 bars but chrome won't load page OR it says 2g and only 2 bars yet browser has no problems at all..

The whole android network-os (and I guess apple has to do it quite the same) is programmed more like: "here android I want to send this" and android saying "Ok" but thinking "I will send it as soon I feel fit to do so" :D

FrankKrumnow
  • 501
  • 5
  • 13
  • I think you misunderstood my question. My question concerned connecting TO a moblle device (INBOUND connection). I've never had an (unexpected) problem creating an OUTBOUND connection. – DontPanic Jul 18 '20 at 20:18
  • I just wanted to give reasons to why no provider would want to sell cellphone contrancts with static ip's to enable android devices to work as servers. – FrankKrumnow Jul 20 '20 at 12:57
-1

you need a static ip(public ip) address for that .

Mobile device ip address are not static so u cant .

To make it work .

Run a web server on a static ip & route the queries to your app (tunnelling).

When android app starts ,you android app needs to register with web server .

Now server knows your android device availablity ,the queries from other end will be be rediredcted.

Rajesh Gopu
  • 863
  • 9
  • 33
  • With all due respect, I don't think this is correct. All a static IP does is the make the IP address of a server persist so people can connect to it in the future, i.e., the address doesn't change. BTW - my devices (dynamic) IP hasn't changed in years. – DontPanic Sep 23 '19 at 13:14
  • This is the way all messaging & chat applications works (with the help of STUN ,TURN ,ICE servers) May be for sometimes your device ip address assigned by carrier will be same.But not always. Thats why the intermediate server coming into picture . If you are able to ping your mobile device carrier ip from a remote network over internet then your application should work temperorly for sometime. Is your ping command is success from a remote network ? – Rajesh Gopu Sep 24 '19 at 05:24
  • My external IP has been the same **forever**. And yes, I can ping it from the Internet. – DontPanic Sep 24 '19 at 14:03
  • I withdraw any statements I made about "my public IP". I now believe that this is a meaningless concept. See my proto-answer. – DontPanic Sep 26 '19 at 12:47