'JAVA/JSP/Source'에 해당되는 글 36건

  1. 2008.07.14 InetAddress/ URL

InetAddress/ URL

|

----- 호스트 정보, URL정보 얻어오기 -----

import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;


class Iptest extends Frame implements ActionListener
{
   TextField tf;
   TextArea ta;
   Button bt,bt1,bt2;
   Panel p;
 

   URL url;
 

   InputStream is;
   BufferedReader br;
 
   InetAddress host;
 
 Iptest()
 {
    tf = new TextField();
    ta = new TextArea();
    p = new Panel();
    bt = new Button("호스트 정보 얻기");
    bt1 = new Button("URL 정보 얻기");
    bt2 = new Button("지우기");
 
 
    p.setLayout(new GridLayout(3,1));
 
    p.add(tf);
    p.add(bt);
    p.add(bt1);
 
    add("North",p);
    add("Center",ta);
    add("South",bt2);
 
    setSize(300,300);
    setVisible(true);
 
    bt.addActionListener(this);
    bt1.addActionListener(this);
    bt2.addActionListener(this);
    tf.addActionListener(this);
 
    addWindowListener(new WindowAdapter()
     {
        public void windowClosing(WindowEvent e)
        {
           System.exit(0);
        }
     });
 
   }
 
 

public void actionPerformed(ActionEvent e)
 {
    String hs,ip, ur;
    try{
         if(e.getSource() == bt || e.getSource() == tf)    //Host정보 얻어오기
       {
 
          host = InetAddress.getByName(tf.getText());  // InetAddress객체 생성
 
          hs = host.getHostName();   // InetAddress객체에서 hostname

                                               // (도메인 네임)을 얻어옴
   
          ta.append(hs+"\n");
   
          ta.append(host.getHostAddress()+"\n");//InetAddress객체에서IP를얻어옴
       }
       else if(e.getSource() == bt1)                  // URL정보 얻어오기
       {
          url = new URL(tf.getText());       // URL객체 생성
          is = url.openStream();              // url로부터 InputStream 객체 얻어오기

          br = new BufferedReader(new InputStreamReader(is));
 
          while(br.readLine() != null)       // 출력
          {
              ur = br.readLine();
              ta.append(ur+"\n");
          }
     }
     else if(e.getSource() == bt2)
     {
         ta.setText(null);
         tf.setText(null);
     }
   
     }catch(Exception a)
    {}
   
   }


 public static void main(String[] args)
 {
      new Iptest();
 }
}


'JAVA/JSP > Source' 카테고리의 다른 글

간단한 서버/클라이언트  (0) 2008.07.14
Socket/ ServerSocket  (0) 2008.07.14
Stream  (0) 2008.07.14
Image  (0) 2008.07.14
Thread  (0) 2008.07.14
And