Android Emulator의 IP 주소를 얻는 방법은 무엇입니까?
코드를 통해 현재 실행중인 Android Emulator 의 IP 주소 를 얻고 싶습니다 . 어떻게 이룰 수 있습니까?
명확히하기 위해 앱 내에서 에뮬레이터를 'localhost'또는 127.0.0.1로 간단히 참조 할 수 있습니다.
웹 트래픽은 개발 컴퓨터를 통해 라우팅되므로 에뮬레이터의 외부 IP는 공급자가 해당 컴퓨터에 할당 한 IP입니다. 개발 머신은 항상 장치에서 10.0.2.2로 연결할 수 있습니다.
에뮬레이터의 IP에 대해서만 질문했기 때문에 무엇을하려고합니까?
에뮬레이터에 IP를 할당하려면 다음을 수행하십시오.
adb shell
ifconfig eth0
다음과 같은 결과를 얻을 수 있습니다.
eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]
이렇게 :
public String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
자세한 정보는 문서를 확인하십시오 : NetworkInterface .
이 방법을 사용하면 Android 에뮬레이터에 대해 100 % 정확한 IP 주소를 얻게됩니다.
yoor 에뮬레이터의 IP 주소를 얻으려면
adb 쉘로 이동하여 다음 명령을 입력하십시오.
adb shell
ifconfig eth0

이 명령을 실행 한 후
IP : 10.0.2.15
마스크 : 255.255.255.0
나를 위해 작동합니다. 네트워킹 응용 프로그램에서도 일하고 있습니다.
에뮬레이터 클라이언트가 동일한 호스트에서 실행중인 서버에 연결하도록하려는 경우와 같이 호스트 컴퓨터의 로컬 호스트를 참조해야하는 경우 별칭 10.0.2.2 를 사용하여 호스트 컴퓨터의 루프백 인터페이스를 참조합니다. 에뮬레이터의 관점에서, 로컬 호스트 (127.0.0.1)가 자신의 루프백 interface.More 세부 사항을 참조 : http://developer.android.com/guide/faq/commontasks.html#localhostalias
public String getLocalIpAddress() {
try {
for (Enumeration < NetworkInterface > en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration < InetAddress > enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
}
return null;
}
참고 URL : https://stackoverflow.com/questions/1720346/how-to-get-the-android-emulators-ip-address
'program story' 카테고리의 다른 글
| 장난감 프로젝트에 설정하고 사용하기 가장 쉬운 버전 관리 시스템은 무엇입니까? (0) | 2020.11.06 |
|---|---|
| Hibernate 또는 JDBC (0) | 2020.11.06 |
| 로그인 실패로 인해 SQL Server 2012를 시작할 수 없습니다. (0) | 2020.11.06 |
| Ubuntu 12.04에서 Python.h 누락 (0) | 2020.11.06 |
| 사용자가 Google recaptcha에서 확인란을 선택한 js를 확인하는 방법은 무엇입니까? (0) | 2020.11.06 |