{"id":1164,"date":"2025-04-24T02:50:07","date_gmt":"2025-04-24T02:50:07","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=1164"},"modified":"2026-07-15T11:02:01","modified_gmt":"2026-07-15T02:02:01","slug":"nodejs8-java-client-chat-basic","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2025\/04\/24\/nodejs8-java-client-chat-basic\/","title":{"rendered":"nodejs8- \uc790\ubc14\ud074\ub77c\uc774\uc5b8\ud2b8 \ucc44\ud305 \ubca0\uc774\uc9c1\/java client chat basic(1)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffbnodejs\uac00 \ub300\ubd80\ubd84 \uc6f9\uc0ac\uc774\ud2b8 \ub9cc\ub4e4\uae30 \uc704\ud55c \ub3c4\uad6c\ub85c \uc54c\uace0 \uacc4\uc2e0 \ubd84\uc774 \ub9ce\uc740 \uac83 \uac19\uc2b5\ub2c8\ub2e4.<br>It seems like most people know nodejs as a tool for creating websites.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uc544\ub798\ub294 nodejs\ub97c \uc11c\ubc84\ub85c \uc0ac\uc6a9\ud558\uace0 \uc790\ubc14\ub97c \ucc44\ud305 \ud074\ub77c\uc774\uc5b8\ud2b8\ub85c \uc0ac\uc6a9\ud558\ub294 \ub9e4\uc6b0 \uac04\ub2e8\ud55c \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4.<br>Below is a very simple program that uses nodejs as a server and java as a chat client.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb\uae30\ucd08\ub97c \uc313\ub294\ub370 \ub3c4\uc6c0\uc774 \ub418\ub9ac\ub77c \uc0dd\uac01\ud569\ub2c8\ub2e4.<br>I think it will help build a foundation.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc5ec\uae30\uc5d0\ub294 nodejs\uc11c\ubc84 \ud558\ub098\uc640 \ud130\ubbf8\ub110\uc6a9\uacfc gui\uc6a9 \ub450\uac1c\uc758 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc788\uc2b5\ub2c8\ub2e4.<br>Here, we have one Node.js server and two clients: one for the terminal and one with a GUI.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ud30c\uc77c \/ Files<\/p>\n\n\n\n<p>&#8212; class\ud30c\uc77c\uc740 java\ud30c\uc77c\uc744 \ucef4\ud30c\uc77c \ud558\uba74 \uc0dd\uc131\ub429\ub2c8\ub2e4.<br>.class file is generated when a .java file is compiled<\/p>\n\n\n\n<p>&#8212; package.json\uc740 npm init \uba85\ub839\uc744 \uc2e4\ud589\ud558\uba74 \uc0dd\uc131\ub429\ub2c8\ub2e4.<br>The package.json file is created when you run the <code>npm init<\/code> command.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ChatClientGUI.class\tClient.class\t\tpackage.json\nChatClientGUI.java\tClient.java\t\tserver.js<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ubaa8\ub4c8\uc124\uce58 \/ Install Module<\/p>\n\n\n\n<p>\u2714\ufe0f \uc5ec\uae30\uc11c\ub294 \ub530\ub85c \ubaa8\ub4c8\uc744 \uc124\uce58\ud558\uc9c0 \uc54a\uc2b5\ub2c8\ub2e4.<br>We do not install any separate modules here.<\/p>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc790\ubc14\uac00 \uc5c6\ub2e4\uba74 open jdk\ub97c \uc124\uce58\ud569\ub2c8\ub2e4.(MacOS)<br>If Java is not installed, install OpenJDK (macOS).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>brew install openjdk<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc790\ubc14 \ud30c\uc77c\uc740 \ud130\ubbf8\ub110\uc5d0\uc11c \uc544\ub798\ucc98\ub7fc \ucef4\ud30c\uc77c \ud569\ub2c8\ub2e4.<br>Compile Java files in the terminal as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Terminal Version\njavac Client.java\n\n# GUI Version\njavac ChatClientGUI.java<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucef4\ud30c\uc77c\uc744 \ud558\uba74 class\ud30c\uc77c\uc774 \uc0dd\uc131\ub429\ub2c8\ub2e4. \ud130\ubbf8\ub110\uc5d0\uc11c \ud074\ub798\uc2a4\ud30c\uc77c\uc740 \uc544\ub798\uc640 \uac19\uc774 \uc2e4\ud589\ud560 \uc218 \uc788\uc2b5\ub2c8\ub2e4.<br>Compiling generates a class file. You can run the class file from the terminal as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Terminal Version\njava Client.java\n\n# GUI Version\njava ChatClientGUI.java<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc804\uccb4\ucf54\ub4dc \/ Full Code<\/p>\n\n\n\n<p>\u2714\ufe0f <strong>server.js<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const net = require('net');\n\nconst server = net.createServer((socket) =&gt; {\n  console.log('\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\\nThe client is connected');\n\n  socket.on('data', (data) =&gt; {\n    console.log('\ud074\ub77c\uc774\uc5b8\ud2b8\ub85c\ubd80\ud130 \ubc1b\uc740 \ub370\uc774\ud130\\nData received from client:', data.toString());\n    socket.write('\uc11c\ubc84\uac00 \ubc1b\uc740 \ub370\uc774\ud130\ub97c \ub2e4\uc2dc \ubcf4\ub0c5\ub2c8\ub2e4\\nThe server sends back the data it received.: ' + data);\n  });\n\n  socket.on('end', () =&gt; {\n    console.log('\ud074\ub77c\uc774\uc5b8\ud2b8 \uc5f0\uacb0\uc774 \ub04a\uc5b4\uc84c\uc2b5\ub2c8\ub2e4.\\nThe client connection was lost.');\n  });\n\n  socket.on('error', (err) =&gt; {\n    console.error('\ud074\ub77c\uc774\uc5b8\ud2b8 \uc18c\ucf13 \uc5d0\ub7ec\\nclient socket error:', err);\n  });\n});\n\nconst port = 3000;\nserver.listen(port, () =&gt; {\n  console.log(`\uc11c\ubc84\uac00 \ud3ec\ud2b8 ${port}\uc5d0\uc11c \ub9ac\uc2a4\ub2dd \uc911\uc785\ub2c8\ub2e4.\\nThe server is listening on port ${port}.`);\n});\n\nserver.on('error', (err) =&gt; {\n  console.error('\uc11c\ubc84 \uc5d0\ub7ecserver error:', err);\n});<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f <strong>Client.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.PrintWriter;\nimport java.net.Socket;\nimport java.util.Scanner;\n\npublic class Client {\n    public static void main(String&#91;] args) {\n        String serverAddress = \"localhost\";\n        int serverPort = 3000;\n\n        try {\n            Socket socket = new Socket(serverAddress, serverPort);\n            System.out.println(\"\uc11c\ubc84\uc5d0 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4. \/ Connected to the server.\");\n            \n            \/\/ \ucd5c\ucd08 1\ud68c\ub9cc \uc785\ub825 \ud504\ub86c\ud504\ud2b8 \ucd9c\ub825\n            \/\/ Print the input prompt only once initially\n            System.out.print(\"\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message: \");\n            System.out.flush();\n\n            \/\/ 1. \uc11c\ubc84\ub85c\ubd80\ud130 \uba54\uc2dc\uc9c0\ub97c \uc2e4\uc2dc\uac04\uc73c\ub85c \ubc1b\ub294 \uc2a4\ub808\ub4dc \uc2dc\uc791\n            \/\/ 1. Start a thread to receive messages from the server in real-time\n            Thread receiveThread = new Thread(() -> {\n\n                try (BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()))) {\n                    String serverResponse;\n\n                    while ((serverResponse = in.readLine()) != null) {\n                        \/\/ ANSI \uc774\uc2a4\ucf00\uc774\ud504 \ucf54\ub4dc \ud2b8\ub9ad \/ ANSI Escape Code Trick\n                        \/\/ \\r: \ucee4\uc11c\ub97c \uc904 \ub9e8 \uc55e\uc73c\ub85c \uc774\ub3d9 \/ Move cursor to start of line\n                        \/\/ \\033&#91;K: \ud604\uc7ac \ucee4\uc11c\uac00 \uc788\ub294 \uc904 \uc9c0\uc6b0\uae30 \/ Clear current line\n                        System.out.print(\"\\r\\033&#91;K\"); \n                        \n                        \/\/ \uc11c\ubc84 \uba54\uc2dc\uc9c0 \ucd9c\ub825 \/ Print server message\n                        System.out.println(\"&#91;\uc11c\ubc84 \uc751\ub2f5 \/ Server Response]: \" + serverResponse);\n                        \n                        \/\/ \uc9c0\uc6cc\uc9c4 \uc785\ub825 \ud504\ub86c\ud504\ud2b8\ub97c \ub9e8 \uc544\ub798\uc5d0 \ub2e4\uc2dc \ubcf5\uad6c\n                        \/\/ Restore the cleared input prompt at the bottom\n                        System.out.print(\"\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message: \");\n                        System.out.flush();\n                    }\n                    \n                } catch (IOException e) {\n                    System.out.println(\"\\n\uc218\uc2e0 \uc2a4\ub808\ub4dc \uc885\ub8cc \/ Receive thread terminated.\");\n                }\n\n            });\n            receiveThread.start();\n\n            \/\/ 2. \uba54\uc778 \uc2a4\ub808\ub4dc: \uc0ac\uc6a9\uc790\uc758 \uc785\ub825\uc744 \ubc1b\uc544 \uc11c\ubc84\ub85c \uc804\uc1a1\n            \/\/ 2. Main Thread: Take user input and send it to the server\n            try (\n                PrintWriter out = new PrintWriter(socket.getOutputStream(), true);\n                Scanner scanner = new Scanner(System.in)\n            ) {\n                while (true) {\n                    String userInput = scanner.nextLine();\n                    \n                    if (\"exit\".equalsIgnoreCase(userInput)) {\n                        break;\n                    }\n                    out.println(userInput);\n                    \n                    \/\/ \ub0b4\uac00 \uc5d4\ud130\ub97c \ucce4\uc744 \ub54c \ud654\uba74\uc5d0 \ub0a8\uc544\uc788\ub294 \ud504\ub86c\ud504\ud2b8\ub97c \uc9c0\uc6b0\uace0 \ub2e4\uc2dc \uae54\ub054\ud558\uac8c \ucd9c\ub825\ud558\uac8c \ub9cc\ub4e6\n                    \/\/ Clears the lingering prompt when Enter is pressed and cleanly reprints it\n                    System.out.print(\"\\r\\033&#91;K\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message: \");\n                    System.out.flush();\n                }\n            }\n\n            socket.close();\n            System.out.println(\"\uc5f0\uacb0\uc744 \uc885\ub8cc\ud569\ub2c8\ub2e4. \/ Connection terminated.\");\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f <strong>ChatClientGUI.java<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import javax.swing.*;\nimport java.awt.*;\nimport java.awt.event.ActionEvent;\nimport java.awt.event.ActionListener;\nimport java.io.BufferedReader;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.PrintWriter;\nimport java.net.Socket;\n\npublic class ChatClientGUI extends JFrame implements ActionListener {\n\n    private static final String SERVER_ADDRESS = \"localhost\";\n    private static final int SERVER_PORT = 3000;\n\n    private JTextArea chatArea;\n    private JTextField messageInput;\n    private JButton sendButton;\n    private Socket socket;\n    private BufferedReader in;\n    private PrintWriter out;\n\n    public ChatClientGUI() {\n        setTitle(\"\ucc44\ud305 \ud074\ub77c\uc774\uc5b8\ud2b8\/chat client\");\n        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);\n        setSize(400, 300);\n        setLayout(new BorderLayout());\n\n        chatArea = new JTextArea();\n        chatArea.setEditable(false);\n        JScrollPane scrollPane = new JScrollPane(chatArea);\n        add(scrollPane, BorderLayout.CENTER);\n\n        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);\n\n        setVisible(true);\n\n        connectToServer();\n    }\n\n    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            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    }\n\n    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    }\n\n    @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);\n                chatArea.append(\"\ub098Me: \" + message + \"\\n\");\n                messageInput.setText(\"\");\n            }\n        }\n    }\n\n    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    }\n\n    public static void main(String&#91;] args) {\n        SwingUtilities.invokeLater(ChatClientGUI::new);\n    }\n}\n\n<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \uc2e4\ud589 \/ Run<\/p>\n\n\n\n<p>\u2714\ufe0f \uc11c\ubc84 \uc2e4\ud589 \/ Run Server<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>node server.js<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f \ud074\ub77c\uc774\uc5b8\ud2b8 \uc2e4\ud589 \/ Run Client<\/p>\n\n\n\n<p>&#8212; Terminal Version<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java Client<\/code><\/pre>\n\n\n\n<p>&#8212; GUI Version<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>java ChatClientGUI<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"680\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-1024x680.png\" alt=\"\" class=\"wp-image-6515\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-1024x680.png 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-300x199.png 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-768x510.png 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-400x266.png 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg-800x531.png 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/server-jpg.png 1144w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>server.js<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"668\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-1024x668.jpg\" alt=\"\" class=\"wp-image-6516\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-1024x668.jpg 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-300x196.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-768x501.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-400x261.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg-800x522.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/terminal-version-jpg.jpg 1168w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Client Terminal Version<\/figcaption><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"974\" src=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-1024x974.jpg\" alt=\"\" class=\"wp-image-6518\" srcset=\"https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-1024x974.jpg 1024w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-300x285.jpg 300w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-768x731.jpg 768w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-400x381.jpg 400w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg-800x761.jpg 800w, https:\/\/www.freelifemakers.org\/wordpress\/wp-content\/uploads\/2025\/04\/gui-version-jpg.jpg 1278w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Client GUI Version<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \uc124\uba85 \/ Code Explanation<\/p>\n\n\n\n<p>\u2714\ufe0f server.js<\/p>\n\n\n\n<p>&#8212;  net.Socket\uc5d0\uc11c socket.on()\uc744 \uc0ac\uc6a9\ud560 \uacbd\uc6b0\uc758 \uace0\uc815\uc774\ubca4\ud2b8 \uc124\uba85\uc785\ub2c8\ub2e4.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\uc774\ubca4\ud2b8 \uc774\ub984 \/ Event Name<\/td><td>\ubc1c\uc0dd \uc2dc\uc810 , \uc124\uba85 \/ Trigger , Description<\/td><\/tr><tr><td>&#8216;data&#8217;<\/td><td>\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc11c\ubc84\ub85c \ub370\uc774\ud130\ub97c \ubcf4\ub0c8\uc744 \ub54c <br>When data is received<\/td><\/tr><tr><td>&#8216;end&#8217;<\/td><td>\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc5f0\uacb0\uc744 \ub04a\uaca0\ub2e4\uace0 \uc2e0\ud638\ub97c \ubcf4\ub0c8\uc744 \ub54c When client signals disconnection<\/td><\/tr><tr><td>&#8216;close&#8217;<\/td><td>\uc18c\ucf13 \uc5f0\uacb0\uc774 \uc644\uc804\ud788 \ub2eb\ud614\uc744 \ub54c <br>When socket is completely closed<\/td><\/tr><tr><td>&#8216;error&#8217;<\/td><td>\ub124\ud2b8\uc6cc\ud06c \ud1b5\uc2e0 \uc911 \uc5d0\ub7ec\uac00 \ubc1c\uc0dd\ud588\uc744 \ub54c <br>When a network error occurs<\/td><\/tr><tr><td>&#8216;connect&#8217;<\/td><td>\uc18c\ucf13\uc774 \uc11c\ubc84\uc640 \uc5f0\uacb0 \uc131\uacf5\ud588\uc744 \ub54c <br>When connection is successfully established<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>&#8212; \ud074\ub77c\uc774\uc5b8\ud2b8 \uc811\uc18d \ucc98\ub9ac \/ Client Connection Handling<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const clients = &#91;];\n\nconst server = net.createServer((socket) => {\n  \/\/ \uc0c8 \ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc811\uc18d\ud558\uba74 \ubc30\uc5f4\uc5d0 \ucd94\uac00\n  \/\/ Add new client to the array upon connection\n  clients.push(socket);\n  console.log(`\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4. \/ Client connected. (Total: ${clients.length})`);\n\n  socket.on('data', (data) => {\n    const message = data.toString();\n    console.log('\ud074\ub77c\uc774\uc5b8\ud2b8\ub85c\ubd80\ud130 \ubc1b\uc740 \ub370\uc774\ud130 \/ Data received from client:', message);\n    \n    \/\/ \ube0c\ub85c\ub4dc\uce90\uc2a4\ud2b8: \ubc30\uc5f4\uc5d0 \uc788\ub294 \ubaa8\ub4e0 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \uba54\uc2dc\uc9c0 \uc804\uc1a1\n    \/\/ Broadcast: Send message to all clients in the array\n    clients.forEach((client) => {\n      \/\/ \uc18c\ucf13\uc774 \uc815\uc0c1\uc801\uc73c\ub85c \uc5f4\ub824\uc788\ub294 \uc0c1\ud0dc\uc778\uc9c0 \ud655\uc778 \ud6c4 \uc804\uc1a1\n      \/\/ Check if the socket is writable before sending\n      if (client.writable) {\n        client.write('\ube0c\ub85c\ub4dc\uce90\uc2a4\ud2b8 \uba54\uc2dc\uc9c0 \/ Broadcast message: ' + message);\n      }\n    });\n  });<\/code><\/pre>\n\n\n\n<p>1)\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc811\uc18d\ud558\uba74 client\ubc30\uc5f4\uc5d0 \ud074\ub77c\uc774\uc5b8\ud2b8\ub97c \ubc30\uc5f4\uc5d0 \ucd94\uac00\ud569\ub2c8\ub2e4.<br>When a client connects, it is added to the <code>client<\/code> array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> clients.push(socket);<\/code><\/pre>\n\n\n\n<p>2)\ud074\ub77c\uc774\uc5b8\ud2b8\ub85c \ubc1b\uc740 \uba54\uc138\uc9c0\ub97c \ud130\ubbf8\ub110\uc5d0 \ucd9c\ub825\ud569\ub2c8\ub2e4.<br>Prints the message received by the client to the terminal.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>console.log('\ud074\ub77c\uc774\uc5b8\ud2b8\ub85c\ubd80\ud130 \ubc1b\uc740 \ub370\uc774\ud130 \/ Data received from client:', message);<\/code><\/pre>\n\n\n\n<p>3)\uc811\uc18d\ub41c \ubaa8\ub4e0\ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uac8c \uba54\uc138\uc9c0\ub97c \ubcf4\ub0c5\ub2c8\ub2e4.<br>Sends a message to all connected clients.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (client.writable) {\n   client.write('\ube0c\ub85c\ub4dc\uce90\uc2a4\ud2b8 \uba54\uc2dc\uc9c0 \/ Broadcast message: ' + message);\n}<\/code><\/pre>\n\n\n\n<p>&#8212; \ud074\ub77c\uc774\uc5b8\ud2b8 \uc811\uc18d \ud574\uc81c \ucc98\ub9ac<br>Handling client disconnection<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>  socket.on('end', () => {\n    console.log('\ud074\ub77c\uc774\uc5b8\ud2b8 \uc5f0\uacb0 \uc885\ub8cc \uc694\uccad \/ Client requested disconnection.');\n  });\n\n  socket.on('close', () => {\n    \/\/ \uc5f0\uacb0\uc774 \uc644\uc804\ud788 \ub04a\uc5b4\uc9c0\uba74 \ubc30\uc5f4\uc5d0\uc11c \ud574\ub2f9 \uc18c\ucf13 \uc81c\uac70\n    \/\/ Remove the socket from the array when completely closed\n    const index = clients.indexOf(socket);\n    if (index !== -1) {\n      clients.splice(index, 1);\n    }\n    console.log(`\ud074\ub77c\uc774\uc5b8\ud2b8 \uc5f0\uacb0 \ud574\uc81c \uc644\ub8cc \/ Client disconnected. (Total: ${clients.length})`);\n  });<\/code><\/pre>\n\n\n\n<p>1)\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc11c\ubc84\uc640 \uc811\uc18d\uc774 \ud574\uc81c\ub420\ub54c \ucc98\ub9ac \ub418\ub294 \ubd80\ubd84\uc785\ub2c8\ub2e4.<br>This section handles the process when the client disconnects from the server.<\/p>\n\n\n\n<p>2)\ud074\ub77c\uc774\uc5b8\ud2b8\uac00 \uc11c\ubc84\uc640 \uc811\uc18d\ud574\uc81c\ub418\uba74 client\ubc30\uc5f4\uc5d0\uc11c \ud074\ub77c\uc774\uc5b8\ud2b8 \uc18c\ucf13 \uc815\ubcf4\ub97c \uc0ad\uc81c\ud569\ub2c8\ub2e4.<br>When a client disconnects from the server, the client socket information is removed from the client array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    const index = clients.indexOf(socket);\n    if (index !== -1) {\n      clients.splice(index, 1);\n    }<\/code><\/pre>\n\n\n\n<p>\u2714\ufe0f Client.java<\/p>\n\n\n\n<p>&#8212; \\033[K (ChatClient.java)<\/p>\n\n\n\n<p>1)\ud130\ubbf8\ub110 \ud654\uba74\uc744 \uc81c\uc5b4\ud558\ub294 ANSI \uc774\uc2a4\ucf00\uc774\ud504 \uc2dc\ud000\uc2a4(ANSI Escape Sequence) \ud45c\uc900 \uba85\ub839\uc5b4\uc785\ub2c8\ub2e4.<br>These are standard ANSI escape sequence commands used to control the terminal screen.<\/p>\n\n\n\n<p>2)\ud604\uc7ac \ucee4\uc11c\uac00 \uc788\ub294 \uc904\uc744 \uc9c0\uc6c1\ub2c8\ub2e4.<br>Deletes the line where the cursor is currently located.<\/p>\n\n\n\n<p>\u2705 \ub098\uba38\uc9c0\ub294 \ub2e4\uc74c \ud3ec\uc2a4\ud2b8\uc5d0\uc11c \uc124\uba85\ud569\ub2c8\ub2e4.<br>I will explain the rest in the next post.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffbnodejs\uac00 \ub300\ubd80\ubd84 \uc6f9\uc0ac\uc774\ud2b8 \ub9cc\ub4e4\uae30 \uc704\ud55c \ub3c4\uad6c\ub85c \uc54c\uace0 \uacc4\uc2e0 \ubd84\uc774 \ub9ce\uc740 \uac83 \uac19\uc2b5\ub2c8\ub2e4.It seems like most people know nodejs as a tool for creating websites. \ud83d\udc49\ud83c\udffb\uc544\ub798\ub294 nodejs\ub97c \uc11c\ubc84\ub85c \uc0ac\uc6a9\ud558\uace0 \uc790\ubc14\ub97c \ucc44\ud305 \ud074\ub77c\uc774\uc5b8\ud2b8\ub85c \uc0ac\uc6a9\ud558\ub294 \ub9e4\uc6b0 \uac04\ub2e8\ud55c \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4.Below is a very simple program that uses nodejs as a server and java as a chat client. \ud83d\udc49\ud83c\udffb\uae30\ucd08\ub97c \uc313\ub294\ub370 \ub3c4\uc6c0\uc774 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,1],"tags":[],"class_list":["post-1164","post","type-post","status-publish","format-standard","hentry","category-nodejs","category-uncategorized","missing-thumbnail"],"_links":{"self":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1164","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=1164"}],"version-history":[{"count":54,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":6549,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions\/6549"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}