{"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":"2025-04-25T10:26:21","modified_gmt":"2025-04-25T10:26:21","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"},"content":{"rendered":"\n<p>nodejs\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>\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>\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>\uc790\ubc14 \ucef4\ud30c\uc77c\uc2dc \ud55c\uae00\ubb38\uc790\uac00 \uc788\uae30 \ub54c\ubb38\uc5d0 UTF-8\uc778\ucf54\ub529\uc73c\ub85c \ucef4\ud30c\uc77c\ud558\uace0 \uc2e4\ud589\ub3c4 UTF-8\ub85c \uc2e4\ud589\ud574\uc57c \ud569\ub2c8\ub2e4.<br>Since there are Korean characters when compiling Java, you must compile it in UTF-8 encoding and run it in UTF-8 as well.<\/p>\n\n\n\n<p>compile : javac -encoding UTF-8 FILENAME.java <br>command prompt run : java -Dfile.encoding=UTF-8 FILENAME<br>window powershell run : java &#8220;-Dfile.encoding=UTF-8&#8221; FILENAME<\/p>\n\n\n\n<p><strong>server.js<\/strong>\ub294 nodejs\uc11c\ubc84 \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4. server.js is a nodejs server program.<br><strong>ChatClientGUI.java<\/strong>\ub294 GUI\ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4. ChatClientGUI.java is a GUI program.<br><strong>ChatClinet<\/strong>\ub294 \uba85\ub839\ud504\ub86c\ud504\ud2b8\ub97c \uc0ac\uc6a9\ud558\ub294 \ud504\ub85c\uadf8\ub7a8\uc785\ub2c8\ub2e4. ChatClient is a program that uses a command prompt.<\/p>\n\n\n\n<p><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><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\"; \/\/ \uc11c\ubc84 \uc8fc\uc18c : server address\n        int serverPort = 3000; \/\/ \uc11c\ubc84 \ud3ec\ud2b8 : server port\n\n        try (\n            Socket socket = new Socket(serverAddress, serverPort);\n            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);\n            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));\n            Scanner scanner = new Scanner(System.in)\n        ) {\n            System.out.println(\"\uc11c\ubc84\uc5d0 \uc5f0\uacb0\ub418\uc5c8\uc2b5\ub2c8\ub2e4.\\n server connected\");\n\n            String userInput;\n            while (true) {\n                System.out.print(\"\ubcf4\ub0bc \uba54\uc2dc\uc9c0 sendmessage: \");\n                userInput = scanner.nextLine();\n                if (\"exit\".equalsIgnoreCase(userInput)) {\n                    break;\n                }\n                out.println(userInput);\n                String serverResponse = in.readLine();\n                System.out.println(\"\uc11c\ubc84 \uc751\ub2f5 server response: \" + serverResponse);\n            }\n            System.out.println(\"\uc5f0\uacb0\uc744 \uc885\ub8cc\ud569\ub2c8\ub2e4. Server connection terminated\");\n\n        } catch (IOException e) {\n            e.printStackTrace();\n        }\n    }\n}\n\n\/\/ \uc778\ucf54\ub529 \ubb38\uc81c\uac00 \uc788\uc744 \uc218 \uc788\uc73c\ub2c8 \uba54\uc138\uc9c0 \ubc1c\uc1a1\uc2dc \uc601\ubb38\uc73c\ub85c \ud14c\uc2a4\ud2b8 \ud560\uac83\n\/\/ \uc708\ub3c4\uc6b0 \ud30c\uc6cc\uc258\uc778\uacbd\uc6b0 \uc624\ub958 \ub0a0 \uc218 \uc788\uc73c\ub2c8 \uba85\ub839\ud504\ub86c\ud504\ud2b8\ub97c \uc0ac\uc6a9 \ud560 \uac83\n\/\/ There may be encoding issues, so please test in English when sending the message.\n\/\/ If you are using Windows Powershell, you may get an error, so use the command prompt.\n\/\/\n\/\/ UTF-8\ucef4\ud30c\uc77c:UTF-8compile : javac -encoding UTF-8 Client.java\n\/\/ UTF-8\uc2e4\ud589:UTF-8 run : java -Dfile.encoding=UTF-8 Client\n\/\/\n\/\/ \uc708\ub3c4\uc6b0 \ud30c\uc6cc\uc258 \uc2e4\ud589: window powershell run : java \"-Dfile.encoding=UTF-8\" Client\n<\/code><\/pre>\n\n\n\n<p><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\/\/ \uc778\ucf54\ub529 \ubb38\uc81c\uac00 \uc788\uc744 \uc218 \uc788\uc73c\ub2c8 \uba54\uc138\uc9c0 \ubc1c\uc1a1\uc2dc \uc601\ubb38\uc73c\ub85c \ud14c\uc2a4\ud2b8 \ud560\uac83\n\/\/ \uc708\ub3c4\uc6b0 \ud30c\uc6cc\uc258\uc778\uacbd\uc6b0 \uc624\ub958 \ub0a0 \uc218 \uc788\uc73c\ub2c8 \uba85\ub839\ud504\ub86c\ud504\ud2b8\ub97c \uc0ac\uc6a9 \ud560 \uac83\n\/\/ There may be encoding issues, so please test in English when sending the message.\n\/\/ If you are using Windows Powershell, you may get an error, so use the command prompt.\n\/\/\n\/\/ UTF-8\ucef4\ud30c\uc77c:UTF-8compile : javac -encoding UTF-8 ChatClientGUI.java\n\/\/ UTF-8\uc2e4\ud589:UTF-8 run : java -Dfile.encoding=UTF-8 ChatClientGUI\n\/\/\n\/\/ \uc708\ub3c4\uc6b0 \ud30c\uc6cc\uc258 \uc2e4\ud589: window powershell run : java \"-Dfile.encoding=UTF-8\" ChatClientGUI<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>nodejs\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. \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. \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":8,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":1174,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions\/1174"}],"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}]}}