2

I have Android client app that communicates with server using Socket.

On my development machine, SocketServer (server is also written in Java) is listening at 127.0.1.1 at port 8666. I also have added Internet access permissions in App manifest of Android app.

But when I attempt to instantiate Socket object using client = new Socket("127.0.1.1",8666), it throws IOException with connection refused message (also tried using localhost instead of manually giving IP address, but it doesn't work as well). While I can easily connect to server from simple console Java program.

What could be wrong here?

Update

My question in some ways relates to this. I AM attempting to access a server running in the machine same in which emulator is running.

Note: I know its not recommended to perform network IOs in any Activity class, and I'm not doing that way either, above code is just a problem simplified to understand.

Community
  • 1
  • 1
Kushal
  • 3,112
  • 10
  • 50
  • 79
  • Try system's IP Address, or 10.0.2.2 instead of 127.0.1.1 in your android application. – user370305 Jun 28 '12 at 12:27
  • Is your Android code running on an emulator or an actual phone? – Neal Jun 28 '12 at 12:28
  • Wireshark is your friend in such situations. Record the traffic and check what is wrong. Double check ip address and port number. – Zuljin Jun 28 '12 at 12:28
  • @Neal: I'm running app in Emulator. – Kushal Jun 28 '12 at 12:30
  • @user370305: Tried using system's IP address, not working. How can I identify exact IP for laptop in context to 10.0.x.x? – Kushal Jun 28 '12 at 12:33
  • Actually, use your system's IP address and in your android application's manifest file put this permission.. **** – user370305 Jun 28 '12 at 12:39
  • @user370305: I have permissions already added to manifest. And also tried using system's IP address (as assigned by ISP), but that didn't worked either. – Kushal Jun 28 '12 at 12:41
  • From command prompt at adb location, **adb forward tcp:8666 tcp:8666** If this doesn't help then post your code and logcat errors.. – user370305 Jun 28 '12 at 12:44
  • @user370305: Cheers!! that command fixed the issue, while I was using `10.0.2.2` at client socket.!! post it as answer, also it would be great if you can explain what that command actually did. – Kushal Jun 28 '12 at 12:49
  • @user370305: Looks like Emulator has occupied the port `8666` since when I ran the command (with emulator open), I didn't knew that I stopped the app. server, now the port is occupied and I can't use it. :-/ – Kushal Jun 28 '12 at 12:54
  • 1
    I posted my answer. Happy Coding..! – user370305 Jun 28 '12 at 12:55

3 Answers3

3

Try using, Port Forwarding

From command prompt at adb location,

adb forward tcp:8666 tcp:8666

The forward command to set up arbitrary port forwarding — forwarding of requests on a specific host port to a different port on an emulator/device instance. Here's how you would set up forwarding of host port 8666 to emulator/device port 8666:

If this doesn't help then post your code and logcat errors.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Okay, every thing (App Server and Emulator) needed a restart after running the command, now everything's working fine. Thanks a lot..!! – Kushal Jun 28 '12 at 12:58
0

Is your Android code running in an emulator or on real hardware over WiFi? If you're running in an emulator, the IP address for your development machine would probably be 10.0.2.2 (see Android Developer Tools: Emulator).

If your app is running on an Android phone connecting over WiFi, you have to specify your machine's IP address, not the loopback address.

Also, when you say your SocketServer is listening at 127.0.1.1, are you binding to that interface? You shouldn't bind to the loopback if you're expecting connections from other interfaces.

Orlymee
  • 2,349
  • 1
  • 22
  • 24
Neal
  • 6,722
  • 4
  • 38
  • 31
  • I'm running app in Emulator, tried using `10.0.2.2`, that doesn't work either. – Kushal Jun 28 '12 at 12:34
  • Just made an update about binding the interface on your server. If you're listening only on loopback, the phone won't be able to connect. – Neal Jun 28 '12 at 12:35
  • I have not manually bound server Socket to listen at 127.0.1.1, when my server is started, it writes the log at which IP and port it is listening to incoming requests, and I don't know why but it sometimes takes `127.0.0.1` and sometimes `127.0.1.1`. – Kushal Jun 28 '12 at 12:38
  • You can try manually specifying the correct IP address in your bind then so it doesn't bind the loopback. If you don't specify an interface, it should listen on all interfaces though... – Neal Jun 28 '12 at 12:40
  • How do I make it listen at all interfaces? – Kushal Jun 28 '12 at 12:44
0

If I got it right: your socket server is running on a separate machine. You cannot use 127.0.0.1 to connect to that server through your Android phone as that is the local loop back address. Find your machine's IP address and use that instead.

In case of using it on the emulator when you try to connect to the socket server running on your machine you still cannot use 127.0.0.1 as that is local loop back address for the Emulator.

Orlymee
  • 2,349
  • 1
  • 22
  • 24
  • Both emulator and Socket server are running on same physical machine, I tried using system's IP address instead of loopback but still the problem persists. – Kushal Jun 28 '12 at 12:42
  • @Kush: try to understand that localhost & 127.0.0.1 are local to both the machine and the emulator. So your machine has its own loopback address and so does the emulator. Use 10.0.2.2. see this SO Post http://stackoverflow.com/questions/2234742/test-the-localhost-in-android-emulator if it still does not work make sure that firewall is not preventing access to that socket. – Orlymee Jun 28 '12 at 12:45