처음부터 시작하는 Java
  • InetAddress 클래스
    2021년 04월 05일 10시 07분 30초에 업로드 된 글입니다.
    작성자: 원2
    728x90
    반응형

    주소는 계속 바뀐다.
    ip = InetAddress.getLocalHost();

    import java.net.*;
    import java.util.*;
    
    public class Ex16_1 {
    
    	public static void main(String[] args) {
    		InetAddress ip = null; // 객체참조변수 null
    		InetAddress[] opArr = null;
    
    		try {
    			ip = InetAddress.getByName("www.naver.com"); // naver로 객체를 생성
    			System.out.println("getHostName() : " + ip.getHostName());
    			System.out.println("getHostAddress() : " + ip.getHostAddress());
    			System.out.println("toString() : " + ip.toString());
    
    			byte[] ipAddr = ip.getAddress();
    			System.out.println("getAddress() : " + Arrays.toString(ipAddr)); // 2의 보수방식
    
    			String result = "";
    			for (int i = 0; i < ipAddr.length; i++)
    				result += (ipAddr[i] < 0 ? ipAddr[i] + 256 : ipAddr[i]) + ".";
    			// 음수가 나올때만 256을 더한것.
    			System.out.println("getAddress()+256 :" + result); // 한개씩 찍어본것
    			System.out.println();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
    		try {
    			ip = InetAddress.getLocalHost();
    			System.out.println("getHostName() : " + ip.getHostName());
    			System.out.println("getHostAddress() : " + ip.getHostAddress());
    			System.out.println();
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
    		try {
    			InetAddress[] ipArr = InetAddress.getAllByName("www.naver.com");
    
    			for (int i = 0; i < ipArr.length; i++)
    				System.out.println("ipArr[" + i + "] : " + ipArr[i]);
    		} catch (UnknownHostException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    ip = InetAddress.getLocalHost();

     

    728x90
    반응형

    'Language > Java' 카테고리의 다른 글

    패키지로 묶어서 소켓 돌리기  (0) 2021.04.05
    TCP Socket프로그래밍  (0) 2021.04.05
    TCP UDP  (0) 2021.04.05
    교착상태  (0) 2021.04.02
    interrupt  (0) 2021.04.02
    JOptionPane  (0) 2021.04.02
    댓글