.read() throws intermittent SocketTimeoutException even though data should be there

I'm working on a web server for Android and even though I've spent days trying to fix it, I'm at my wits' end with this particular bug. I'm trying to read the request from the browser, and the code works fine most of the time, but something like 5% of the requests fail and it throws random SocketTimeoutExceptions without even reading a single character from the Socket. I have tested this with different browsers and it happens with all of them, so chances are the problem is on my end. Here's the relevant code, stripped down as far as possible: public class ServerThread extends Thread { private ServerSocket ss = null; private boolean isRunning; private ExecutorService threadPool = new ThreadPoolExecutor(2, 12, 60L, TimeUnit.SECONDS, new SynchronousQueue(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); public ServerThread() { } public synchronized void run() { ss = new ServerSocket(8080, 1); isRunning = true; while (isRunning) { Socket clientSocket = null; try { if (ss != null) { clientSocket = ss.accept(); if (isRunning) { this.threadPool.execute(new HTTPSession(clientSocket)); } } } catch (IOException e) { e.printStackTrace(); } } } } And: public class HTTPSession implements Runnable { private Socket mSocket = null; public HTTPSession (Socket s) { mSocket = s; } public void run() { InputStream ips = null; try { mSocket.setSoTimeout(15000); ips = mSocket.getInputStream(); ips.read(); } catch (Exception e) { e.printStackTrace(); Log.v("HTTPSession", "Socket connected: " + mSocket.isConnected() + ", Socket closed: " + mSocket.isClosed() + ", InputShutdown: " + mSocket.isInputShutdown()); } finally { try { ips.close(); } catch (IOException ioe) { } try { mSocket.close(); } catch (IOException ioe) { } } } } So ServerThread accepts the connection, HTTPSession tries to read from the Socket and sometimes it throws the SocketTimeoutException after the 15 seconds are up. The output from the Log statement in the catch in this case is: Socket connected: true, Socket closed: false, InputShutDown: false What gives? Surely 15 seconds is enough of a wait and it seems unlikely that mainstream web browsers just aren't sending any data, so why can't I read it? I would appreciate any input on this problem.

以上就是.read() throws intermittent SocketTimeoutException even though data should be there的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏