ServerSocket accept () does not accept connections on Android

I am trying to install on a ServerSocket on my Android phone and send either int or something from my computer.

The code on the phone creates a ServerSocket, and then blocks while waiting for a connection:

ServerSocket serverSocket = ServerSocketFactory.getDefault() .createServerSocket(4444); Log.d("HostThread", "ServerSocket created:"+serverSocket .getInetAddress().getHostAddress()); Socket socket = serverSocket.accept(); 

(The magazine says "10-27 11: 41: 43.437: DEBUG / HostThread (23957): ServerSocket created: 0.0.0.0")

A simple bit of code running on my PC is trying to connect to the phone:

 Socket s = new Socket("xx.xx.xx.xx", 4444); 

... (plus a few more bits if the socket is created, but I don't understand this, so I left it!)

Basically, the phone accepts and the computer does not connect. Xx.xx.xx.xx is the public IP address of the phone I receive programmatically (and this corresponds to checking on whatismyip.com).

I installed the permission of the INTERNET on the phone. I was also able to do this in the reverse order (ServerSocket on PC, client on phone).

Any ideas I'm wrong about?

+4
source share
1 answer

Do you run this in an emulator or on a real device? As far as I understand, most networks of NAT operators do not allow connections to devices. This may be your problem if you are running on a real device.

However, if your code above is just an example - and you are not actually using port 4444, you should also be aware that most unix systems (including Android) do not allow incoming connections on ports below 1024 if you do not have root privileges.

+3
source

All Articles