2011년 6월 9일 목요일

Java NetWork - 2

4. 인터넷 주소 처리
InetAddress Class
① package network.net;
② import java.net.*;
④ public class InetAddressMain {
⑤ public InetAddressMain() {
⑥ }
⑧ private void exec(String[] Domain) {
⑨ try {
⑩ for( int leng = 0; leng < Domain.length; leng++) {
⑪ InetAddress inetaddress[] = InetAddress.getAllByName(Domain[leng]);
⑫ for(int i = 0; i < inetaddress.length; i++) {
⑬ String hostName = inetaddress[i].getHostAddress();
⑭ System.out.println(hostName);
⑮ }
16 }
17 } catch (UnknownHostException unhoste) {
18 System.err.println(unhoste);
19 }
20
21 }
22 public static void main(String[] args) {
23 InetAddressMain inetAddressMain = new InetAddressMain();
24 if(args.length > 0) {
25 System.out.println("Use : java InetAddressMain [Domain 1] [Domain 2] ...");
26 System.exit(-1);
27 }
28 inetAddressMain.exec(args);
29 }
30 }


5. URL클래스를 이용해 데이터를 가져오기
① // network.net.httpMgr
③ package network.net;
④ import java.awt.Frame;
⑤ import java.awt.event.WindowAdapter;
⑥ import java.awt.event.WindowEvent;
⑦ import javax.swing.UIManager;
⑧ import java.awt.Dimension;
⑨ import java.awt.Toolkit;
⑪ public class httpMgr {
⑫ public httpMgr() {
⑬ Frame frame = new httpMgrFrame();
⑭ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
⑮ Dimension frameSize = frame.getSize();
16 if (frameSize.height > screenSize.height) {
17 frameSize.height = screenSize.height;
18 }
19 if (frameSize.width > screenSize.width) {
20 frameSize.width = screenSize.width;
21 }
22 frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);
23 frame.addWindowListener(new WindowAdapter() {
24 public void windowClosing(WindowEvent e) {
25 System.exit(0);
26 }
27 });
28 frame.setVisible(true);
29 }
30
31 public static void main(String[] args) {
32 try {
33 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
34 } catch(Exception e) {
35 e.printStackTrace();
36 }
37 new httpMgr();
38 }
39 }

① // network.net.httpMgrFrame
③ package network.net;
④ import javax.swing.JFrame;
⑤ import java.awt.Dimension;
⑥ import javax.swing.JTextField;
⑦ import java.awt.Rectangle;
⑧ import javax.swing.BorderFactory;
⑨ import javax.swing.JButton;
⑩ import javax.swing.JTextArea;
⑪ import javax.swing.JScrollPane;
⑫ import javax.swing.JTextPane;
⑬ import java.awt.event.ActionListener;
⑭ import java.awt.event.ActionEvent;
⑮ import java.net.*;
16 import java.io.*;
17
18 public class httpMgrFrame extends JFrame {
19 private JTextField Ip = new JTextField();
20 private JTextField Port = new JTextField();
21 private JTextField Uri = new JTextField();
22 private JTextField fileName = new JTextField();
23
24 private JButton submit = new JButton();
25 private JButton reset = new JButton();
26 private JButton saveAs = new JButton();
27
28 private JTextArea property = new JTextArea();
29 private JScrollPane jScrollPane1 = new JScrollPane();
30 private JTextPane htmlMessage = new JTextPane();
31
32 public httpMgrFrame() {
33 try {
34 jbInit();
35 } catch(Exception e) {
36 e.printStackTrace();
37 }
38 }
39
40 private void jbInit() throws Exception {
41 this.getContentPane().setLayout(null);
42 this.setSize(new Dimension(512, 602));
43 this.setTitle("HTTP MGR");
44 Ip.setBounds(new Rectangle(35, 25, 150, 45));
45 Ip.setBorder(BorderFactory.createTitledBorder("IP"));
46 Ip.setNextFocusableComponent(Port);
47
48 Port.setBounds(new Rectangle(35, 80, 150, 45));
49 Port.setBorder(BorderFactory.createTitledBorder("PORT"));
50 Port.setNextFocusableComponent(Uri);
51
52 Uri.setBounds(new Rectangle(35, 135, 150, 45));
53 Uri.setBorder(BorderFactory.createTitledBorder("URI"));
54 Uri.setNextFocusableComponent(property);
55
56 submit.setText("submit");
57 submit.setBounds(new Rectangle(220, 85, 85, 35));
58 submit.setActionCommand("submit");
59 submit.setNextFocusableComponent(reset);
60 submit.addActionListener(new ActionListener() {
61 public void actionPerformed(ActionEvent e) {
62 submit_actionPerformed(e);
63 }
64 });
65
66 reset.setText("reset");
67 reset.setBounds(new Rectangle(350, 85, 85, 35));
68 reset.addActionListener(new ActionListener() {
69 public void actionPerformed(ActionEvent e) {
70 reset_actionPerformed(e);
71 }
72 });
73
74 property.setBounds(new Rectangle(35, 190, 435, 55));
75 property.setBorder(BorderFactory.createTitledBorder("PROPERTY"));
76 property.setNextFocusableComponent(submit);
77
78 jScrollPane1.setBounds(new Rectangle(40, 275, 430, 180));
79 htmlMessage.setBorder(BorderFactory.createTitledBorder("HTML MESSAGE"));
80 fileName.setBounds(new Rectangle(40, 485, 275, 45));
81 fileName.setBorder(BorderFactory.createTitledBorder("FILE NAME"));
82 fileName.setNextFocusableComponent(saveAs);
83 fileName.setEnabled(false);
84 saveAs.setText("save as");
85 saveAs.setBounds(new Rectangle(360, 490, 85, 35));
86 saveAs.setActionCommand("saveAs");
87 saveAs.setEnabled(false);
88 saveAs.addActionListener(new ActionListener() {
89 public void actionPerformed(ActionEvent e) {
90 saveAs_actionPerformed(e);
91 }
92 });
93 jScrollPane1.getViewport().add(htmlMessage, null);
94
95 this.getContentPane().add(saveAs, null);
96 this.getContentPane().add(fileName, null);
97 this.getContentPane().add(jScrollPane1, null);
98 this.getContentPane().add(property, null);
99 this.getContentPane().add(reset, null);
100 this.getContentPane().add(submit, null);
101 this.getContentPane().add(Uri, null);
102 this.getContentPane().add(Port, null);
103 this.getContentPane().add(Ip, null);
104 }
105
106 private void reset_actionPerformed(ActionEvent e) {
107 // 아직 구현 안함.
108 }
109
110 private void submit_actionPerformed(ActionEvent e) {
111 String ipString = Ip.getText();
112 int portNum = Integer.parseInt(Port.getText());
113 String uriString = Uri.getText();
114 String propertyString = property.getText();
115 try {
116 URL url = new URL("http",ipString,portNum,uriString);
117 StringBuffer sb = StreamParser.Parser(url.openStream());
118 htmlMessage.setText(sb.toString());
119 fileName.setEnabled(true);
120 saveAs.setEnabled(true);
121 } catch (MalformedURLException mfurle) {
122 htmlMessage.setText(mfurle.getMessage());
123 } catch (IOException ioe) {
124 htmlMessage.setText(ioe.getMessage());
125 }
126 }
127
128 private void saveAs_actionPerformed(ActionEvent e) {
129 try{
130 FileWriter fileOut = new FileWriter(fileName.getText());
131 fileOut.write(htmlMessage.getText());
132 fileOut.close();
133 htmlMessage.setText("파일이 저장되었습니다.");
134 } catch (IOException ioe) {
135 htmlMessage.setText("파일저장중 에러가 발생하였습니다.");
136 }
137 }
138 }
139
140 class StreamParser {
141 public static StringBuffer Parser(InputStream in) throws IOException {
142 StringBuffer sb = new StringBuffer();
143 BufferedReader br = new BufferedReader(new InputStreamReader(in));
144 String readString = "";
145 while( (readString = br.readLine()) != null) {
146 sb.append(readString+" \n");
147 }
148 return sb;
149 }
150 }

댓글 없음:

댓글 쓰기

블록체인 개요 및 오픈소스 동향

블록체인(block chain) 블록체인은 공공 거래장부이며 가상 화폐로 거래할때 발생할때 발생할 수 있는 해킹을 막는 기술. 분산 데이터베이스의 한 형태로, 지속적으로 성장하는 데이터 기록 리스트로서 분산 노드의 운영자에 의한 임의 조작이 불가...