{"action":"created","issue":{"url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/4","labels_url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/4/labels{/name}","comments_url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/4/comments","events_url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/4/events","html_url":"https://github.com/Panesoft/insoltech-gps/issues/4","id":100867229,"number":4,"title":"Problema en programa de prueba.","user":{"login":"InsoltechFernando","id":13783656,"avatar_url":"https://avatars.githubusercontent.com/u/13783656?v=3","gravatar_id":"","url":"https://api.github.com/users/InsoltechFernando","html_url":"https://github.com/InsoltechFernando","followers_url":"https://api.github.com/users/InsoltechFernando/followers","following_url":"https://api.github.com/users/InsoltechFernando/following{/other_user}","gists_url":"https://api.github.com/users/InsoltechFernando/gists{/gist_id}","starred_url":"https://api.github.com/users/InsoltechFernando/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/InsoltechFernando/subscriptions","organizations_url":"https://api.github.com/users/InsoltechFernando/orgs","repos_url":"https://api.github.com/users/InsoltechFernando/repos","events_url":"https://api.github.com/users/InsoltechFernando/events{/privacy}","received_events_url":"https://api.github.com/users/InsoltechFernando/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-08-13T20:54:57Z","updated_at":"2015-08-14T01:24:04Z","closed_at":"2015-08-14T01:24:04Z","body":"Este ejemplo recibe bien los mensajes pero hay veces que tiene problemas cuando se cierra la conexion desde el lado del cliente y empieza a imprimir lineas vacias. Sera necesario matar el thread de alguna manera?\r\n-----------------------------------------------------------------------------------------------------------------------------------------\r\nimport java.io.BufferedReader;\r\nimport java.io.IOException;\r\nimport java.io.InputStreamReader;\r\nimport java.io.PrintWriter;\r\nimport java.net.ServerSocket;\r\nimport java.net.Socket;\r\n\r\npublic class Prueba {\r\n\r\n\tpublic static void main(String[] args) throws Exception {\r\n\t\tint myPort = 9000;\r\n\r\n\t\tif (args.length > 0) {\r\n\t\t\tmyPort = Integer.parseInt(args[0]);\r\n\t\t}\r\n\t\tSystem.out.println(\"Servidor escuchando en el puerto : \" + myPort);\r\n\t\t/*\r\n\t\t * catch (ArrayIndexOutofBoundsException e) { ... }\r\n\t\t */\r\n\t\tServerSocket listener = new ServerSocket(myPort);\r\n\t\ttry {\r\n\t\t\twhile (true) {\r\n\t\t\t\tnew Handler(listener.accept()).start();\r\n\t\t\t}\r\n\t\t} finally {\r\n\t\t\tlistener.close();\r\n\t\t}\r\n\t}\r\n\r\n\tprivate static class Handler extends Thread {\r\n\t\t// private String name;\r\n\t\tprivate Socket socket;\r\n\t\tprivate BufferedReader in;\r\n\t\tprivate PrintWriter out;\r\n\r\n\t\tpublic Handler(Socket socket) {\r\n\t\t\tthis.socket = socket;\r\n\t\t}\r\n\r\n\t\tpublic void run() {\r\n\t\t\ttry {\r\n\r\n\t\t\t\tin = new BufferedReader(new InputStreamReader(socket.getInputStream()));\r\n\t\t\t\tout = new PrintWriter(socket.getOutputStream(), true);\r\n\r\n\t\t\t\twhile (true) {\r\n\t\t\t\t\tchar cbuf[] = new char[5000];\r\n\t\t\t\t\tint CantCharRead;\r\n\r\n\t\t\t\t\tCantCharRead = in.read(cbuf); // .readLine();\r\n\t\t\t\t\tString input = new String(cbuf);\r\n\t\t\t\t\tSystem.out.println(\"Received: \" + input + \"_\" + Integer.toString(CantCharRead));\r\n\t\t\t\t\tout.println(input);\r\n\t\t\t\t}\r\n\t\t\t} catch (IOException e) {\r\n\t\t\t\tSystem.out.println(e);\r\n\t\t\t} finally {\r\n\r\n\t\t\t\ttry {\r\n\t\t\t\t\tsocket.close();\r\n\r\n\t\t\t\t} catch (IOException e) {\r\n\t\t\t\t}\r\n\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/comments/130909747","html_url":"https://github.com/Panesoft/insoltech-gps/issues/4#issuecomment-130909747","issue_url":"https://api.github.com/repos/Panesoft/insoltech-gps/issues/4","id":130909747,"user":{"login":"InsoltechFernando","id":13783656,"avatar_url":"https://avatars.githubusercontent.com/u/13783656?v=3","gravatar_id":"","url":"https://api.github.com/users/InsoltechFernando","html_url":"https://github.com/InsoltechFernando","followers_url":"https://api.github.com/users/InsoltechFernando/followers","following_url":"https://api.github.com/users/InsoltechFernando/following{/other_user}","gists_url":"https://api.github.com/users/InsoltechFernando/gists{/gist_id}","starred_url":"https://api.github.com/users/InsoltechFernando/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/InsoltechFernando/subscriptions","organizations_url":"https://api.github.com/users/InsoltechFernando/orgs","repos_url":"https://api.github.com/users/InsoltechFernando/repos","events_url":"https://api.github.com/users/InsoltechFernando/events{/privacy}","received_events_url":"https://api.github.com/users/InsoltechFernando/received_events","type":"User","site_admin":false},"created_at":"2015-08-14T01:24:04Z","updated_at":"2015-08-14T01:24:04Z","body":"Solucionado. Solo hacia falta verificar que el valor de in.read(cbuf) fuera >0."}}