{"id":6550,"date":"2026-07-16T13:50:26","date_gmt":"2026-07-16T04:50:26","guid":{"rendered":"https:\/\/www.freelifemakers.org\/wordpress\/?p=6550"},"modified":"2026-07-17T17:03:54","modified_gmt":"2026-07-17T08:03:54","slug":"nodejs8-java-client-chat-basic-2","status":"publish","type":"post","link":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/2026\/07\/16\/nodejs8-java-client-chat-basic-2\/","title":{"rendered":"nodejs8- \uc790\ubc14\ud074\ub77c\uc774\uc5b8\ud2b8 \ucc44\ud305 \ubca0\uc774\uc9c1\/java client chat basic(2)"},"content":{"rendered":"\n<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \uc124\uba85 \/ Code Explanation<\/p>\n\n\n\n<p>\u2714\ufe0f Client.java<\/p>\n\n\n\n<p>&#8212; nodejs \uc11c\ubc84\ub791 \ud638\uc2a4\ud2b8\uc640 \ud3ec\ud2b8\ub97c \ub3d9\uc77c\ud558\uac8c \ub9de\ucda5\ub2c8\ub2e4.<br>Match the host and port to those of the Node.js server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>        String serverAddress = \"localhost\";\n        int serverPort = 3000;<\/code><\/pre>\n\n\n\n<p>&#8212; \uc2a4\ub808\ub4dc\ub97c \uc0ac\uc6a9\ud558\uc5ec \ucc44\ud305 \ud0c0\uc774\ud551\uacfc \ud654\uba74\uc5d0 \ucd9c\ub825\ud558\ub294 \ubd80\ubd84\uc744 \ubd84\ub9ac\uc2dc\ud0b5\ub2c8\ub2e4.<br>Use threads to separate the chat typing process from the screen output.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            Thread receiveThread = new Thread(() -&gt; {\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();<\/code><\/pre>\n\n\n\n<p>1)\uc11c\ubc84\ub85c \ubd80\ud130 \uba54\uc138\uc9c0\ub97c \uc2e4\uc2dc\uac04\uc73c\ub85c \ubc1b\ub294 \uc2a4\ub808\ub4dc\uc785\ub2c8\ub2e4.<br>This is a thread that receives messages from the server in real time.<\/p>\n\n\n\n<p>2)\uc2a4\ub808\ub4dc\ub85c \ubd84\ub9ac\uc2dc\ud0a4\uc9c0 \uc54a\uc73c\uba74 \ud654\uba74\uc774 \uc0ac\uc6a9\uc790 \uc785\ub825\ub9cc \ubc1b\ub294 \ubd80\ubd84\uc5d0\uc11c \uba48\ucdb0\uc838 \uc788\uc2b5\ub2c8\ub2e4.<br>If you don&#8217;t separate it into a thread, the screen freezes while waiting for user input.<\/p>\n\n\n\n<p>3) \uc544\ub798\uc758 \ucf54\ub4dc\ub294 \ud574\ub2f9\ucee4\uc11c\uac00\uc704\uce58\ud55c \uc904\uc744 \uc0ad\uc81c\ud558\uace0 \ucee4\uc11c\ub97c \uac00\uc7a5 \uc67c\ucabd\uc73c\ub85c \uc62e\uae41\ub2c8\ub2e4.<br>The code below deletes the line where the cursor is currently located and moves the cursor to the far left.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>System.out.print(\"\\r\\033&#91;K\"); <\/code><\/pre>\n\n\n\n<p>4)\uc704\uc758 ANSI \uc774\uc2a4\ucf00\uc774\ud504 \ucf54\ub4dc \ud2b8\ub9ad\uc774 \ud544\uc694\ud55c \uc774\uc720\ub294 \uc774 \ucc44\ud305 \ud074\ub77c\uc774\uc5b8\ud2b8\uc5d0\uc11c \ud654\uba74\uc5d0 &#8220;\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message:&#8221; \ucd9c\ub825\ud558\ub294 \ubd80\ubd84\uc774 \ub450\uad70\ub370 \uc788\uc2b5\ub2c8\ub2e4.<br>The reason the ANSI escape code trick mentioned above is necessary is that this chat client outputs the text &#8220;Send message:&#8221; to the screen in two different places.<\/p>\n\n\n\n<p>5)\uc11c\ubc84\uc5d0\uc11c \uba54\uc138\uc9c0\uac00 \ub3c4\ucc29\ud558\uba74 &#8220;\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message:&#8221;\uac00 \ud654\uba74\uc5d0 \ucd9c\ub825\ub418\uace0 \ub0b4\uac00 \ucc44\ud305\ubb38\uc790\ub97c \uc785\ub825\ud558\uace0 \uc5d4\ud130\uce58\uba74 &#8220;\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message:&#8221;\uac00 \ub610 \ucd9c\ub825\ub429\ub2c8\ub2e4.<br>When a message arrives from the server, &#8220;Send message:&#8221; is displayed on the screen; after I type a chat message and press Enter, &#8220;Send message:&#8221; appears again.<\/p>\n\n\n\n<p>6)\uadf8\ub798\uc11c \uc704\uc758 \ucf54\ub4dc\uc5d0\uc11c\uc640 \uba54\uc778\uc2a4\ub808\ub4dc\uc5d0\uc11c System.out.print(&#8220;\\r\\033[K\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message: &#8220;); \uc774 \ucf54\ub4dc\ub294 \ud654\uba74\uc5d0 \uc911\ubcf5\ub418\ub294 \ubb38\uc790\uac00 \ubc1c\uc0dd\ud558\uc9c0 \uc54a\uac8c \uc815\ub9ac\ud558\ub294 \uc5ed\ud560\uc744 \ud569\ub2c8\ub2e4.<br>Therefore, in the code above, the line <code>System.out.print(\"\\r\\033[K\ubcf4\ub0bc \uba54\uc2dc\uc9c0 \/ Send message: \");<\/code> executed on the main thread serves to ensure that duplicate characters do not appear on the screen.<\/p>\n\n\n\n<p>7)\ub2e4\uc74c\ucf54\ub4dc\ub294 \uc5d4\ud130\uc785\ub825\uc774 \uc5c6\uc5b4\ub3c4 \ubc84\ud37c\uc5d0 \uc800\uc7a5\ub418\uc5b4 \uc788\ub294 \ubb38\uc790\ub97c \ud654\uba74\uc5d0 \ud45c\uc2dc\ud558\ub77c\ub294 \uc758\ubbf8\uc785\ub2c8\ub2e4<br>The following code instructs the system to display the characters stored in the buffer on the screen without requiring the Enter key to be pressed.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code> System.out.flush();<\/code><\/pre>\n\n\n\n<p>&#8212; \uba54\uc778\uc2a4\ub808\ub4dc\ub85c \uc0ac\uc6a9\uc790\uc758 \uc785\ub825\uc744 \ubc1b\uc544\uc11c \uc11c\ubc84\ub85c \uc804\uc1a1\ud569\ub2c8\ub2e4.<br>The main thread receives user input and transmits it to the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>            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            }<\/code><\/pre>\n\n\n\n<p>1)\ub370\uc774\ud130\ub97c \uc11c\ubc84\ub85c \ubcf4\ub0bc \uc218 \uc788\ub294 \uae30\ubcf8\uc801\uc778 &#8216;\ub124\ud2b8\uc6cc\ud06c \ucd9c\ub825 \ud1b5\ub85c(\uc2a4\ud2b8\ub9bc)&#8217;\ub97c \uc18c\ucf13\uc73c\ub85c\ubd80\ud130 \uc5bb\uc5b4\uc635\ub2c8\ub2e4.<br>You obtain a basic &#8216;network output channel (stream)&#8217; from the socket that allows you to send data to the server.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>PrintWriter out = new PrintWriter(socket.getOutputStream(), true);<\/code><\/pre>\n\n\n\n<p>1) \uc0ac\uc6a9\uc790\uc758 \ud0a4\ubcf4\ub4dc \uc785\ub825\uc744 \ubc1b\uc744 \uc2a4\uce90\ub108\ub97c \uc900\ube44\ud569\ub2c8\ub2e4.<br>Prepare a scanner to receive keyboard input from the user.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Scanner scanner = new Scanner(System.in);<\/code><\/pre>\n\n\n\n<p>2)while(true){ &#8230; }\ub294 \ub8e8\ud504\ubb38\uc744 \ubb34\ud55c \ubc18\ubcf5\ud558\uba74\uc11c \ub8e8\ud504 \ube14\ub85d\uc744 \uacc4\uc18d \uc2e4\ud589\ud569\ub2c8\ub2e4.<br><code>while(true){ ... }<\/code> repeatedly executes the loop block in an infinite loop.<\/p>\n\n\n\n<p>3)\uc0ac\uc6a9\uc790\uac00 \uae00\uc790 \uc785\ub825\ud6c4 \uc5d4\ud130\ub97c \ub204\ub974\uba74  userInput\ubcc0\uc218\uc5d0 \uac12\uc744 \ub2f4\uace0 \ub2e4\uc74c\uc904\ub85c \ub118\uc5b4\uac11\ub2c8\ub2e4.<br>When the user presses Enter after typing, the value is stored in the <code>userInput<\/code> variable, and the cursor moves to the next line.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String userInput = scanner.nextLine();<\/code><\/pre>\n\n\n\n<p>4)\uc2e4\uc81c \uc18c\ucf13\uc744 \ud1b5\ud574 \ub124\ud2b8\uc6cc\ud06c\ub85c \ub370\uc774\ud130\ub97c \uc804\ub2ec\ud569\ub2c8\ub2e4.<br>It transmits data over the network via an actual socket.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>out.println(userInput);<\/code><\/pre>\n\n\n\n<p>5)exit\ub97c \uc785\ub825\ud558\uba74 \ub8e8\ud504 \uad6c\ubb38\uc744 \ube60\uc838\ub098\uac11\ub2c8\ub2e4.<br>Entering &#8216;exit&#8217; exits the loop.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (\"exit\".equalsIgnoreCase(userInput)) { break; }<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ud83d\udc49\ud83c\udffb \ucf54\ub4dc \uc124\uba85 \/ Code Explanation \u2714\ufe0f Client.java &#8212; nodejs \uc11c\ubc84\ub791 \ud638\uc2a4\ud2b8\uc640 \ud3ec\ud2b8\ub97c \ub3d9\uc77c\ud558\uac8c \ub9de\ucda5\ub2c8\ub2e4.Match the host and port to those of the Node.js server. &#8212; \uc2a4\ub808\ub4dc\ub97c \uc0ac\uc6a9\ud558\uc5ec \ucc44\ud305 \ud0c0\uc774\ud551\uacfc \ud654\uba74\uc5d0 \ucd9c\ub825\ud558\ub294 \ubd80\ubd84\uc744 \ubd84\ub9ac\uc2dc\ud0b5\ub2c8\ub2e4.Use threads to separate the chat typing process from the screen output. 1)\uc11c\ubc84\ub85c \ubd80\ud130 \uba54\uc138\uc9c0\ub97c \uc2e4\uc2dc\uac04\uc73c\ub85c \ubc1b\ub294 \uc2a4\ub808\ub4dc\uc785\ub2c8\ub2e4.This is a thread that [&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-6550","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\/6550","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=6550"}],"version-history":[{"count":19,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/6550\/revisions"}],"predecessor-version":[{"id":6572,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/6550\/revisions\/6572"}],"wp:attachment":[{"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=6550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=6550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.freelifemakers.org\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=6550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}