3

If you were to implement a persistent tcp connection on android what port number would you choose?

This is the sort of connection used by Google's C2DM service.

Erdal
  • 1,472
  • 3
  • 16
  • 33
  • You want to know which port you should use for the daemon on your server? Did I understand this correctly? – Julian Sep 05 '11 at 04:25
  • yes, for the server, since the client initiates the connection. – Erdal Sep 06 '11 at 00:46
  • this is very interesting and clears it up a bit more: https://labs.ericsson.com/apis/network-probe/ – Erdal Sep 07 '11 at 09:37

3 Answers3

1

Use any port really. It probably doesn't really matter so long as it's above 1024. Let's say you choose port 5000.

I'd recommend using a second ip address and forward all traffic on ip2:80 to ip1:5000. That way you can get around any firewall restrictions on your network.

If you want more details about adding a second ip address and adding a NAT to your iptables to forward traffic from port 80 on ip2 to port 5000 on ip1, I can share my notes with you.

Eamorr
  • 9,872
  • 34
  • 125
  • 209
  • so you are suggesting the client app would connect on port 80? – Erdal Sep 08 '11 at 23:11
  • Yep, this is no problem so long as you have two IP addresses. On my cell phone network, only ports 80 and 443 are open - this is how I get around that. – Eamorr Sep 08 '11 at 23:25
  • and how long will a connection stay around before the carrier decides to kill it? I mean how often should I send keep alives? – Erdal Sep 09 '11 at 02:38
  • Not sure. I think it might vary from carrier to carrier depending on the network equipment they use. Do you want to do long polling and/or server push? If so, I'd recommend using Node.js on the server side and Socket.IO on the client side. – Eamorr Sep 09 '11 at 14:42
  • I want to keep a tcp socket always connected and do server push. Interesting pointer about Socket.IO. I think it's only Javascript isn't it? My app is android native Java. Thanks! – Erdal Sep 09 '11 at 19:00
0

How about 1764? (42*42). Or possibly 3141?

JesusFreke
  • 19,784
  • 5
  • 65
  • 68
0

Technically it really doesn't matter what port you choose. You just can't use a port that is needed for another service if you also want to run this service. So if you want to run it on a mail server, port 25 is a bad choice.

But since there are sometimes firewalls in place that may filter traffic, I'd recommend port 443 (https) where you have a slightly higher chance of getting your traffic through.

Julian
  • 2,051
  • 2
  • 22
  • 30
  • I'm pretty sure you wouldn't be able to bind to the low ports – JesusFreke Sep 06 '11 at 04:37
  • I want to keep this connection alive without sending any traffic on it for as much time as needed. I'm not sure if you are aware that carriers remove connections that they think are stale after some amount of time. I wonder if they do it more aggressively for connections that use port 443... – Erdal Sep 06 '11 at 10:22
  • 1
    @Julian: on linux systems, you typically need special permissions to bind to the low ports (<1024). On android, I believe a normal application can only bind to ports > 1024. For example, see: http://code.google.com/p/android/issues/detail?id=4039 (old issue.. but I believe it's still the case) – JesusFreke Sep 06 '11 at 14:03
  • @Erdal Carriers do all kind of strange things. There won't be this one solution for everything. – Julian Sep 06 '11 at 17:07
  • @JesusFreke He asked for the port on the server and you usually don't run Android on a server. – Julian Sep 06 '11 at 17:08