{"id":6573,"date":"2026-07-17T08:31:24","date_gmt":"2026-07-16T23:31:24","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=6573"},"modified":"2026-07-17T17:05:00","modified_gmt":"2026-07-17T08:05:00","slug":"nodejs8-java-client-chat-basic-3","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/07\/17\/nodejs8-java-client-chat-basic-3\/","title":{"rendered":"nodejs8- \uc790\ubc14\ud074\ub77c\uc774\uc5b8\ud2b8 \ucc44\ud305 \ubca0\uc774\uc9c1\/java client chat basic(3)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc\uc124\uba85 \/ Code Explanation<\/p>\n\n\n\n<p>\u2714\ufe0f ChatClientGUI.java<\/p>\n\n\n\n<p>&#8212; ChatClientGUI \uc0dd\uc131\uc790 \/ ChatClientGUI Constructor<\/p>\n\n\n\n<p>1)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed \/ Chat display area<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>      chatArea = new JTextArea();\n      chatArea.setEditable(false);\n      JScrollPane scrollPane = new JScrollPane(chatArea);\n      add(scrollPane, BorderLayout.CENTER);<\/code><\/pre>\n\n\n\n<p>1-1)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed\uc73c\ub85c \uc218\uc815\ud560 \uc218 \uc5c6\ub3c4\ub85d setEditable(false\ub85c \uc124\uc815\ub418\uc5b4 \uc788\uc2b5\ub2c8\ub2e4.)<br>It is set to <code>setEditable(false)<\/code> to prevent editing within the chat display area.<\/p>\n\n\n\n<p>1-2)\ub300\ud654\ub0b4\uc6a9\uc774 \ub9ce\uc744 \uacbd\uc6b0 \uc2a4\ud06c\ub864\uc744 \uc801\uc6a9\ud569\ub2c8\ub2e4.<br>Scroll functionality is applied if the conversation content is extensive.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JScrollPane scrollPane = new JScrollPane(chatArea);<\/code><\/pre>\n\n\n\n<p>1-3)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed\uc744 \uac00\uc6b4\ub370 \uac00\ub4dd \ucc44\uc6b0\ub3c4\ub85d \ubc30\uce58\ud569\ub2c8\ub2e4.<br>Position the chat display area to fully fill the center.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>add(scrollPane, BorderLayout.CENTER);<\/code><\/pre>\n\n\n\n<p>1-4)\ub808\uc774\uc544\uc6c3\uc740 \ub2e4\uc74c\uacfc \uac19\uc2b5\ub2c8\ub2e4.<br>The layout is as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>+-----------------------------------+\n\n|             NORTH (\ubd81)            |\n+-------+-------------------+-------+\n\n|  WEST |                   |  EAST |\n|  (\uc11c) |    CENTER (\uc911\uc559)   |  (\ub3d9) |\n|       |                   |       |\n+-------+-------------------+-------+\n\n|             SOUTH (\ub0a8)            |\n+-----------------------------------+\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>2)\ucc44\ud305 \uc785\ub825\ucc3d \ubc0f send \ubc84\ud2bc<br>Chat input field and Send button<\/p>\n\n\n\n<p>2-1)\ucc44\ud305\uc744 \uc785\ub825 \ucc3d\uacfc send\ubc84\ud2bc\uc744 \uc815\uc758 \ud558\uace0 \ubc30\uce58\ud569\ub2c8\ub2e4.<br>Define and position the chat input field and the send button.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        JPanel inputPanel = new JPanel(new BorderLayout());\n        messageInput = new JTextField();\n        sendButton = new JButton(\"\ubcf4\ub0b4\uae30\/send\");\n        sendButton.addActionListener(this);\n\n        inputPanel.add(messageInput, BorderLayout.CENTER);\n        inputPanel.add(sendButton, BorderLayout.EAST);\n        add(inputPanel, BorderLayout.SOUTH);<\/code><\/pre>\n\n\n\n<p>2-2)\uba54\uc2dc\uc9c0 \uc785\ub825\ucc3d\uacfc \ubcf4\ub0b4\uae30 \ubc84\ud2bc\uc744 \ud55c \uc138\ud2b8\ub85c \ubb36\uae30 \uc704\ud55c \ub3c4\uad6c\uc785\ub2c8\ub2e4.<br>This is a tool for grouping the message input field and the send button together as a single unit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>JPanel inputPanel = new JPanel(new BorderLayout());<\/code><\/pre>\n\n\n\n<p>2-3)\ubc84\ud2bc\uacfc \uc774\ubca4\ud2b8\ub97c \uc5f0\uacb0\ud569\ub2c8\ub2e4<br>Connect the button to the event.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sendButton.addActionListener(this);<\/code><\/pre>\n\n\n\n<p>2-4)this\uc790\uae30 \uc790\uc2e0\uc758 \ud074\ub798\uc2a4(ChatClientGUI)\ub97c \ucc38\uc870\ud569\ub2c8\ub2e4.<br>It references its own class (ChatClientGUI).<\/p>\n\n\n\n<p>2-5)\uc774\ub54c \uc774 \ud074\ub798\uc2a4\uc5d0 \uc120\uc5b8\ub41c actionPerformed(ActionEvent e) \uc774 \ud568\uc218\ub97c \ud638\ucd9c\ud569\ub2c8\ub2e4.<br>At this point, the <code>actionPerformed(ActionEvent e)<\/code> function declared in this class is called.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    @Override\n    public void actionPerformed(ActionEvent e) {\n        if (e.getSource() == sendButton) {\n            String message = messageInput.getText();\n            if (!message.isEmpty()) {\n                out.println(message); \/\/ \uc11c\ubc84\ub85c \uba54\uc2dc\uc9c0 \uc804\uc1a1 \/ Send message to server\n                chatArea.append(\"\ub098Me: \" + message + \"\\n\");\n                messageInput.setText(\"\");\n            }\n        }\n    }<\/code><\/pre>\n\n\n\n<p>2-5)actionPerformed(ActionEvent e)\ud568\uc218\uc55e\uc5d0 @Override \uc5b4\ub178\ud14c\uc774\uc158\uc774 \ubd99\uc5b4\uc788\uc5b4\uc11c \uc7ac\uc815\uc758\ud55c \ud568\uc218\uc784\uc744 \uc54c \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>The <code>@Override<\/code> annotation preceding the <code>actionPerformed(ActionEvent e)<\/code> function indicates that it is an overridden function.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>3)\ud14d\uc2a4\ud2b8 \uc785\ub825\ucc3d\uacfc\ubc84\ud2bc\uc744 \ud328\ub110\uc5d0 \ubd99\uc774\uace0 \uc544\ub798\ucabd(south)\uc5d0 \ubc30\uce58\ud569\ub2c8\ub2e4. <br>Attach the text input field and the button to the panel and position them at the bottom (south).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        inputPanel.add(messageInput, BorderLayout.CENTER);\n        inputPanel.add(sendButton, BorderLayout.EAST);\n        add(inputPanel, BorderLayout.SOUTH);<\/code><\/pre>\n\n\n\n<p>4)nodejs\uc11c\ubc84\uc5d0 \uc811\uc18d\ud558\uace0 \uba54\uc138\uc9c0 \uc218\uc2e0\uc2a4\ub808\ub4dc\ub97c \uc2dc\uc791\ud569\ub2c8\ub2e4.<br>Connect to the Node.js server and start the message reception thread.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>connectToServer();<\/code><\/pre>\n\n\n\n<p>4-1)connectToServer\ud568\uc218<br>connectToServer function<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    private void connectToServer() {\n        try {\n            socket = new Socket(SERVER_ADDRESS, SERVER_PORT);\n            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\n            out = new PrintWriter(socket.getOutputStream(), true);\n\n            chatArea.append(\"\uc11c\ubc84\uc5d0 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\\nsercer connected\");\n\n            \/\/ \uc11c\ubc84\ub85c\ubd80\ud130 \uba54\uc2dc\uc9c0\ub97c \uc218\uc2e0\ud558\ub294 \uc2a4\ub808\ub4dc \uc2dc\uc791\n            \/\/ Start a thread to receive messages from the server\n            Thread receiveThread = new Thread(this::receiveMessages);\n            receiveThread.start();\n\n        } catch (IOException e) {\n            chatArea.append(\"\uc11c\ubc84 \uc5f0\uacb0\uc5d0 \uc2e4\ud328\ud588\uc2b5\ub2c8\ub2e4: \" + e.getMessage() + \"\\n\");\n        }\n    }<\/code><\/pre>\n\n\n\n<p>&#8212; \ub098\uba38\uc9c0 \ud568\uc218 \/ Remainder function<\/p>\n\n\n\n<p>1)\uc11c\ubc84\ub85c\ubd80\ud130 \uba54\uc138\uc9c0 \ubc1b\uae30<br>Receive message from server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    private void receiveMessages() {\n        String message;\n        try {\n            while ((message = in.readLine()) != null) {\n                chatArea.append(\"\uc11c\ubc84server: \" + message + \"\\n\");\n            }\n        } catch (IOException e) {\n            chatArea.append(\"\uc11c\ubc84 \uc5f0\uacb0\uc774 \ub04a\uc5b4\uc84c\uc2b5\ub2c8\ub2e4.\\nFailed to connect to server.\");\n        } finally {\n            closeConnection();\n        }\n    }<\/code><\/pre>\n\n\n\n<p>1-1)\ubb34\ud55c\ub8e8\ud504 \ud558\uba74\uc11c \uba54\uc138\uc9c0\ub97c \uc77d\uc5b4\uc640\uc11c \ucc44\ud305\uc601\uc5ed\uc5d0 \ud45c\uc2dc\ud569\ub2c8\ub2e4.<br>It runs an infinite loop to read messages and display them in the chat area.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            while ((message = in.readLine()) != null) {\n                chatArea.append(\"\uc11c\ubc84server: \" + message + \"\\n\");\n            }<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>2)\uc11c\ubc84\uc640 \uc811\uc18d \uc885\ub8cc \ucc98\ub9ac\ubd80\ubd84\uc785\ub2c8\ub2e4.<br>This section handles the disconnection from the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    private void closeConnection() {\n        try {\n            if (out != null) out.close();\n            if (in != null) in.close();\n            if (socket != null &amp;&amp; !socket.isClosed()) socket.close();\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }<\/code><\/pre>\n\n\n\n<p>3)main\ud568\uc218\ub85c \ud504\ub85c\uadf8\ub7a8\uc758 \uccab \uc9c4\uc785\uc810\uc785\ub2c8\ub2e4.<br>The <code>main<\/code> function serves as the program&#8217;s initial entry point.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    public static void main(String&#91;] args) {\n        SwingUtilities.invokeLater(ChatClientGUI::new);\n    }<\/code><\/pre>\n\n\n\n<p>3-1)\uc624\ub958 \ubc1c\uc0dd\ud560 \uc218 \uc788\ub294 \uba54\uc778\uc2a4\ub808\ub4dc \ub300\uc2e0\uc5d0 invokeLater\ub77c\ub294 GUI\uc804\uc6a9\uc2a4\ub808\ub4dc\ub85c \uc2e4\ud589\ud558\ub3c4\ub85d \ud569\ub2c8\ub2e4.<br>Instead of the main thread, where errors might occur, execute the task using a GUI-specific thread called <code>invokeLater<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc\uc124\uba85 \/ Code Explanation \u2714\ufe0f ChatClientGUI.java &#8212; ChatClientGUI \uc0dd\uc131\uc790 \/ ChatClientGUI Constructor 1)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed \/ Chat display area 1-1)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed\uc73c\ub85c \uc218\uc815\ud560 \uc218 \uc5c6\ub3c4\ub85d setEditable(false\ub85c \uc124\uc815\ub418\uc5b4 \uc788\uc2b5\ub2c8\ub2e4.)It is set to setEditable(false) to prevent editing within the chat display area. 1-2)\ub300\ud654\ub0b4\uc6a9\uc774 \ub9ce\uc744 \uacbd\uc6b0 \uc2a4\ud06c\ub864\uc744 \uc801\uc6a9\ud569\ub2c8\ub2e4.Scroll functionality is applied if the conversation content is extensive. 1-3)\ucc44\ud305\ud45c\uc2dc \uc601\uc5ed\uc744 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6573","post","type-post","status-publish","format-standard","hentry","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/6573","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=6573"}],"version-history":[{"count":47,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/6573\/revisions"}],"predecessor-version":[{"id":6621,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/6573\/revisions\/6621"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=6573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=6573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=6573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}