1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import java.net.*;
import java.io.*;
 
public class Ex02_01 
{
    public static void main(String[] args)
    {
        try
        {
            //InetAddress ia = new InetAddress(); 처럼 하면 좋겠지만 
            //InetAddress 는 생성자 자체가 존재하지 않기때문에 쓸수없다        
            InetAddress[] ia = InetAddress.getAllByName("www.naver.com");    //여러개의 주소를 얻는방법
            
            for(int i=0; i < ia.length ; i++)
            {
                System.out.println(i + 1 + "번째의 주소 => " + ia[i].getHostName() +"   아이피 => "+ia[i].getHostAddress());
            }
        }
        catch(UnknownHostException e){}
        
    }
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.net.*;
import java.io.*;
 
public class Ex02_02 
{
    public static void main(String[] args)
    {
        try        //외부주소 알아내기
        {
            InetAddress ia = InetAddress.getByName("www.naver.com");    //주소 한개만 알아내기            
            System.out.println(ia.getHostAddress());
            System.out.println(ia.getHostName());            
        }
        catch(UnknownHostException e){}
        
        try        // 내컴 주소 알아내기
        {
            InetAddress iaa = InetAddress.getLocalHost();
            System.out.println(iaa);
        }
        catch(UnknownHostException e){}
        
    }
}




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.net.*;
import java.io.*;
 
public class Ex02_03 
{
    public static void main(String[] args)
    {
        try
        {
            InetAddress ia = InetAddress.getByName("www.naver.com");
            byte[] bb = ia.getAddress();        //getAddress를 쓰려면 배열이기 때문에 배열로 넣어줘야된다
                                                //getAddress는 특정객체로부터 배열형태의 IP주소를 빼내어
                                                //byte형식으로 형변환
//            for(int i = 0; i < bb.length ; i++)
//            {
//                System.out.println(bb[i]);
//            }
            
            for(int ggg : bb)
            {
                System.out.print(ggg+"\t");        //125,-47,-34,-115 현재는 이렇게 나온다
                                                //byte 형으로 변환한 번호이다
            }
            
            
            byte[] vvv={125,-47,-34,-115};        //위에서 나온 숫자를 getByAddress 를 넣으면 IP주소가 나온다
                                                //125.209.222.141
            InetAddress iaa = InetAddress.getByAddress(vvv);
            System.out.println("iaa = "+iaa);
            System.out.println("dns = "+iaa.getHostName());
            System.out.println("ip = "+iaa.getHostAddress());
        }
        catch(UnknownHostException e){}
    }
}





1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * awt이용 
 * A.프레임을 통한 현재 컴퓨터 정보 표시
 * B.프레임에서 특정 대상컴퓨터의 정보 표시 예제
 */

 
import java.net.*;
import java.awt.*;
import java.awt.event.*;
 
class Ex02_04Sub extends Frame implements ActionListener    //Frame을 상속받고 Action이벤트를 쓰기위해  implements 로 
{
    //사용할 객체 생성
    private Label lb = new Label("사이트 : ", Label.RIGHT);    //
    private TextField tf = new TextField();        //입력창으로 이용할 객체 생성
    private TextArea ta = new TextArea();        //결과창으로 사용할 객체생성
    private Button bt = new Button("Clear");    //Clear라는 이름을 가진 버튼 객체 생성
    private Button bt1 = new Button("End");        //End라는 이름을 가진 버튼 객체 생성
    
    
    public Ex02_04Sub(String str)
    {
        super(str);
        this.init();
        this.start();
        this.setSize(300,200);
        this.setVisible(true);        //보이도록 세팅할것이냐~!!!
    }
    
    public void init()        //위에서 만든 사용할 객체 생성한것으로 초기 화면 구성 Swing
    {
        this.setLayout(new BorderLayout());            //Layout 응 BorderLayout으로 세팅
        this.add("Center", ta);                        //Center 에 TextArea를 생성
        Panel p =new Panel(new BorderLayout());        //Label 과 TextField를 만들기 위한 Panel을 BorderLayout으로 생성
        p.add("West", lb);                            //왼쪽에 Label객체를 p에 생성
        p.add("Center", tf);                        //가운데에 TextField객체 p에 생성
        this.add("North",p);                        //위쪽에 p를 화면에 생성
        Panel p1 = new Panel(new FlowLayout(FlowLayout.RIGHT));        //Panel 생성을 FlowLayout형식의 오른쪽으로 생성
        p1.add(bt);        //p1에 bt객체 생성만
        p1.add(bt1);    //p1에 bt1 객체 생성만
        this.add("South", p1);    //이화면에 p1 을 추가후 아래쪽으로 출력
    }
    
    public void start()        //시작메소드 
    {
        tf.addActionListener(this);        //textField 에서 이벤트 발생
        bt.addActionListener(this);        //clear 버튼 이벤트 발생
        bt1.addActionListener(this);    //End 버튼 이벤트 발생 보낸다
    }
 
    @Override
    public void actionPerformed(ActionEvent e)     //이벤트를 사용하기위한 메소드 생성
    {
        if(e.getSource() == tf)        //ActionEvent 를 받았는데 내용이 tf면
        {
            String str = tf.getText().trim();     //str에 textField에 있는 내용을 가져옴
            
            if(str == null || str.length() == 0)         //str에 null 이거나 str의 길이가 아무것도 없다면
            {
                return;                                    //그냥 반환한다
            }
            try
            {    
                InetAddress[] ia = InetAddress.getAllByName(str);
                //내 컴퓨터 정보출력
                InetAddress ia1 = InetAddress.getLocalHost();        //로컬의 IP주소를 얻어서 ia1 에 넣는다
                ta.append("Localhost name : " + ia1.getHostName()+"\n");    //textArea에 ia1의 호스트이름을 추가한다
                ta.append("Localhost Address : "+ia1.getHostAddress()+ "\n\n");    //textArea에 가져온 hostAddress를 추가한다.
                
                //원격 컴퓨터 정보출력                
                for(int i=0; i< ia.length; i++)
                {
                    ta.append("Remotehost[] name : " + ia[i].getHostName()+"\n");    //textArea에 ia의 호스트이름을 추가한다
                    ta.append("Remote Address[] : "+ia[i].getHostAddress()+ "\n\n");    //textArea에 가져온 hostAddress를 추가한다.
                    
                    //ta.setText(""); setText는 앞에껄 지우고 덮어쓰기하는것이다. 
                }
            }
            catch(UnknownHostException ee)
            {
                ta.setText("Ereer = "+ee.toString());
                return;
            }
        }
        else if(e.getSource() == bt)        //ActionEvent 가 bt이면
        {
            tf.setText("");        //textField를 공백으로 바꾼다
            ta.setText("");        //textAria를 공백으로 바꾼다
            tf.requestFocus();    //textField 로 커서를 놓는다
        }
        else if(e.getSource() == bt1)
        {
            System.exit(0);
        }
        
    }
}
 
public class Ex02_04 
{
    public static void main(String[] args)
    {
        Ex02_04Sub es = new Ex02_04Sub("ip관리하기");        //Frame으로 상송받은 Ex02_04Sub 으로 
                                                        //이름이 ip관리하기로 객체 생성
    }
}
 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

 
import java.net.*;
import java.io.*;
 
public class Ex02_05 
{
    public static void main(String[] args)
    {
        InetAddress ia = null;
        Socket soc = null;
        OutputStream os = null;
        InputStream is = null;
        try
        {
            ia = InetAddress.getByName("www.naver.com");            
        }
        catch(UnknownHostException ee){}
        try
        {
            soc = new Socket(ia,80);    //Socket을 이용하여 ia=주소,80=포트번호로 연결
//            soc = new Socket("www.naver.com",80); //으로도 사용가능
            
            os = soc.getOutputStream();
            is = soc.getInputStream();
            /*
             OutputStreamWriter osw =new OutputStreamWriter(os);
             BufferedWriter bw = new BufferedWriter(osw);
             PrintWriter pw = new PrintWriter(bw);
             
             pw.println("test");   // 이건 지정한 사이트로 전송된다
             pw.flush();
             */
        }
        catch(IOException ee){}
        System.out.println("soc = "+soc);
        System.out.println("os = "+ os);
        System.out.println("is = "+ is);
    }
}
 




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

 
// 웹페이지 긁어오기
 
import java.net.*;
import java.io.*;
 
public class Ex02_06 
{
    public static void main(String[] args)
    {
        InetAddress ia = null;        //사이트 주소 저장하기위한 준비
        Socket soc = null;            //네트워크를 처리할 수 있는 소켓 객체 생성        
//        DataOutputStream dos = null;        //이렇게 쓰는방법이있다
//        DataInputStream dis = null;
        OutputStream os = null;
        InputStream is =null;
        
        try
        {
            ia = InetAddress.getByName("www.naver.com");
        }
        catch(UnknownHostException ee){}
        
        try
        {
            soc = new Socket(ia,80);            //포트를 열어주고
            
            /*    //이렇게 쓰는 방법이있다
             * dos =new DataOutputStream(            //출력형식
                 new BufferedOutputStream(
                    soc.getOutputStream()));
            dis = new DataInputStream(            //입력형식    
                  new BufferedInputStream(
                  soc.getInputStream()));
             */
            os = soc.getOutputStream();
            is = soc.getInputStream();
            
                
            
            String str = "get http://www.daum.net/index.html http1.0 \r\n\n";
                    //get 방식           full 주소                    버전       엔터두번
            os.write(str.getBytes());    //서버에게 이야기 한다 write 메소드는 byte밖에 보낼수 없다.str을 Byte방식으로 분할 전달 한다.
            while(true)    //이제 받아서 화면상에 보여줘야된다
            {        
                int xx = is.read();        //soc.getInputStream(); 에서 받은 내용을 xx에 입력
                if(xx == -1)            //더이상 내용이 없으면
                {
                    break;                
                }
                System.out.print((char)xx);
            }
            os.close();
            is.close();
            soc.close();
            
            
            
        }
        catch(IOException ee){}
    }
}
 







Posted by 앗뜨거워 ( @bok3937 ) 앗뜨거