0

I have an Android APP that needs to handle multiple TCP connections, as server-side. Nowadays I know all the possible ports where the clients are trying to connect, so I'm creating TCP socket listeners for each one.

But on the other hand, sometimes I receive connections to some ports where I don't have socket listeners, so the connection fails.

Is there any kind of TCP socket listener that accepts all incoming ports or a way to know when a incoming connection it's been dropped because no socket could handle it?

So, possible solutions I'm thinking are:

  • Open a generic TCP socket listener that accepts any TCP connection and then create a socket to handle it.

  • Catch dropped incoming connections to create a socket listener and make them work at the second try.

To have a more specific idea, I'm trying to do a NAT on android, so I need to be listening on all possible incoming connections to handle them and forward to the correct network.

Is it possible to do that on java/android?

Thanks!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
rul3s
  • 332
  • 1
  • 2
  • 18
  • Possible duplicate of [Java Server - Multiple ports?](https://stackoverflow.com/questions/5079172/java-server-multiple-ports) – emandt Feb 13 '19 at 11:11
  • 1
    TCP requires individual ports per connection. There is no way to open a single listener for all available ports. You would have to open a listener for each port individually. But you really shouldn't be doing that. What is stopping you from just using a single port for your server? Multiple clients can connect to the same server port. Why do you have clients that are trying to connect to ports you are not listening on? Sounds more like a bug on the client side than on the server side. – Remy Lebeau Feb 14 '19 at 01:03
  • @RemyLebeau I'm doing some kind of forwarder APP between 3G and WifiAP connections. Multiple softwares will connect throught it, and I can't specify all of them, i want something abstract, working like a NAT. An incoming connection from 3G, catch it, get data, and then open another socket on WifiAp. This is my main idea, and nowadays I've got it working, but for example a pasive/active FTP it can't work because i don't know the random port that FTP data is going to use. – rul3s Feb 14 '19 at 08:47
  • @rul3s even NATs use pre-configured ports. I still don't understand why your forwarder can't as well. As for FTP, the ports used during transfers are announced in the messaging. FTP-aware NATs parse the messages between client and server to discover which ports need forwarding – Remy Lebeau Feb 14 '19 at 17:07

0 Answers0