
我的android应用程序通过socket连接到服务器,socket以node.Js编码.当它在前台停留15分钟时,它会丢失与服务器的连接.以下是将sockt连接到服务器的代码
public voID connect() { this.connectionStatus = CONNECT_STATUS_CONNECTING; Log.v(AppConstants.DEBUG_TAG, userID + " : Connecting to Server"); if (mThread != null && mThread.isAlive()) { return; } mThread = new Thread(new Runnable() { @OverrIDe public voID run() { try { Log.v(AppConstants.DEBUG_TAG, userID + " : Thread Action Started"); String secret = createSecret(); int port = (mURI.getPort() != -1) ? mURI.getPort() : (mURI.getScheme().equals("wss") ? 443 : 80); String path = TextUtils.isEmpty(mURI.getPath()) ? "/" : mURI.getPath(); if (!TextUtils.isEmpty(mURI.getquery())) { path += "?" + mURI.getquery(); } String originScheme = mURI.getScheme().equals("wss") ? "https" : "http"; URI origin = new URI(originScheme, "//" + mURI.getHost(), null); SocketFactory factory = mURI.getScheme().equals("wss") ? getSSLSocketFactory() : SocketFactory.getDefault(); mSocket = factory.createSocket(mURI.getHost(), port); mSocket.setKeepAlive(true); PrintWriter out = new PrintWriter(mSocket.getoutputStream()); out.print("GET " + path + " http/1.1\r\n"); out.print("Upgrade: websocket\r\n"); out.print("Connection: Upgrade\r\n"); out.print("Host: " + mURI.getHost() + "\r\n"); out.print("Origin: " + origin.toString() + "\r\n"); out.print("Sec-WebSocket-Key: " + secret + "\r\n"); out.print("Sec-WebSocket-Version: 13\r\n"); if (mExtraheaders != null) { for (nameValuePair pair : mExtraheaders) { out.print(String.format("%s: %s\r\n", pair.getname(), pair.getValue())); } } out.print("\r\n"); out.flush(); HybiParser.HappyDatainputStream stream = new HybiParser.HappyDatainputStream(mSocket.getinputStream()); // Read http response status line. Statusline statusline = parseStatusline(readline(stream)); if (statusline == null) { Log.v(AppConstants.DEBUG_TAG, "Received no reply from server."); throw new httpException("Received no reply from server."); } else if (statusline.getStatusCode() != httpStatus.SC_SWITCHING_PROTOColS) { throw new httpResponseException(statusline.getStatusCode(), statusline.getReasonPhrase()); } // Read http response headers. String line; boolean valIDated = false; while (!TextUtils.isEmpty(line = readline(stream))) { header header = parseheader(line); if (header.getname().equals("Sec-WebSocket-Accept")) { String expected = createSecretValIDation(secret); String actual = header.getValue().trim(); if (!expected.equals(actual)) { Log.v(AppConstants.DEBUG_TAG, "Bad Sec-WebSocket-Accept header value."); throw new httpException("Bad Sec-WebSocket-Accept header value."); } valIDated = true; } } if (!valIDated) { Log.v(AppConstants.DEBUG_TAG, "No Sec-WebSocket-Accept header."); throw new httpException("No Sec-WebSocket-Accept header."); } onConnect(); Log.v(AppConstants.DEBUG_TAG, userID + " : Thread should be connected by Now"); // Now decode websocket frames. mParser.start(stream); } catch (EOFException ex) { Log.d(AppConstants.DEBUG_TAG, "WebSocket EOF!", ex); ondisconnect(0, "EOF"); } catch (SSLException ex) { // Connection reset by peer Log.d(AppConstants.DEBUG_TAG, "Websocket SSL error!", ex); ondisconnect(0, "SSL"); } catch (Exception ex) { one rror(ex); } } }); Log.v(AppConstants.DEBUG_TAG, userID + " : Thread about to be started"); mThread.start();}anu解决这个问题?
解决方法:
经过谷歌搜索后,我找到了解决这个问题的方法.为套接字连接添加超时.
mSocket.setSoTimeout(10000);如果没有任何响应,10秒钟它将抛出SocketTimeoutException并在此异常的catch中关闭连接(如果存在),然后再次连接.
catch (SocketTimeoutException e){ if(mSocket.isConnected()){ disconnect(); } connect(); } 总结 以上是内存溢出为你收集整理的Android套接字连接超时全部内容,希望文章能够帮你解决Android套接字连接超时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)