zoukankan      html  css  js  c++  java
  • Android模拟器的ip获取以及模拟器之间socket通信

    http://my.oschina.net/xiahuawuyu/blog/81786

    1、Android  获取本机Mac 地址方法:

    需要在AndroidManifest.xml文件中添加权限:    

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  

    Java代码

    1
    2
    3
    4
    5
    public String getLocalMacAddress() { 
            WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE); 
            WifiInfo info = wifi.getConnectionInfo(); 
            return info.getMacAddress(); 
        }

    2、Android 获取本机IP地址方法

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    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("WifiPreference IpAddress", ex.toString()); 
            
            return null
        }

    --------------------------------------------------

    实现网络五子棋时用到了两个设备间的Socket通信,如果使用真机调试比较麻烦,用两个模拟器之间进行通信会比较方便。

    首先要获得的模拟器的IP地址

    在本机上启动两个模拟器,emulator-5554,emulator-5556查看模拟器ip

    在命令行窗口输入命令

    adb –s emulator-5554 shell

    getprop

    找到一下两行:

    [net.eth0.dns1]: [10.0.2.3]

    [net.gprs.local-ip]: [10.0.2.15]

    可以发现两个模拟器的IP地址完全一样,都为10.0.2.15,DNS都为10.0.2.3,所以要实现两个模拟器之间的通信,使用模拟器的IP地址是办不到的。

    模拟器提供了一个特殊的IP,这个IP地址为10.0.2.2,这个IP地址可以说等同于PC本机的IP地址127.0.0.1,所以,通过这个特殊的IP地址可以进行PC与模拟器之间的通信。

    考虑模拟器作为主机的情况:

    需要把模拟器的端口映射到PC的端口,通过连接PC端的端口来把请求重定向发送给,模拟器。

    1)PC作为客户端,模拟器1为主机

    2)模拟器2为客户端,模拟器1为主机

     

    映射PC端口到模拟器端口的命令如下:

    >adb –semulator-5554 forward tcp:6665 tcp:6666

    映射成功后,连接模拟器的端口6666就要通过连接PC的端口6665进行连接,客户端连接的IP必须为环路IP,不能使用本机IP,如192.168.1.102,因为本机IP跟环路IP没有映射关系,所以发送到本机IP(192.168.1.102)的连接请求无法映射到127.0.0.1的环路IP上,使用PC端程序作为客户端时,环路IP必须使用127.0.0.1,使用模拟器作为客户端时,环路IP使用10.0.2.2,虽然两者是同一个环路IP,但是PC只能识别127.0.0.1,模拟器只能识别10.0.2.2。

    3)如果要实现两个模拟器之间的相互通信,就必须每个模拟器都既为Server又为Client,并在PC机上映射为不同的端口。结构图如下

    由于PC的本机IP(192.168.1.102)没有映射到环路IP,所以不能在Android真机与模拟器之间或者运行在两台PC的模拟器之间进行Socket通信,如果要实现连接,必须在作为服务器的PC上,运行一个用于数据中继的后台程序,进行数据的转发。

  • 相关阅读:
    面试题:区分List中remove(int index)和remove(Object obj)
    Collection的子接口之一:List 接口
    面试题:ArrayList、LinkedList、Vector三者的异同?
    jdk 5.0 新增的foreach循环(用于遍历集合、数组)
    Iterator迭代器接口(遍历Collection的两种方式之一)
    哈希值
    Collection接口方法
    集合框架的概述
    注解(Annotation)
    System类、Math类、BigInteger与BigDecimal的使用
  • 原文地址:https://www.cnblogs.com/carl2380/p/4203496.html
Copyright © 2011-2022 走看看