마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

소스해석 부탁드립니다요 3333

–6 추천
    public void Click_getIP(View view) {



        try {

            text.setText("Get IP: " + getIpAddress().toString());



            //throw new RuntimeException("No network connections found.");

        } catch (Exception ex) {

        }



    }

    public void Click_getbroadcastIP(View view) {

        //InetAddress addr1 = getBroadcastAddress();



        try {

            text1.setText("Broadcast IP: " + getBroadcastAddress().toString());

        }catch (IOException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        };

    }

    private String getIpAddress() {

        String ip = "";

        try {

            Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface.getNetworkInterfaces();

            while (enumNetworkInterfaces.hasMoreElements()) {

                NetworkInterface networkInterface = enumNetworkInterfaces.nextElement();

                Enumeration<InetAddress> enumInetAddress = networkInterface.getInetAddresses();

                while (enumInetAddress.hasMoreElements()) {

                    InetAddress inetAddress = enumInetAddress.nextElement();

                    if (inetAddress.isSiteLocalAddress()) {

                        ip += inetAddress.getHostAddress() + "\n";

                    }

                }

            }

        } catch (SocketException e) {

            e.printStackTrace();

            ip += "Something Wrong! " + e.toString() + "\n";

        }

        return ip;

    }



        private InetAddress getBroadcastAddress() throws IOException {

            WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);

            DhcpInfo myDhcpInfo = myWifiManager.getDhcpInfo();

            if (myDhcpInfo == null) {

                System.out.println("Could not get broadcast address");

                return null;

            }

            int broadcast = (myDhcpInfo.ipAddress & myDhcpInfo.netmask)

                    | ~myDhcpInfo.netmask;

            byte[] quads = new byte[4];

            for (int k = 0; k < 4; k++)

                quads[k] = (byte) ((broadcast >> k * 8) & 0xFF);

            return InetAddress.getByAddress(quads);

        }















    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);

        return true;

    }





}
w=qwrf (70 포인트) 님이 2018년 9월 1일 질문

1개의 답변

0 추천
얼 른 사 라 져 ^^
익명사용자 님이 2018년 9월 3일 답변
...