Akashic Records

Java 네트워크 - 3 본문

오래된글/Java

Java 네트워크 - 3

Andrew's Akashic Records 2018. 4. 9. 11:07
728x90

6. 클라이언트 소켓

데이터는 “데이터그램”이라고 불리우는 한정된 크기의 패킷 형태로 존재한다. 데이터그램은 “헤더”와 “페이로드”로 구성되어 있다. 헤더에는 이 패킷이 어디로 가고 있는지를 나타내는 목적지 주소와 포트 번호, 어디에서 왔는지를 나타내는 출발지 주소와 포트 번호, 그리고 패킷이 정확히 전달 되도록 하기 위해서 필요한 여러 정보들이 들어있다.

  • public Socket(String host, int port) throws UnknownHostException, IOException

  • public Socket(InetAddress address, int port) throws IOException

  • public InetAddress getInetAddress() : 소켓의 접속처의 주소를 돌려준다.

  • public InetAddress getLocalAddress()

  • public int getPort()

  • public int getLocalPort() : 이 소켓에 바인딩된 로컬 포트를 돌려준다.

  • public InputStream getInputStream() throw IOException : 소켓으로부터 들어오는 원시 데이터를 반환

  • public OutputStream getOutputStream() throw IOException : 소켓으로 데이터를 보내기 위해 사용되는 원시 OutputSteam을 반환한다.

  • public synchronized void close() throws IOException : 소켓을 닫습니다.

① package network.net.whois;

② 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 whoIsMain {

⑩ public whoIsMain() {

⑪ Frame frame = new whoIsFrame();

⑫ Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();

⑬ Dimension frameSize = frame.getSize();

⑭ if (frameSize.height > screenSize.height) {

⑮ frameSize.height = screenSize.height;

16 }

17 if (frameSize.width > screenSize.width) {

18 frameSize.width = screenSize.width;

19 }

20 frame.setLocation((screenSize.width - frameSize.width) / 2, (screenSize.height - frameSize.height) / 2);

21 frame.addWindowListener(new WindowAdapter() {

22 public void windowClosing(WindowEvent e) {

23 System.exit(0);

24 }

25 });

26 frame.setVisible(true);

27 }

28

29 public static void main(String[] args) {

30 try {

31 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());

32 } catch(Exception e) {

33 e.printStackTrace();

34 }

35 new whoIsMain();

36 }

37 }

38

39 package network.net.whois;

40 import javax.swing.JFrame;

41 import java.awt.Dimension;

42 import javax.swing.JLabel;

43 import java.awt.Rectangle;

44 import javax.swing.JTextField;

45 import javax.swing.JCheckBox;

46 import javax.swing.JScrollPane;

47 import javax.swing.JButton;

48 import javax.swing.JTextPane;

49 import java.awt.event.ActionListener;

50 import java.awt.event.ActionEvent;

51 import java.awt.event.KeyListener;

52 import java.awt.event.KeyEvent;

53 import java.awt.event.MouseListener;

54 import java.awt.event.MouseEvent;

55 import java.net.*;

56 import java.io.*;

57

58 public class whoIsFrame extends JFrame {

59 private JLabel jLabel1 = new JLabel();

60 private JTextField name = new JTextField();

61 private JScrollPane messageScrollPane = new JScrollPane();

62 private JButton search = new JButton();

63 private JTextPane messagePane = new JTextPane();

64

65 public whoIsFrame() {

66 try {

67 jbInit();

68 } catch(Exception e) {

69 e.printStackTrace();

70 }

71 }

72

73 private void jbInit() throws Exception {

74 this.getContentPane().setLayout(null);

75 this.setSize(new Dimension(505, 573));

76 this.setTitle("Who is?");

77 jLabel1.setText("Search Word");

78 jLabel1.setBounds(new Rectangle(20, 30, 85, 30));

79 name.setBounds(new Rectangle(105, 30, 180, 30));

80 messageScrollPane.setBounds(new Rectangle(25, 120, 445, 365));

81 search.setText("SEARCH");

82 search.setBounds(new Rectangle(310, 30, 95, 30));

83 search.addMouseListener(new java.awt.event.MouseAdapter() {

84 public void mouseClicked(MouseEvent e) {

85 search_mouseClicked(e);

86 }

87 });

88 search.addKeyListener(new java.awt.event.KeyAdapter() {

89 public void keyPressed(KeyEvent e) {

90 search_keyPressed(e);

91 }

92 });

93 search.addActionListener(new ActionListener() {

94 public void actionPerformed(ActionEvent e) {

95 search_actionPerformed(e);

96 }

97 });

98 messagePane.setEditable(false);

99 messagePane.setEnabled(false);

100 this.getContentPane().add(search, null);

101 messageScrollPane.getViewport().add(messagePane, null);

102 this.getContentPane().add(messageScrollPane, null);

103 this.getContentPane().add(name, null);

104 this.getContentPane().add(jLabel1, null);

105 }

106

107 private void isUse(boolean use) {

108 name.setEnabled(use);

109 search.setEnabled(use);

110 messagePane.setEnabled(!use);

111 }

112 private void setMessage(String msg) {

113 messagePane.setText(messagePane.getText()+msg+" \n");

114 }

115 private void action() {

116 //isUse(false);

117 Socket socket = null;

118 try{

119 socket = new Socket("whois.internic.net",43);

120 PrintStream out = new PrintStream(new BufferedOutputStream(socket.getOutputStream()));

121 out.print(name.getText());

122 out.print("\r\n");

123

124 BufferedReader in =

125 new BufferedReader(new InputStreamReader(socket.getInputStream()));

126 String readLineStr = "";

127 while((readLineStr = in.readLine()) != null) {

128 setMessage(readLineStr);

129 }

130 } catch (IOException ioe) {

131 setMessage(ioe.toString());

132 }

133 }

134 private void search_actionPerformed(ActionEvent e) {

135 action();

136 }

137

138 private void search_keyPressed(KeyEvent e) {

139 action();

140 }

141

142 private void search_mouseClicked(MouseEvent e) {

143 action();

144 }

145 }


728x90

'오래된글 > Java' 카테고리의 다른 글

Java 네트워크 - 5  (0) 2018.04.09
Java 네트워크 - 4  (0) 2018.04.09
Java 네트워크 - 2  (0) 2018.04.09
Java 네트워크 - 1  (0) 2018.04.09
Jakarta Commons Logging  (0) 2018.04.07
Comments