{"id":5998,"date":"2016-11-27T08:12:42","date_gmt":"2016-11-27T13:12:42","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=5998"},"modified":"2017-07-26T07:57:56","modified_gmt":"2017-07-26T12:57:56","slug":"simulating-sockettimeoutexception","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2016\/11\/27\/simulating-sockettimeoutexception\/","title":{"rendered":"Simulating SocketTimeoutException"},"content":{"rendered":"<p>A SocketTimeoutException occurs when a socket object instance has been configured to throw the exception if it receives no data in the prescribed number of milliseconds.  The default is 0, which disables the timeout.  Reasons you may want to use a timeout value other than 0 include ensuring a given thread is not tied up waiting for a slow remote service to respond.<\/p>\n<p>To be clear, this is not the same issue as a Connection Reset, or a ConnectionTimeoutException.  In this case, we successfully connected, but the remote service didn&#8217;t respond to us in a timely fashion (or at least as timely as we expected).<\/p>\n<pre>\r\nimport java.net.*;\r\nimport java.io.*;\r\nimport java.util.*;\r\n\r\nclass serverSocket {\r\n  public static void main (String args[]) throws Exception {\r\n    ServerSocket ss;\r\n    DataInputStream is;\r\n    PrintStream os;\r\n    ss = new ServerSocket(5000);\r\n    while (true) {\r\n      Socket clientSocket = ss.accept();\r\n      System.out.println(\"got socket\");\r\n      is = new DataInputStream(clientSocket.getInputStream());\r\n      os = new PrintStream(clientSocket.getOutputStream());\r\n      String line = is.readLine();\r\n      Thread.sleep(Integer.parseInt(args[0]));\r\n      os.println(\"sending back \" + line.toUpperCase());\r\n      os.close();\r\n      is.close();\r\n    }\r\n  }\r\n}\r\n\r\nclass clientSocket {\r\n  public static void main (String args[]) throws Exception {\r\n    Socket s = new Socket(\"localhost\", 5000);\r\n    System.out.println(s.getSoTimeout());\r\n    s.setSoTimeout(Integer.parseInt(args[1]));\r\n    System.out.println(s.getSoTimeout());\r\n    DataInputStream is = new DataInputStream(s.getInputStream());\r\n    PrintStream os = new PrintStream(s.getOutputStream());\r\n    try {\r\n      System.out.println(new Date());\r\n      \/\/Thread.sleep(10000);\r\n      os.println(args[0]);\r\n      String responseLine;\r\n\t  while ((responseLine = is.readLine()) != null) {\r\n        System.out.println(\"Server: \" + responseLine);\r\n        os.close();\r\n        is.close();\r\n      }\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n    finally {\r\n\t  os.close();\r\n\t  s.close();\r\n\t}\r\n    System.out.println(new Date());\r\n  }\r\n}\r\n<\/pre>\n<p>You can simulate this with what is below&#8230;<\/p>\n<pre>\r\nc:\\Users\\showard>java serverSocket 2000\r\ngot socket\r\n<\/pre>\n<p>&#8230;and for the client&#8230;<\/p>\n<pre>\r\nc:\\Users\\showard>java clientSocket steve 1000\r\n0\r\n1000\r\nSun Nov 27 08:04:22 EST 2016\r\njava.net.SocketTimeoutException: Read timed out\r\n        at java.net.SocketInputStream.socketRead0(Native Method)\r\n        at java.net.SocketInputStream.socketRead(Unknown Source)\r\n        at java.net.SocketInputStream.read(Unknown Source)\r\n        at java.net.SocketInputStream.read(Unknown Source)\r\n        at java.net.SocketInputStream.read(Unknown Source)\r\n        at java.io.DataInputStream.readLine(Unknown Source)\r\n        at clientSocket.main(serverSocket.java:38)\r\nSun Nov 27 08:04:23 EST 2016\r\n\r\nc:\\Users\\showard>\r\n<\/pre>\n<p>Try it with varying values for the sleep in each class to simulate different behaviour.<\/p>\n<p>The server handles the underlying exception as shown below.  Specifically, notice that the EPIPE exception is thrown&#8230;<\/p>\n<pre>\r\n29659 08:51:17.673124 accept(7, {sa_family=AF_INET6, sin6_port=htons(57906), inet_pton(AF_INET6, \"::ffff:127.0.0.1\", &sin6_addr), sin6_flowinfo=0, sin6_scope_id=0}, [28]) = 8\r\n29659 08:51:17.673179 fcntl(8, F_GETFL) = 0x2 (flags O_RDWR)\r\n29659 08:51:17.673211 fcntl(8, F_SETFL, O_RDWR) = 0\r\n29659 08:51:17.673523 write(1, \"got socket\", 10) = 10\r\n29659 08:51:17.673600 write(1, \"\\n\", 1) = 1\r\n29659 08:51:17.675158 recvfrom(8,  <unfinished ...>\r\n29659 08:51:17.686821 <... recvfrom resumed> \"s\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:17.686876 recvfrom(8, \"t\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:17.686910 recvfrom(8, \"e\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:17.686987 recvfrom(8, \"v\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:17.687050 recvfrom(8, \"e\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:17.687100 recvfrom(8, \"\\n\", 1, 0, NULL, NULL) = 1\r\n29659 08:51:19.687698 sendto(8, \"sending back STEVE\", 18, 0, NULL, 0) = 18\r\n29659 08:51:19.687813 sendto(8, \"\\n\", 1, 0, NULL, 0) = -1 EPIPE (Broken pipe)\r\n29659 08:51:19.687875 --- SIGPIPE {si_signo=SIGPIPE, si_code=SI_USER, si_pid=29658, si_uid=8084694} ---\r\n29659 08:51:19.687915 rt_sigreturn()    = -1 EPIPE (Broken pipe)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A SocketTimeoutException occurs when a socket object instance has been configured to throw the exception if it receives no data in the prescribed number of milliseconds. The default is 0, which disables the timeout. Reasons you may want to use&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2016\/11\/27\/simulating-sockettimeoutexception\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[24,25,56],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5998"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=5998"}],"version-history":[{"count":7,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5998\/revisions"}],"predecessor-version":[{"id":6424,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/5998\/revisions\/6424"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=5998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=5998"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=5998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}