Android 怎么获取手机获取客户端的ip地址址

博客分类:
获取当前设备的IP地址
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ip = intToIp(ipAddress);
public String intToIp(int i) {
return ((i && 24 ) & 0xFF ) + "." +
((i && 16 ) & 0xFF) + "." +
((i && 8 ) & 0xFF) + "." +
( i & 0xFF) ;
浏览: 752161 次
来自: 上海
写到最后一种文件才看到这个,洼的一声哭了出来
直接粘贴别人帖子还是英文的,有意思?
请问下,这些超级命令,是否需要android root权限,尤 ...
过来学习一下 。谢谢博主=- =-
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'在本机上启动两个模拟器,本机(PC)和模拟器的信息如下:
大家可以看到,两个模拟器的IP地址都是完全一样的,所以要实现两个模拟器之间的通信,使用模拟器的IP地址是办不到的。
获取模拟器名称:
&adb devices
List of devices attached
emulator-5554 device
emulator-5556 device
模拟器提供了一个特殊的IP,此IP的地址为10.0.2.2,此IP地址可以说等同于PC本机的IP地址127.0.0.1 。所以,通过此特殊IP地址,实现PC本机与模拟器的通信是没有问题。
【PC-SERVER / 模拟器-CLIENT】
目的:通过7100端口跟服务器进行通信。
实施方法比较简单,直接按照下面的方法就可以了。
PC-SERVER:ServerSocket server = new ServerSocket(7100);
模拟器-CLIENT:Socket socket = new Socket("192.168.1.3", 7100);
PC-SERVER:ServerSocket server = new ServerSocket(7100);
模拟器-CLIENT:Socket socket = new Socket("10.0.2.2", 7100);
【模拟器-SERVER / PC-CLIENT】
目的:连接到服务器的7100端口。
实施操作稍微复杂一些,不能按照前面的方法来进行。要达到此目的,先要进行端口映射。
把本机端口TCP/6100映射到模拟器TCP/7100端口(UDP也是一样)。
&adb –s emulator-5554 forward tcp:6100 tcp:7100
端口映射成功后,再进行下面的实验。
方法1(正确):
模拟器-SERVER:ServerSocket server = new ServerSocket(7100);
PC-CLIENT:Socket socket = new Socket("127.0.0.1", 6100);
方法2(错误):
模拟器-SERVER:ServerSocket server = new ServerSocket(7100);
PC-CLIENT:Socket socket = new Socket("192.168.1.3", 6100);
错误原因是127.0.0.1是本机环路IP,跟本机IP没有映射关系,所以发送到本机IP的连接请求,无法映射到127.0.0.1的环路IP上。
由于这个原因,两台PC机上的模拟器之间,是无法进行网络连接的。如果要实现连接,只能在作为服务器的PC上,运行一个用于数据中继的后台程序,进行数据的转发。
【模拟器(5554)-SERVER / 模拟器(5556)-CLIENT】
目的:模拟器(5556)连接到模拟器(5554)的7100端口。
实施操作也相对复杂一些。要达到此目的,也要先进行端口映射。
把本机端口TCP/6100映射到模拟器(5554)TCP/7100端口(UDP也是一样)。
&adb –s emulator-5554 forward tcp:6100 tcp:7100
端口映射成功后,再进行下面的实验。
方法1(正确):
模拟器(5554)-SERVER:ServerSocket server = new ServerSocket(7100);
模拟器(5556)-CLIENT:Socket socket = new Socket("10.0.2.2", 6100);
原理说明:
模拟器(5556)通过连接10.0.2.2:6100,相当于连接本机(PC)的127.0.0.1:6100,由于本机IP的6100端口进行过端口映射,映射到模拟器(5554)的7100端口。所以,来自模拟器(5556)的连接请求会发送到模拟器(5554)上。实现了两个模拟器之间的网络通信功能。
此方法在本机调试时非常有用。
方法2(错误):
模拟器(5554)-SERVER:ServerSocket server = new ServerSocket(7100);
模拟器(5556)-CLIENT:Socket socket = new Socket("192.168.1.3", 6100);
错误原因跟【PC-CLIENT / 模拟器-SERVER】的方法2一样。
Android模拟器Ping主机
众所周之Android模拟器采用的是Qemu,而整个模拟的内核是Linux,虽然没有x-windows界面和bash shell,但测试下基本的Linux Shell还是没有问题的,今天就在android emulator上玩回ping。
首先我们进入cmd或console状态下,使用cd命令进入Android SDK的Tools命令,可以输入adb shell这时候,这个时候从&变为了#,现在已经在linux shell中了,虽然Android平台没有vi这样的编辑器,但是执行些简单的命令比如cd、rmdir、mkdir还是没有问题,我们直接在Android模拟器中测试ping本机,如# ping localhost,看看返回如图:
[环境相关] 安卓模拟器ping局域网中的其他机器
获取android模拟器ip
public String getLocalIpAddress() {
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) {
edit_show.append(ex.toString()+"\n");
浏览: 147657 次
来自: 广西
问下,我本机有个html5的应用,我手机连在同一个无线路由器上 ...
看到那个引用信息了,但是没看懂,谢谢!
唉 jrebel.jar提示过期了
不错,我转了,以前对这个不懂搞了三天都没弄好啊,谢谢你了,支 ...
可是我的没有设置&Maximum version&q ...
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'网站已改版,请使用新地址访问:
HttpConnTest android获取本机终端公网ip地址及运营商
244万源代码下载- www.pudn.com
&文件名称: HttpConnTest& & [
& & & & &&]
&&所属分类:
&&开发工具: Java
&&文件大小: 8741 KB
&&上传时间:
&&下载次数: 11
&&提 供 者:
&详细说明:android获取本机终端公网ip地址及运营商-android terminal access to the local public network ip address and operators
文件列表(点击判断是否您需要的文件,如果是垃圾请在下面评价投诉):
&&HttpConnTest&&............\.classpath&&............\.project&&............\.settings&&............\.........\org.eclipse.jdt.core.prefs&&............\AndroidManifest.xml&&............\assets&&............\bin&&............\...\AndroidManifest.xml&&............\...\HttpConnTest.apk&&............\...\classes&&............\...\.......\com&&............\...\.......\...\example&&............\...\.......\...\.......\httpconntest&&............\...\.......\...\.......\............\BuildConfig.class&&............\...\.......\...\.......\............\HttpConnMainActivity$1.class&&............\...\.......\...\.......\............\HttpConnMainActivity$2.class&&............\...\.......\...\.......\............\HttpConnMainActivity.class&&............\...\.......\...\.......\............\R$attr.class&&............\...\.......\...\.......\............\R$dimen.class&&............\...\.......\...\.......\............\R$drawable.class&&............\...\.......\...\.......\............\R$id.class&&............\...\.......\...\.......\............\R$layout.class&&............\...\.......\...\.......\............\R$menu.class&&............\...\.......\...\.......\............\R$string.class&&............\...\.......\...\.......\............\R$style.class&&............\...\.......\...\.......\............\R.class&&............\...\classes.dex&&............\...\dexedLibs&&............\...\.........\android-support-v4-78e774cc046f23eb2acdb.jar&&............\...\.........\jsoup-1.4.1-3cd7aeed770c2bdc4dd968.jar&&............\...\jarlist.cache&&............\...\proguard.txt&&............\...\res&&............\...\...\drawable-hdpi&&............\...\...\.............\ic_launcher.png&&............\...\...\drawable-mdpi&&............\...\...\.............\ic_launcher.png&&............\...\...\drawable-xhdpi&&............\...\...\..............\ic_launcher.png&&............\...\...\drawable-xxhdpi&&............\...\...\...............\ic_launcher.png&&............\...\resources.ap_&&............\gen&&............\...\com&&............\...\...\example&&............\...\...\.......\httpconntest&&............\...\...\.......\............\BuildConfig.java&&............\...\...\.......\............\R.java&&............\ic_launcher-web.png&&............\libs&&............\....\android-support-v4.jar&&............\....\jsoup-1.4.1.jar&&............\netvideolive_wokspace&&............\.....................\.metadata&&............\.....................\.........\.lock&&............\.....................\.........\.log&&............\.....................\.........\.plugins&&............\.....................\.........\........\com.collabnet.subversion.merge&&............\.....................\.........\........\..............................\MergeResults&&............\.....................\.........\........\org.eclipse.cdt.core&&............\.....................\.........\........\....................\.log&&............\.....................\.........\........\org.eclipse.cdt.make.core&&............\.....................\.........\........\.........................\specs.c&&............\.....................\.........\........\.........................\specs.cpp&&............\.....................\.........\........\org.eclipse.cdt.managedbuilder.core&&............\.....................\.........\........\org.eclipse.core.resources&&............\.....................\.........\........\..........................\.history&&............\.....................\.........\........\..........................\.projects&&............\.....................\.........\........\..........................\.........\NetvideoLive&&............\.....................\.........\........\..........................\.........\............\.indexes&&............\.....................\.........\........\..........................\.........\............\........\a0&&............\.....................\.........\........\..........................\.........\............\........\..\11&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\5f&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\62&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\66&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\cd&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\d6&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\de&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\eb&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\..\ee&&............\.....................\.........\........\..........................\.........\............\........\..\..\properties.index&&............\.....................\.........\........\..........................\.........\............\........\properties.index&&............\.....................\.........\........\..........................\.........\............\.location&&............\.....................\.........\........\..........................\.........\............\.markers&&............\.....................\.........\........\..........................\.........\............\org.eclipse.jdt.apt.core&&............\.....................\.........\........\..........................\.........\............\org.eclipse.jdt.core&&............\.....................\.........\........\..........................\.........\............\....................\state.dat&&............\.....................\.........\........\..........................\.root&&............\.....................\.........\........\..........................\.....\.indexes&&............\.....................\.........\........\..........................\.....\........\history.version&&............\.....................\.........\........\..........................\.....\........\properties.index
&[]:很好,推荐下载
&近期下载过的用户:
&输入关键字,在本站244万海量源码库中尽情搜索:
&[] - ANDROID 手机投票器APP,可用于班级投票。使用SOCKET通信,分服务器和客户端两部分程序。
&[] - android 读取短信程序,java 语言编写,非常好用,可以作为gps 通信读取位置安卓 获取手机IP地址的实现代码
转载 & & 投稿:lqh
本篇文章主要介绍 Android 4.0 获取手机IP地址的方法,附有实现代码,具有参考价值,希望对有需要的小伙伴有帮助
1.获取手机IP地址的代码:
public static String getLocalIpAddress(){
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 e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
但是在4.0 下 会出现类似fe80::b607:f9ff:fee5:487e的IP地址, 这个是IPV6的地址,我们需要获得是的IPV4的地址,所以要在上诉代码中加一个判断
InetAddressUtils.isIPv4Address(inetAddress.getHostAddress());
2.完整代码如下:
public static String getLocalIpAddress(){
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() && InetAddressUtils.isIPv4Address(inetAddress.getHostAddress())) {
return inetAddress.getHostAddress().toString();
}catch (SocketException e) {
// TODO: handle exception
Utils.log("WifiPreference IpAddress---error-" + e.toString());
获取android手机当前ip地址
public class NetWorkUtils {
* 检查网络是否可用
* @param paramContext
public static boolean checkEnable(Context paramContext) {
boolean i =
NetworkInfo localNetworkInfo = ((ConnectivityManager) paramContext
.getSystemService("connectivity")).getActiveNetworkInfo();
if ((localNetworkInfo != null) && (localNetworkInfo.isAvailable()))
* 将ip的整数形式转换成ip形式
* @param ipInt
public static String int2ip(int ipInt) {
StringBuilder sb = new StringBuilder();
sb.append(ipInt & 0xFF).append(".");
sb.append((ipInt && 8) & 0xFF).append(".");
sb.append((ipInt && 16) & 0xFF).append(".");
sb.append((ipInt && 24) & 0xFF);
return sb.toString();
* 获取当前ip地址
* @param context
public static String getLocalIpAddress(Context context) {
// 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();
WifiManager wifiManager = (WifiManager) context
.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int i = wifiInfo.getIpAddress();
return int2ip(i);
} catch (Exception ex) {
return " 获取IP出错鸟!!!!请保证是WIFI,或者请重新打开网络!\n" + ex.getMessage();
Android中获取本机ip地址和MAC地址
通过InetAddress.getLocalHost()得到始终是“127.0.0.1”,要想得到真正的网络ip地址要通过下面的方法:
首先新建一个工程,修改AndroidManifest.xml文件增加用户权限,如下:
&uses-permission android:name="android.permission.INTERNET"/& //必写
&uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/&
&uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"&&/uses-permission&
&uses-permission android:name="android.permission.ACCESS_WIFI_STATE"&&/uses-permission&//必写
&uses-permission android:name="android.permission.CHANGE_WIFI_STATE"&&/uses-permission&
主要函数代码如下:
// 得到本机ip地址
public String getLocalHostIp()
String ipaddress = "";
Enumeration&NetworkInterface& en = NetworkInterface
.getNetworkInterfaces();
// 遍历所用的网络接口
while (en.hasMoreElements())
NetworkInterface nif = en.nextElement();// 得到每一个网络接口绑定的所有ip
Enumeration&InetAddress& inet = nif.getInetAddresses();
// 遍历每一个接口绑定的所有ip
while (inet.hasMoreElements())
InetAddress ip = inet.nextElement();
if (!ip.isLoopbackAddress()
&& InetAddressUtils.isIPv4Address(ip
.getHostAddress()))
return ipaddress = "本机的ip是" + ":" + ip.getHostAddress();
catch (SocketException e)
Log.e("feige", "获取本地ip地址失败");
e.printStackTrace();
// 得到本机Mac地址
public String getLocalMac()
String mac = "";
// 获取wifi管理器
WifiManager wifiMng = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
Android 获取wifi的IP地址
WifiManager wifimanage=(WifiManager)context.getSystemService(Context.WIFI_SERVICE);//获取WifiManager
//检查wifi是否开启
if(!wifimanage.isWifiEnabled()) {
  wifimanage.setWifiEnabled(true);
WifiInfo wifiinfo= wifimanage.getConnectionInfo();
String ip=intToIp(wifiinfo.getIpAddress());
//将获取的int转为真正的ip地址,参考的网上的,修改了下
private String intToIp(int i) {
  return (i & 0xFF)+ "." + ((i && 8 ) & 0xFF)? + "." + ((i && 16 ) & 0xFF) +"."+((i && 24 ) & 0xFF );
OK,这样就好了吗?呵呵,别忘记加上权限&
&uses -permission="" android:name="android.permission.ACCESS_WIFI_STATE"&&/uses&&
&uses -permission="" android:name="adnroid.permission.CHANGE_WIFI_STATE"&&/uses&
&&总结:大家可以对比一下,Android 获取手机 IP&地址的方法,以免在编程的过程中造成不必要的问题.
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具&nbsp>&nbsp
&nbsp>&nbsp
&nbsp>&nbsp
Android 怎么获取手机端的ip地址
摘要:在和服务器互动的时候,常常会用到客户端ip地址。当然,在服务器端可以获取请求过来的ip,在手机端,怎么获取自己的ip呢?请参阅下面的函数:[java]publicstaticStringGetHostIp(){&&&&&&try{&&&&&&&&for(Enumeration&NetworkInterface&en=Netwo
在和服务器互动的时候,常常会用到客户端ip地址。当然,在服务器端可以获取请求过来的ip,在手机端,怎么获取自己的ip呢?请参阅下面的函数:
[java] public static String GetHostIp() {&&&&&& try {&&&&&&&& for (Enumeration&NetworkInterface& en = NetworkInterface&&&&&&&&&&&&&&&& .getNetworkInterfaces(); en.hasMoreElements();) {&&&&&&&&&&&& NetworkInterface intf = en.nextElement();&&&&&&&&&&&& for (Enumeration&InetAddress& ipAddr = intf.getInetAddresses(); ipAddr&&&&&&&&&&&&&&&&&&&& .hasMoreElements();) {&&&&&&&&&&&&&&&& InetAddress inetAddress = ipAddr.nextElement();&&&&&&&&&&&&&&&& if (!inetAddress.isLoopbackAddress()) {&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& return inetAddress.getHostAddress();&&&&&&&&&&&&&&&& }&&&&&&&&&&&& }&&&&&&&& }&&&& } catch (SocketException ex) {&&&& } catch (Exception e) {&&&& }&&&&&}&&public static String GetHostIp() {&&&try {&&&for (Enumeration&NetworkInterface& en = NetworkInterface&&&&&.getNetworkInterfaces(); en.hasMoreElements();) {&&&&NetworkInterface intf = en.nextElement();&&&&for (Enumeration&InetAddress& ipAddr = intf.getInetAddresses(); ipAddr&&&&&&.hasMoreElements();) {&&&&&InetAddress inetAddress = ipAddr.nextElement();&&&&&if (!inetAddress.isLoopbackAddress()) {&&&&&&&&&&&&return inetAddress.getHostAddress();&&&&&}&&&&}&&&}&&} catch (SocketException ex) {&&} catch (Exception e) {&&}&&&}
其实,上面的方法通过java.net下的相关类获取ip的。主要用到的类有:java.net.NetworkInterface和java.net.InetAddress摘自 心灵净土
以上是的内容,更多
的内容,请您使用右上方搜索功能获取相关信息。
若你要投稿、删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内给你回复。
云服务器 ECS
可弹性伸缩、安全稳定、简单易用
&40.8元/月起
预测未发生的攻击
&24元/月起
为您提供0门槛上云实践机会
你可能还喜欢
你可能感兴趣
阿里云教程中心为您免费提供
Android 怎么获取手机端的ip地址相关信息,包括
的信息,所有Android 怎么获取手机端的ip地址相关内容均不代表阿里云的意见!投稿删除文章请联系邮箱:zixun-group@service.aliyun.com,工作人员会在五个工作日内答复
售前咨询热线
支持与服务
资源和社区
关注阿里云
International

我要回帖

更多关于 手机端h5获取ip地址 的文章

 

随机推荐