{"id":2903,"date":"2019-06-30T10:06:53","date_gmt":"2019-06-30T01:06:53","guid":{"rendered":"https:\/\/now0930.pe.kr\/wordpress\/?p=2903"},"modified":"2019-06-30T10:07:20","modified_gmt":"2019-06-30T01:07:20","slug":"%eb%94%94%ec%a7%80%ed%84%b8-%ec%95%a1%ec%9e%90-%ea%b0%9c%ec%84%a0","status":"publish","type":"post","link":"https:\/\/now0930.pe.kr\/wordpress\/%eb%94%94%ec%a7%80%ed%84%b8-%ec%95%a1%ec%9e%90-%ea%b0%9c%ec%84%a0\/","title":{"rendered":"\ub514\uc9c0\ud138 \uc561\uc790 \uac1c\uc120"},"content":{"rendered":"\n<p>\ucd9c\uc7a5\uae30\uac04 \ub0a8\ub294 \uc2dc\uac04\uc744 \ubcf4\ub0b4\uae30 \uc704\ud574, \uc804\uc5d0 \ub9cc\ub4e4\ub358 \ub514\uc9c0\ud138 \uc561\uc790\ub97c \ub2e4\uc2dc \uc190\ubcf4\uae30\ub85c \ud588\ub2e4. \uc804\uc5d0 \uc81c\uc791\ud55c \ud504\ub85c\uadf8\ub7a8\uc740 \ub2e4\uc74c \ubb38\uc81c\uc810\uc744 \uac00\uc9c0\uace0 \uc788\ub2e4.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>\uc11c\ubc84\uac00 \uc800\uc7a5\ud55c \ud30c\uc77c \uc774\ub984\uc744 \ubc1b\uc744 \uc218 \uc5c6\uc74c.<\/li><li>\uac04\ud639 200\uac1c \uc911 \uba87 \uac1c \ube7c\uba39\uace0 \ub2e4\uc6b4\ub85c\ub4dc.<\/li><li>\uc571\uc774 \ud558\ub8e8 \uc911 \ud2b9\uc815 \uc2dc\uac01 \ud30c\uc77c\uc744 \ubc1b\uae30 \ub54c\ubb38\uc5d0, \uc571\uc774 \uc815\uc9c0\ub418\uba74 \ub2e4\uc74c\ub0a0\uae4c\uc9c0 \uae30\ub2e4\ub824\uc57c \ud568.<\/li><\/ol>\n\n\n\n<p>1, 2 \ubc88\uc744 \uc218\uc815\ud588\uace0 3\ubc88\ub3c4 \ud560 \uc218 \uc788\uc73c\ub098 \ub108\ubb34 \ub9ce\uc774 \ub72f\uc5b4 \uace0\uccd0\uc57c \ud558\ubbc0\ub85c \uc548\ud588\ub2e4. socket\uc744 \uc5f4\uc5b4 \ub2e8\uc21c \ud30c\uc77c\ub9cc \ubc1b\uc558\uc73c\ub098, text\ub97c \uc804\uc1a1\ud558\uc5ec 1. \uc804\uccb4 \ud30c\uc77c \uac1c\uc218 \uc804\uc1a1, 2. i\ubc88\uc9f8 \ud30c\uc77c \uc774\ub984, \ud06c\uae30 \uc804\uc1a1, 3. i\ubc88\uc9f8 \ud30c\uc77c \ub0b4\uc6a9 \uc804\uc1a1\uc73c\ub85c \ubc14\uafe8\ub2e4. \uc774 socket\uc744 \uc774\uc6a9\ud558\uc5ec \uc571\uc5d0 \ud2b9\uc815 \ubc84\ud2bc\uc744 \ub9cc\ub4e4\uc5b4 \uc11c\ubc84\uc5d0\uc11c \ud30c\uc77c\uc744 \uc5c5\ub370\uc774\ud2b8, \uc804\uc1a1\ud558\uac8c \ud560 \uc218 \uc788\ub2e4. \ub300\ub7b5 \uc544\ub798\uc640 \uac19\uc774 \uad6c\uc131\ud588\ub2e4.<\/p>\n\n\n\n<p class=\"wp-block-pdfemb-pdf-embedder-viewer\"><\/p>\n\n\n\n<p>socket \uc73c\ub85c \ud30c\uc77c\uc744 \ubcf4\ub0b8 \ud6c4  \ub2e4\uc74c\uc5d0 \ub2e4\uc2dc \uc4f0\uae30\uc704\ud574 close \uc548\ud588\ub2e4. \uc774\ub807\uac8c \ud558\uba74 \ubc1b\ub294 \ucabd\uc774 \uc5bc\ub9c8\ub098 \ubc1b\uc744\uc9c0 \ubaa8\ub974\uae30 \ub54c\ubb38\uc5d0, \ud06c\uae30\ub97c \uc804\uc1a1\ud588\ub2e4.<\/p>\n\n\n\n<figure class=\"wp-block-embed\"><div class=\"wp-block-embed__wrapper\">\nhttps:\/\/stackoverflow.com\/questions\/4886293\/socket-input-stream-hangs-on-final-read-best-way-to-handle-this\n<\/div><\/figure>\n\n\n\n<p>\ud30c\uc77c \uc804\uc1a1 \ud6c4, \ubc14\ub85c \ud14d\uc2a4\ud2b8\ub97c \ubcf4\ub0b4\uba74 40\uac1c \uc815\ub3c4 \ud30c\uc77c\uc744 \ubcf4\ub0b4\uace0 \ud55c\uc5c6\uc774 \uae30\ub2e4\ub9b0\ub2e4. \uc774 \uacbd\uc6b0 \ub2e4\uc2dc \ubb34\uc5c7\uc778\uac00 \uc804\uc1a1\ud558\ub3c4\ub85d \ud574\uc918\uc57c \ud558\ub294\ub370, \uadc0\ucc2e\uace0 \uc5b4\ub824\uc6cc \uadf8\ub0e5 \ub51c\ub808\uc774\ub97c \uc37c\ub2e4. \uc774 \ubb3c\uac74\uc744 \ub354 \ub72f\uc5b4 \uace0\uce58\uace0 \uc2f6\uc9c4 \uc54a\ub2e4. \ub2f4\uc5d4 \ubb34\uc5c7\uc744 \ud574\uc57c \ud558\ub098?<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/\uc2a4\ud06c\ub9b0\uc0f7-2019-06-30-09-38-20-1024x576.png\" alt=\"\" class=\"wp-image-2915\" srcset=\"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/\uc2a4\ud06c\ub9b0\uc0f7-2019-06-30-09-38-20-1024x576.png 1024w, https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/\uc2a4\ud06c\ub9b0\uc0f7-2019-06-30-09-38-20-300x169.png 300w, https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/\uc2a4\ud06c\ub9b0\uc0f7-2019-06-30-09-38-20-768x432.png 768w, https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/\uc2a4\ud06c\ub9b0\uc0f7-2019-06-30-09-38-20.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\" style=\"text-align:left\"><em>\uc11c\ubc84 \ucf54\ub4dc<\/em><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package sender;\n\nimport java.io.BufferedInputStream;\nimport java.io.BufferedReader;\nimport java.io.File;\nimport java.io.FileInputStream;\nimport java.io.IOException;\nimport java.io.InputStreamReader;\nimport java.io.OutputStream;\nimport java.io.PrintWriter;\nimport java.net.ServerSocket;\nimport java.net.Socket;\nimport java.text.SimpleDateFormat;\nimport java.util.Date;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\npublic class Main {\n\tpublic static void main(String[] args) {\n\n\t\tServerSocket serverSocket = null;\n\/\/\t\tfinal String FILE_TO_SEND = \"\/home\/now0930\/tempPhoto\/1234.jpg\";\n\/\/\t\tbyte[] myFileByteArray;\n\t\tFileInputStream fis;\n\t\tBufferedInputStream bis;\n\/\/\t\tFile myFileToSend;\n\t\tFile myPhotoDirectory;\n\t\tFile[] myFiles;\n\/\/\t\tint i = 0;\n\t\tString responderStr;\n\t\tint action = 0;\n\t\tfinal int DEFAULT_BUFFER_SIZE = 1000000;\n\n\t\tboolean myDirectoryWasUpdated = false;\n\t\tboolean ended = false;\n\t\tint fileIndex = 0;\n\t\t\/*\n\t\t * try { serverSocket = new ServerSocket(9998); System.out.println(getTime() +\n\t\t * \"\uc11c\ubc84\uac00 \uc900\ube44\ub418\uc5c8\uc74c.\");\n\t\t * \n\t\t * } \/\/ try catch (IOException e) { e.printStackTrace(); } \/\/ catch\n\t\t *\/\n\n\t\t\/\/ myFileToSend = new File(FILE_TO_SEND);\n\t\t\/\/ myFileByteArray = new byte[(int)myFileToSend.length()];\n\n\/\/\t\twhile (true) {\n\t\t\/\/ \uc11c\ubc84\uc5d0\uc11c \uc2e4\ud589 \ubc29\ubc95..\n\t\t\/\/ script \uc2e4\ud589 \ubd84.\n\t\t\/\/ \uc11c\ubc84\uac00 \ud2b9\uc815 \ub514\ub809\ud1a0\ub9ac\uc5d0 \ud2b9\uc815 \uc2dc\uac01\uc5d0 jpg file\uc744 \uad50\uccb4, \uae30\uc874 \ud30c\uc77c\uc744 \uc0ad\uc81c\n\t\t\/\/ \uc885\ub8cc \ud6c4, updated\ub780 \ud30c\uc77c\uc744 \ub9cc\ub4e6.\n\t\t\/\/ \ud2b9\uc815 \uc2dc\uac01\uc5d0 cron\uc774 \uc2e4\ud589\n\n\t\t\/\/ java \uc2e4\ud589 \ubd84..\n\t\t\/\/ \ub514\ub809\ud1a0\ub9ac\uc5d0\uc11c updated\ub97c \ucc3e\uc74c..\n\t\t\/\/ updated\ub780 \ud30c\uc77c\uc744 \ucc3e\uc73c\uba74,\n\t\t\/\/ socket.accept()\ub85c \uc77c\uc815\uc2dc\uac04 \uae30\ub2e4\ub9b0 \ud6c4,\n\t\t\/\/ \uc751\ub2f5.\n\t\t\/\/ \ud30c\uc77c 1\ud68c \uc804\uc1a1\ud6c4 process \uc885\ub8cc.\n\t\t\/\/ updated \ud30c\uc77c \uc0ad\uc81c\n\n\t\t\/\/ updated \ud30c\uc77c\uc774 \uc5c6\uc73c\uba74\n\t\t\/\/ \ud504\ub85c\uc138\uc2a4 \uc885\ub8cc..\n\n\t\t\/\/ file \uc785\ucd9c\ub825..\n\t\t\/\/ \ub514\ub809\ud1a0\ub9ac\uc758 \ud30c\uc77c\uc744 \ub9ac\uc2a4\ud2b8\ub85c \ub9cc\ub4dc\ub294 \ubd80\ubd84.\n\t\t\/\/ directory\uc758 \ud30c\uc77c \ucc3e\uae30\n\n\t\tmyPhotoDirectory = new File(\"\/home\/now0930\/tempPhoto\");\n\t\tmyFiles = myPhotoDirectory.listFiles();\n\t\tSystem.out.println(\"\ucd9c\ub825\" + myFiles.length);\n\n\t\tmyDirectoryWasUpdated = false;\n\t\t\/\/ updated \ud30c\uc77c \ud655\uc778..\n\t\tfor (int j = 0; j &lt; myFiles.length; j++) {\n\t\t\tif (myFiles[j].getName().equals(\"updated\")) {\n\t\t\t\tmyDirectoryWasUpdated = true;\n\t\t\t\tSystem.out.println(\"\ud30c\uc77c\uc774 \uc5c5\ub370\uc774\ud2b8 \ub418\uc5c8\uc74c\");\n\t\t\t\tbreak;\n\t\t\t}\n\t\t} \/\/ for\n\n\t\t\/\/ \ud30c\uc77c\uc774 \uc5c5\ub370\uc774\ud2b8 \ub418\uc9c0 \uc54a\uc73c\uba74, while\ub97c \ube60\uc838 \ub098\uac10..\n\t\tif (myDirectoryWasUpdated) {\n\n\t\t\ttry {\n\t\t\t\tserverSocket = new ServerSocket(9998);\n\t\t\t\tSystem.out.println(getTime() + \"\uc11c\ubc84\uac00 \uc900\ube44\ub418\uc5c8\uc74c.\");\n\t\t\t\tSocket socket = serverSocket.accept();\n\t\t\t\tSystem.out.println(getTime() + socket.getInetAddress() + \"\ub85c\ubd80\ud130 \uc5f0\uacb0\uc774 \ub4e4\uc5b4\uc62e\");\n\t\t\t\tInputStreamReader responder = new InputStreamReader(socket.getInputStream());\n\t\t\t\tBufferedReader bfResponder = new BufferedReader(responder);\n\t\t\t\tOutputStream out = socket.getOutputStream();\n\t\t\t\tPrintWriter responderToClient = new PrintWriter(socket.getOutputStream());\n\t\t\t\tString[] temp;\n\t\t\t\t\/\/ \ud3f4\ub354\uc758 \ud30c\uc77c \ub9ac\uc2a4\ud2b8 \uc804\uc1a1\n\t\t\t\tSystem.out.println(getTime() + \"\uc11c\ubc84\uac00 \ub300\uae30\uc911\");\n\t\t\t\twhile (true) {\n\t\t\t\t\tresponderStr = bfResponder.readLine();\n\t\t\t\t\tSystem.out.println(responderStr);\n\t\t\t\t\tPattern patFilename = Pattern.compile(\"send [0-9]{1,3} filename\");\n\t\t\t\t\tPattern patFileContents = Pattern.compile(\"send [0-9]{1,3} filecontents\");\n\t\t\t\t\tPattern patClose = Pattern.compile(\"close\");\n\t\t\t\t\tMatcher matFilename = patFilename.matcher(responderStr);\n\t\t\t\t\tMatcher matFileContents = patFileContents.matcher(responderStr);\n\t\t\t\t\tMatcher matClose = patClose.matcher(responderStr);\n\t\t\t\t\tif (responderStr.equals(\"how many files has you?\")) {\n\t\t\t\t\t\taction = 1;\n\t\t\t\t\t} else if (matFilename.find()) {\n\t\t\t\t\t\t\/\/ \ud30c\uc77c \uc778\ub371\uc2a4\ub97c \uc138\ubd80 \ud310\ub2e8.\n\t\t\t\t\t\ttemp = responderStr.split(\" \");\n\t\t\t\t\t\tfileIndex = Integer.parseInt(temp[1]);\n\t\t\t\t\t\t\/\/ action 4: client\uc5d0 END\ub97c \ubcf4\ub0b4\uace0 \uc18c\ucf13 \ub2eb\uc74c.\n\t\t\t\t\t\t\/\/ updated\ub97c \uc9c0\uc6c0\n\t\t\t\t\t\t\/\/ action 2: client\ub85c \ud30c\uc77c\uc744 \ubcf4\ub0c4.\n\t\t\t\t\t\taction = (fileIndex >= myFiles.length) ? 4 : 2;\n\n\t\t\t\t\t} else if (matFileContents.find()) {\n\t\t\t\t\t\taction = 3;\n\t\t\t\t\t} \/\/ match fileContents\n\n\t\t\t\t\tswitch (action) {\n\t\t\t\t\t\/\/ 1: how many files have you?\n\t\t\t\t\tcase 1:\n\t\t\t\t\t\tresponderToClient.println(\"FileList: \" + myFiles.length);\n\t\t\t\t\t\tresponderToClient.flush();\n\t\t\t\t\t\tSystem.out.println(\"\ud30c\uc77c \uac2f\uc218 \ubcf4\ub0c4.\");\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\/\/ send ??? filename\n\t\t\t\t\tcase 2:\n\t\t\t\t\t\tSystem.out.println(\"\uc815\uaddc\uc2dd \uac10\uc9c0\ub428\");\n\/\/\t\t\t\t\t\ttemp = responderStr.split(\" \");\n\/\/\t\t\t\t\t\tfileIndex = Integer.parseInt(temp[1]);\n\n\t\t\t\t\t\t\/\/ updated \ud30c\uc77c\uc744 \ubcf4\ub0b4\uc9c0 \uc54a\uace0, \uadf8\ub9bc \ud30c\uc77c\ub9cc \ubcf4\ub0c4.\n\t\t\t\t\t\t\/\/ \ud30c\uc77c \uc774\ub984\uacfc \ud06c\uae30\ub97c \ubcf4\ub0c4.\n\t\t\t\t\t\t\/\/ \ud06c\uae30\ub97c \ubcf4\ub0b4\uc57c \ubc1b\ub294\ucabd\uc5d0\uc11c \uc5bc\ub9c8\ub098 \ubc1b\uc744\uc9c0 \uc54e. \uc774\ud6c4 \ud310\ub2e8.\n\t\t\t\t\t\tif (myFiles[fileIndex].getName().contains(\"jpg\") || myFiles[fileIndex].getName().contains(\"png\")\n\t\t\t\t\t\t\t\t|| myFiles[fileIndex].getName().contains(\"JPEG\")\n\t\t\t\t\t\t\t\t|| myFiles[fileIndex].getName().contains(\"JPG\")) {\n\t\t\t\t\t\t\tresponderToClient.println(\"Filename: \" + myFiles[fileIndex].getName() + \", FileSize: \"\n\t\t\t\t\t\t\t\t\t+ myFiles[fileIndex].length());\n\t\t\t\t\t\t\tSystem.out.println(\"Filename: \" + myFiles[fileIndex].getName() + \", FileSize: \"\n\t\t\t\t\t\t\t\t\t+ myFiles[fileIndex].length());\n\t\t\t\t\t\t\tresponderToClient.flush();\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\tresponderToClient.println(\"Filename: this is no image file\");\n\t\t\t\t\t\t\tresponderToClient.flush();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t\/\/ send ??? filecontents\n\t\t\t\t\tcase 3:\n\t\t\t\t\t\t\/*\n\t\t\t\t\t\t * if (fileIndex >= myFiles.length) myFileByteArray = null; else myFileByteArray\n\t\t\t\t\t\t * = new byte[(int) myFiles[fileIndex].length()];\n\t\t\t\t\t\t *\/\n\t\t\t\t\t\t\/\/ fis = new FileInputStream(myFileToSend);\n\t\t\t\t\t\t\/\/ myFiles.length \uac00 0\uc77c \uacbd\uc6b0, myFiles[i]\uac00 arrayOutOfBoundsException\uc774 \uc788\uc74c..\n\n\t\t\t\t\t\t\/**\n\t\t\t\t\t\t * if(myFiles.length !=0 &amp;&amp; myFiles[i].exists()&amp;&amp; \/\/updated\ub780 \ud30c\uc77c\uc744 \uc548\ubcf4\ub0b4\uac8c \uc81c\uc678..\n\t\t\t\t\t\t * myFiles[i].getName()!=\"updated\" &amp;&amp; \/\/\ud655\uc7a5\uc790\uac00 jpg\ub9cc \ubcf4\ub0c4..\n\t\t\t\t\t\t * myFiles[i].getName().matches(\".*\\\\.jpg\")){\n\t\t\t\t\t\t **\/\n\t\t\t\t\t\tint readBytes;\n\t\t\t\t\t\tbyte[] buffer = new byte[DEFAULT_BUFFER_SIZE];\n\t\t\t\t\t\tfis = new FileInputStream(myFiles[fileIndex].getPath());\n\t\t\t\t\t\tbis = new BufferedInputStream(fis);\n\t\t\t\t\t\t\/\/ bis.read(myFileByteArray,0,myFileByteArray.length);\n\t\t\t\t\t\t\/\/ bis.read(myFileByteArray, 0, myFileByteArray.length);\n\t\t\t\t\t\twhile ((readBytes = fis.read(buffer)) > 0) {\n\t\t\t\t\t\t\tout.write(buffer, 0, readBytes);\n\t\t\t\t\t\t\tout.flush();\n\t\t\t\t\t\t}\n\t\t\t\t\t\tSystem.out.println(getTime() + myFiles[fileIndex].getPath() + \"\ub97c \ubcf4\ub0c5\ub2c8\ub2e4.\");\n\t\t\t\t\t\tSystem.out.println(fileIndex + \"\ubc88\uc9f8 \ud30c\uc77c \uc804\uc1a1 \uc644\ub8cc.\");\n\t\t\t\t\t\t\/\/ out, socket\uc744 \uacc4\uc18d \uc0ac\uc6a9\ud558\uae30 \uc704\ud574 \ub2eb\uc9c0 \uc54a\uc74c.\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\tcase 4:\n\t\t\t\t\t\t\/\/ \ubcf4\ub0bc \ud30c\uc77c\uc744 \uac00\uc9c0\uace0 \uc788\ub294\uc9c0 \ud655\uc778.\n\t\t\t\t\t\t\/\/ \ub9c8\uc9c0\ub9c9 \ud30c\uc77c\uc774\uba74 client\ub85c END\ub97c \ubcf4\ub0b4\uace0 updated \ud30c\uc77c \uc0ad\uc81c.\n\t\t\t\t\t\tif (fileIndex >= myFiles.length) {\n\t\t\t\t\t\t\tresponderToClient.println(\"End\");\n\t\t\t\t\t\t\tresponderToClient.flush();\n\t\t\t\t\t\t\t\/\/ App\uc73c\ub85c END\ub97c \ubcf4\ub0b4\uace0 updated \ud30c\uc77c \uc0ad\uc81c.\n\t\t\t\t\t\t\tended = true;\n\t\t\t\t\t\t\t\/\/ \ud30c\uc77c \uc0ad\uc81c \ud655\uc778..\n\t\t\t\t\t\t\t\/\/ j\uac12\uc744 \uae30\uc5b5\ud558\uace0 \uc788\ub2e4, \ub098\uc911\uc5d0 \uc0ad\uc81c..\n\t\t\t\t\t\t\tfor (int j = 0; j &lt; myFiles.length; j++) {\n\t\t\t\t\t\t\t\tif (myFiles[j].getName().equals(\"updated\")) {\n\t\t\t\t\t\t\t\t\tmyDirectoryWasUpdated = false;\n\t\t\t\t\t\t\t\t\tmyFiles[j].delete();\n\t\t\t\t\t\t\t\t\tSystem.out.println(\"\ud30c\uc77c\uc774 \uc0ad\uc81c\ub418\uc5c8\uc74c\");\n\t\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t\t} \/\/ if\n\t\t\t\t\t\t\t} \/\/ for\n\n\t\t\t\t\t\t} \/\/ if fileIndex..\n\t\t\t\t\t\t\/\/ switch, case break;\n\t\t\t\t\t\tbreak;\n\n\t\t\t\t\t}\/\/ switch\n\t\t\t\t\t\/\/switch\ubb38 \uc2e4\ud589\uc2dc \ub2e4\uc2dc readline\uc744 \ud558\uc9c0 \uc54a\uae30 \uc704\ud574, \uc5ec\uae30\uc5d0\uc11c \uac15\uc81c \ube0c\ub808\uc774\ud06c\n\t\t\t\t\tif (ended) {\n\t\t\t\t\t\t\/\/ close socket\n\t\t\t\t\t\tsocket.close();\n\t\t\t\t\t\tSystem.out.println(\"Socket \ub2eb\ud798\");\n\t\t\t\t\t\t\/\/ while break\n\t\t\t\t\t\tbreak;\n\t\t\t\t\t}\n\t\t\t\t} \/\/ while\n\n\t\t\t} \/\/ try\n\t\t\tcatch (IOException e) {\n\t\t\t\te.printStackTrace();\n\t\t\t} \/\/ catch\n\n\t\t\tcatch (Exception e) {\n\t\t\t\te.printStackTrace();\n\n\t\t\t} \/\/ catch\n\n\t\t} \/\/ if myDirectory is updated?\n\n\t}\/\/ main\n\n\tprivate static String getTime() {\n\t\t\/\/ TODO Auto-generated method stub\n\t\tSimpleDateFormat f = new SimpleDateFormat(\"[hh:mm:ss]\");\n\t\treturn f.format(new Date());\n\t}\/\/ getTime\n\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\ud074\ub77c\uc774\uc5b8\ud2b8 \ucf54\ub4dc(\uc548\ub4dc\ub85c\uc774\ub4dc \uc571, Receiver \ud074\ub798\uc2a4)<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>package tk.now0930.photoframev2;\n\nimport android.content.Context;\n\nimport java.io.BufferedOutputStream;\nimport java.io.BufferedReader;\nimport java.io.File;\nimport java.io.FileOutputStream;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\nimport java.io.PrintWriter;\nimport java.net.ConnectException;\nimport java.net.Socket;\nimport java.util.Calendar;\nimport java.util.GregorianCalendar;\nimport java.util.Locale;\nimport java.util.Timer;\nimport java.util.TimerTask;\nimport java.util.concurrent.TimeUnit;\nimport java.util.regex.Matcher;\nimport java.util.regex.Pattern;\n\n\/**\n * Created by now0930 on 17. 9. 23.\n *\/\n\npublic class LongTermTaskReceive {\n    Timer myScheduler;\n    TimerTask mLongTermTaskReceive;\n    Calendar currentTime;\n    public static final int ServerPort = 9999;\n    String FileToReceive;\n    byte[] myByteArray;\n    File file;\n    FileOutputStream fos;\n    BufferedOutputStream bos;\n    int bytesRead;\n    int current = 0;\n    private static int fileIndex = 0;\n    long passedCheck, displayCheck;\n    Context myContext;\n    static int state = 0;\n\n    LongTermTaskReceive(Context from) {\n        this.myContext = from;\n        currentTime = ((PhotoFrame) myContext).getCalendar().getInstance();\n        myScheduler = ((PhotoFrame) myContext).getTimerRecive();\n        fos = null;\n        bos = null;\n\n        mLongTermTaskReceive = new TimerTask() {\n            @Override\n            public void run() {\n                \/\/\ub9e4 \ud134 \uc2dc\uac01\uc744 \ubc1b\uae30\uc704\ud574, run\uc548\uc5d0\ub3c4 \uc124\uc815.\n                currentTime = ((PhotoFrame) myContext).getCalendar().getInstance();\n                \/\/passedCheck>0 \uc774\uba74 \uc9c0\uc815 \uc2dc\uac01\uc774 \uc9c0\ub0ac\uc74c..\n                passedCheck = currentTime.getTimeInMillis() - Constants.mReceiveTimeStop.getTimeInMillis();\n                \/\/display\ud560 \uc2dc\uac04\uc778 \uc9c0 \ud655\uc778..\n                \/\/app\uc774 \ucda9\ub3cc\ud568..\n                displayCheck = currentTime.getTimeInMillis() - Constants.mShorttermTimeStart.getTimeInMillis();\n\n                if (displayCheck &lt; 0) {\n                    try {\n                        String ServerIP = \"192.168.1.73\";\n                        System.out.println(\"S:Connecting\");\n                        Socket socket = new Socket(ServerIP, 9998);\n                        \/\/\ud30c\uc77c \uc774\ub984, \uc694\uccad\ud558\uae30 \uc704\ud574 \uc5f0\uacb0 \uc124\uc815.\n                        PrintWriter askToServer = new PrintWriter(socket.getOutputStream());\n                        InputStream in = socket.getInputStream();\n                        String deciderStr;\n                        InputStreamReader decieder = new InputStreamReader(socket.getInputStream());\n                        BufferedReader bfDecieder = new BufferedReader(decieder);\n                        \/\/\uc815\uaddc\ud45c\ud604\uc2dd\uc744 \uc0ac\uc6a9\ud558\uae30 \uc704\ud574 Matcher, Pattern \uc124\uc815.\n                        String[] fileToAccept;\n                        String fileNameTmp = \"\";\n                        String fileSizeTmp = \"\";\n                        int fileIndex = 0;\n                        int i = 0;\n                        while (true) {\n                            switch (state) {\n                                \/\/\ucc98\uc74c \ud30c\uc77c \uac2f\uc218\uc694\uccad\n                                \/\/\ubcf4\ub0c8\uc744 \ub54c \ubc14\ub85c readline \uc2e4\ud589.\n                                \/\/\uac19\uc774 \uc2e4\ud589\ud558\uc9c0 \uc54a\uc73c\uba74 \ud55c\uc5c6\uc774 \uae30\ub2e4\ub9b4 \uc218 \uc788\uc74c.\n\n                                case 0:\n                                    askToServer.println(\"how many files has you?\");\n                                    askToServer.flush();\n                                    deciderStr = bfDecieder.readLine();\n                                    Pattern patFilelist = Pattern.compile(\"FileList: \");\n                                    Matcher matFilelist = patFilelist.matcher(deciderStr);\n                                    if (matFilelist.find()) {\n                                        fileToAccept = deciderStr.split(\" \");\n                                        fileIndex = Integer.parseInt(fileToAccept[1]);\n                                        state = 1;\n                                    }\n                                    \/\/\ud30c\uc77c \uac2f\uc218 \uc218\uc2e0 \ub300\uae30\n                                    \/\/ \uc11c\ubc84\uac00 \uc804\uc1a1\ud55c \uba54\uc138\uc9c0\ub97c \ubd84\uc11d\ud558\ub294 \ubd80\ubd84..\n                                    \/\/\ub2e4\uc74c\uc5d0 \uc5b4\ub290 \uc2a4\ud14c\uc774\ud2b8\ub85c \uac08\uc9c0 \uacb0\uc815.\n                                    \/\/\uc5ec\uae30\ub85c \ub4e4\uc5b4\uc624\uba74 \ud56d\uc0c1 \uae30\ub2e4\ub9b4 \uc218 \uc788\uc74c.\n                                    \/\/\uc804 \uc2a4\ud15d\uc5d0\uc11c \uba54\uc138\uc9c0\ub97c \ud558\ub098 \ubcf4\ub0b4\uc57c \ub428.\n                                    \/\/\uc5ec\uae30\ub85c \ub4e4\uc5b4\uc624\uba74 \uc77c\ub2e8 \ub2e4\ub978 case\ub85c \ub098\uac10.\n                                    \/\/i\ubc88\uc9f8 \ud30c\uc77c \uc774\ub984 \uc694\uccad, \ubc1b\uc740 \ud6c4 i \uc99d\uac00.\n                                    break;\n                                case 1:\n                                    askToServer.println(\"send \" + i + \" filename\");\n                                    askToServer.flush();\n                                    deciderStr = bfDecieder.readLine();\n                                    Pattern patFilename = Pattern.compile(\"Filename:( .*.(jpg|png|JPG|JPEG)), FileSize: ([0-9]{0,10})\");\n                                    Matcher matFilename = patFilename.matcher(deciderStr);\n                                    Pattern patEnd = Pattern.compile(\"End\");\n                                    Matcher matEnd = patEnd.matcher(deciderStr);\n                                    if (matFilename.find()) {\n                                        fileNameTmp = matFilename.group(1);\n                                        fileSizeTmp = matFilename.group(3);\n                                        \/\/fileSizeTmp \ud06c\uae30 \ubc84\ud37c\ub97c \ub9cc\ub4e6.\n                                        \/\/socket\uc5d0\uc11c \uc624\ub294 \uc785\ub825\uc744 \ubc84\ud37c\ub85c \uc368\uc8fc\uace0, \ub9c8\uc9c0\ub9c9\uae4c\uc9c0 \uc77d\uc73c\uba74 \ud30c\uc77c\ub85c \uc368\uc90c.\n                                        myByteArray = new byte[Integer.parseInt(fileSizeTmp)];\n                                        System.out.println(\"filename is: \" + fileNameTmp);\n                                        System.out.println(\"filesize is: \" + fileSizeTmp);\n                                        state = 2;\n                                    } else if (matEnd.find()) {\n                                        state = 5;\n                                        break;\n                                    } else {\n                                        i++;\n                                        state = 1;\n                                    }\n                                    break;\n                                \/\/\uc774\ubbf8 \ubcf4\ub0b4\uc9c4 \ud30c\uc77c \uac2f\uc218\ub97c \ubcf4\uace0 \ud30c\uc77c \uc218\uc2e0.\n                                case 2:\n                                    \/\/FileToReceive = \"receivedFile\" + fileIndex + \".jpg\";\n                                    askToServer.println(\"send \" + i + \" filecontents\");\n                                    askToServer.flush();\n\n                                    file = new File(Constants.FileDirectory, fileNameTmp);\n                                    fos = new FileOutputStream(file);\n                                    \/\/System.out.println(\"\ud30c\uc77c \ucd9c\ub825 \uc704\uce58\"+((PhotoFrame)myContext).getApplicationContext().getFilesDir());\n                                    System.out.println(\"\ud30c\uc77c \ucd9c\ub825 \uc704\uce58\" + file.getAbsoluteFile());\n                                    bos = new BufferedOutputStream(fos);\n                                    bytesRead = in.read(myByteArray, 0, myByteArray.length);\n                                    current = bytesRead;\n                                    do {\n                                        bytesRead = in.read(myByteArray, current, (myByteArray.length - current));\n                                        if (bytesRead >= 0)\n                                            current += bytesRead;\n                                        \/\/socekt\uc774 \ub04a\uae30\uba74 \ube60\uc800\ub098\uac10.\n\n                                        if (bytesRead == -1)\n                                            break;\n                                    } while (myByteArray.length > current);\n                                    bos.write(myByteArray, 0, current);\n                                    i++;\n                                    \/\/\ub2e4\uc74c\uc5d0 \uc18c\ucf13\uc744 \uc0ac\uc6a9\ud574\uc57c \ud558\ubbc0\ub85c \ub04a\uc5b4\uc8fc\uba74 \uc548\ub428\n                                    \/\/bos.flush();\n                                    \/\/in.close();\n                                    \/\/\ud30c\uc77c\uc744 \uc218\uc2e0\ud558\uace0 \uc788\ub2e4\uace0 \uba54\uc138\uc9c0\ub97c \ubcf4\ub0c4..\n                                    ((PhotoFrame) myContext).myHandler.sendEmptyMessage(1);\n                                    System.out.println(\"message..\ud30c\uc77c \uc218\uc2e0.\");\n                                    state = 1;\n                                    \/\/\ub108\ubb34 \ube60\ub974\uba74 socket \uc804\uc1a1\uc744 \uc783\uc5b4\ubc84\ub9bc.\n                                    \/\/\uc11c\ubc84\uac00 \ucda9\ubd84\ud558\uac8c \uc77d\uc744 \uc218 \uc788\ub3c4\ub85d \uc801\uc815 \uc2dc\uac04 \uc9c0\uc5f0.\n                                    TimeUnit.SECONDS.sleep(1);\n                                    break;\n\n                                \/\/\uc5f0\uacb0 \uc885\ub8cc.\n                                case 5:\n                                    System.out.println(\"\uc5f0\uacb0 \uc885\ub8cc\ub428.\");\n                                    in.close();\n                                    \/\/while\ubb38\uc744 \ub098\uac00\uba74 \ucc98\uc74c\ubd80\ud130 \ub2e4\uc2dc \uc2dc\uc791.\n                                    \/\/while loop\ub97c \uc720\uc9c0\ud558\uc5ec \uc5ec\uae30\uc5d0\uc11c close\ud558\uace0, state 6\uc5d0\uc11c \ub300\uae30.\n                                    state = 6;\n                                    break;\n                                case 6:\n                                    TimeUnit.SECONDS.sleep(10);\n                                    break;\n                            }\/\/switch\n\n                        } \/\/while\n\n\n                    }   \/\/try\n\n                    catch (ConnectException e) {\n                        e.printStackTrace();\n                    } \/\/catch, ConnectException\n\n                    catch (IOException e) {\n                        e.printStackTrace();\n                    }\/\/catch, IOException\n\n                    catch (Exception e) {\n                        e.printStackTrace();\n                    }   \/\/catch, Exception\n\n                }\/\/display \uc2dc\uac04 \ud655\uc778..\n\n                if (passedCheck > 0) {\n                    System.out.println(\"message...Receive \ud0c0\uc774\uba38\uac00 cancel..\");\n                    ((PhotoFrame) myContext).myTimerThread.setReceiveTimerFlagFLASE();\n                    cancel();\n                }\/\/if\n\n\n            }\/\/run\n        };\/\/TimerTask\n        \/\/\ud2b9\uc815 \uc2dc\uac01\uc5d0 \uc791\uc5c5 \uc2dc\ud589..\uc624\ud6c4 8\uc2dc 57\ubd84..\n        myScheduler.schedule(mLongTermTaskReceive, Constants.mReceiveTimeStart.getTime(), TimeUnit.MILLISECONDS.convert(3, TimeUnit.SECONDS));\n        System.out.println(\"ReceiveTime \uc2dc\uac01\uc740..\" + Constants.mReceiveTimeStart.getTime());\n    }\/\/LongTermTaskReceive\n\n\n}<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/PhotoFramev2.zip\">\uc548\ub4dc\ub85c\uc774\ub4dc \uc804\uccb4 \uc571<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\ucd9c\uc7a5\uae30\uac04 \ub0a8\ub294 \uc2dc\uac04\uc744 \ubcf4\ub0b4\uae30 \uc704\ud574, \uc804\uc5d0 \ub9cc\ub4e4\ub358 \ub514\uc9c0\ud138 \uc561\uc790\ub97c \ub2e4\uc2dc \uc190\ubcf4\uae30\ub85c \ud588\ub2e4. \uc804\uc5d0 \uc81c\uc791\ud55c \ud504\ub85c\uadf8\ub7a8\uc740 \ub2e4\uc74c \ubb38\uc81c\uc810\uc744 \uac00\uc9c0\uace0 \uc788\ub2e4. \uc11c\ubc84\uac00 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2917,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[12],"tags":[591,98,218,595],"class_list":["post-2903","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-12","tag-andorid","tag-java","tag-socket","tag-595"],"jetpack_featured_media_url":"https:\/\/now0930.pe.kr\/wordpress\/wp-content\/uploads\/2019\/06\/20190629_204554.jpg","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2903","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/comments?post=2903"}],"version-history":[{"count":4,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2903\/revisions"}],"predecessor-version":[{"id":2920,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/posts\/2903\/revisions\/2920"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media\/2917"}],"wp:attachment":[{"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/media?parent=2903"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/categories?post=2903"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/now0930.pe.kr\/wordpress\/wp-json\/wp\/v2\/tags?post=2903"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}