Android套接字连接超时 [英] Android socket connection timeout

查看:94
本文介绍了Android套接字连接超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的android应用通过套接字连接到服务器,该套接字在node.js中进行了编码.将前景放置15分钟后,它将失去与服务器的连接.以下是将袜子连接到服务器的代码

My android app is connected to the server through socket, which is coded in node.js. When the is left in the foreground for 15 minutes it losses connection to the server. The following is the code that connects the sockt to the server

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) {
                onError(ex);
            }
        }
    });
    Log.v(AppConstants.DEBUG_TAG, userId + " : Thread about to be started");
    mThread.start();
}

这个问题的解决方案?

推荐答案

搜索了很多之后,我找到了解决该问题的方法.将超时添加到套接字连接.

After googling a lot I found out a solution to this problem. Add timeout to the socket connection.

mSocket.setSoTimeout(10*1000);

如果没有任何响应,则在10秒钟后它将抛出 SocketTimeoutException ,并在此异常的捕获中关闭连接(如果存在),然后再次连接.

If there isn't any response, after 10 seconds it will throw SocketTimeoutException and in the catch of this exception close the connection if exists, then connect again.

catch (SocketTimeoutException e) {
  if (mSocket.isConnected()) {
    disconnect();
  }
  connect();
}

这篇关于Android套接字连接超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆