views:

57

answers:

2

Hi,

I am trying to setup at ServerSocket on my Android phone, and send a char or int or anything from my computer.

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

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

(The log 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 tries to connect to the phone:

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

... (plus some more bits if the socket is created. But I'm not getting this point, so left that out!)

Basically, the phone is getting to accept, and the computer is not connecting. The xx.xx.xx.xx is the public IP of the phone I obtain programatically (and it matches up with checking on whatismyip.com).

I have set the INTERNET permission on the phone. I have also been able to do this in reverse (ServerSocket on pc, client on phone).

Any ideas where I am going wrong?

A: 

Are you running this in the emulator or on a real device? As far as I understand, most operators networks are NATed and doesn't allow connections to devices. This may be your problem, if you're running on a real device.

However, if your code above is just an example - and you actually don't use port 4444, you should also be aware of that most unix systems (including Android) won't allow incoming connections on ports lower than 1024, unless you have root permissions.

Björn
I'm running on a real device, connected to my mobile network. Are you saying that it is impossible to allow incoming connections like this?
lukeo05
@lukeo05 Impossible depending on your network operator :) Ask your operator, they should know!
Björn
I assume that it is possible over Wifi? That seems like my only option if the network operator says NO!
lukeo05
Or design your network usage to go in the other direction, so the connection from the phone is outbound, perhaps to an intermediate server rather than the ultimate client pc. Also for usb connected situations, you can enable usb debugging on the phone and use an adb port forward to connect from the pc to phone.
Chris Stratton
A: 

Can you ping that IP address from that client?

EJP