2

What is 'localhost' address for Android device?

I'm testing a program using client and server at the same app.

neither SERVER_IP ="http://localhost" nor "http://127.0.0.1" works for

InetAddress serverAddr = InetAddress.getByName(SERVER_IP);
socket = new Socket(serverAddr, SERVERPORT);

it gives:

Unable to resolve host "http://localhost": No address associated with hostname android

What is the best way to use localhost?

UPDATE Thanks to all comments: "http://localhost" is incorrecct. Right is "localhost" or "127.0.0.1" or "127.0.0.2", etc. But I hope the best solution is the marked answer.

Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • 2
    possible duplicate of [How can i access my localhost from my android device?](http://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device) – Adam S Oct 08 '14 at 18:32
  • 1
    try 127.0.2.2. let me know and are you testing the client on emulator? – ChanChow Oct 08 '14 at 18:34
  • @AllIsWell no. 127.0.0.2 doesn't work on real device. – Vyacheslav Oct 08 '14 at 18:37
  • @AdamS thanks. I saw this answer. But this is not what I want. – Vyacheslav Oct 08 '14 at 18:37
  • Ugh, yeah I tried to un-mark that since I realised it was the wrong thing and I couldn't figure out how. – Adam S Oct 08 '14 at 18:39
  • @trololo 127.0.2.2 is the one you tried or 127.0.0.2? – ChanChow Oct 08 '14 at 18:40
  • For clarification, are you attempting to _run a server on the device_ and _connect to it on the same device_? That's totally different to most localhost questions that pop up on here, I'd recommend updating your question to be a little clearer about that, if that's what you're doing. – Adam S Oct 08 '14 at 18:42
  • 1
    `SERVER_IP ="http://localhost" nor "http://127.0.0.1"`. Don't specify a protocol and surely not http. Try `SERVER_IP ="localhost" or "127.0.0.1"`. And indeed, is this in the same app or on the same device? – greenapps Oct 08 '14 at 19:08
  • @greenapps I found that without http:// all IPs before works: 127.0.0.1, 127.0.2.2, localhost. In all of cases above the s.getRemoteSocketAddress().toString() gives "127.0.0.1" – Vyacheslav Oct 09 '14 at 03:45
  • @greenapps the same app and the same device. – Vyacheslav Oct 09 '14 at 03:47

2 Answers2

3

Sockets communicate over TCP, which is a lower level protocol than HTTP. When you are identifying a server address for socket communication, use the bare name of the server (for example, "localhost") or a dotted quad (for example "192.168.0.100").

Don't include http:// or any other scheme.

If I understand the question correctly and the goal is to open a socket from and to the same machine (the phone), you can just use

InetAddress serverAddr = InetAddress.getByName(null);

to get the "loopback" interface, which is equivalent to using localhost.

x-code
  • 2,940
  • 1
  • 18
  • 19
  • This is what I want! Thanks a lot. Connection accepted. s.getRemoteSocketAddress().toString() gave me ip_port: /::1:44967 – Vyacheslav Oct 09 '14 at 03:25
1

According to this answer, you can simply use your local IP to act as localhost. You can find it by using ipconfig on Windows or ifconfig on linux.

Apparently you can also substitute 10.0.2.2

Community
  • 1
  • 1
RyPope
  • 2,645
  • 27
  • 51