android 通过WIFI 实现手机间的Socket通信

我的学生时代 2016-06-05 08:31:36
[客户端:android手机客户端——服务端:android手机服务端]
我的问题是:在实现过程中,无法实现Socket的简单连接。
我看到的现象是创建wifi的手机获取本地IP——服务端IP:
InetAddress inetAddress = InetAddress.getLocalHost();


和我客户端所获取的服务端DHCP
DhcpInfo dhcpInfo = wifiManager.getDhcpInfo();
int iDhcp = dhcpInfo.gateway;

是不同的

我就不知道是怎么回事了
...全文
617 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
我的学生时代 2016-06-08
  • 打赏
  • 举报
回复

/**
 杨铭 Created by kys_8 on 16/6/5,0005. <p>Email:771365380@qq.com</p> <p>Mobile phone:15133350726</p>
 */
public class OurIP
{

    /** 得到本机所有IP地址 */
    public void getLocalHostIP()
    {
        Enumeration<NetworkInterface> netInterfaces = null;
        try
        {
            netInterfaces = NetworkInterface.getNetworkInterfaces();
        }
        catch (SocketException e)
        {
            e.printStackTrace();
        }
        while (netInterfaces.hasMoreElements())
        {
            NetworkInterface nif = netInterfaces.nextElement();
            Enumeration<InetAddress> iparray = nif.getInetAddresses();
            while (iparray.hasMoreElements())
            {
                InetAddress ip = iparray.nextElement();
                System.out.println(ip.getHostAddress());
            }
        }
    }

    // 得到本机Mac地址
    private String getLocalMac()
    {
        String mac = "";
        // 获取wifi管理器
        WifiManager wifiMng = (WifiManager) Global.connectActivity
                .getSystemService(Context.WIFI_SERVICE);
        WifiInfo wifiInfor = wifiMng.getConnectionInfo();
        mac = "本机的mac地址是:" + wifiInfor.getMacAddress();
        return mac;
    }

    /** 下面的方法就是从这个方法改来的 */
    private void getApIp_source()
    {
        StringBuilder IFCONFIG = new StringBuilder();
        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() && !inetAddress
                            .isLinkLocalAddress() && inetAddress.isSiteLocalAddress())
                    {
                        IFCONFIG.append(inetAddress.getHostAddress().toString() + "\n");
                    }

                }
            }
        }
        catch (SocketException ex)
        {
        }
        System.out.println(IFCONFIG);
    }

    /** 获取自定义AP的IP */
    public InetAddress getApIp()
    {
        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.isSiteLocalAddress() && inetAddress.getAddress()[0] < 0)
                    {//对于byte 如果想找到一个在0-255之间的大于byte.max  的那就要设置条件 byte < 0
                        return inetAddress;
                    }
                }
            }
        }
        catch (SocketException ex)
        {
            ex.printStackTrace();
        }
        _Log.e(_Log.msg() + "暂时没有得到AP地址··稍候");
        return null;
    }
蒲锦_up 2016-06-06
  • 打赏
  • 举报
回复
小白在此 2016-06-06
  • 打赏
  • 举报
回复
你的两个手机是通过wifi直连吗?所谓的直连就是一个手机开热点,另一个手机连上这个热点,这样你的客户端手机获取的网关肯定是服务端手机的本地ip啊,如果你两台手机连的是一个路由器的话那肯定就不一样了。当然,还有一种可能,那就是你服务端获取本机ip的方式不正确,获取的是外网的ip,而不是开启热点后自己的内网ip,这只是一种思路,你可以逐一排查。

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧