java点滴之ServerSocket的使用(二)

2014-11-23 21:39:31 · 作者: · 浏览: 20
fferSize(HTTP.DEFAULT_CHUNK_SIZE); // sock.setSoLinger(true, HTTP.DEFAULT_TIMEOUT); DLog.d(mTAG, "open", "accept :" + String.valueOf(sock.getSendBufferSize())); return sock; } catch (SocketTimeoutException e1) { return null; } catch (Exception e) { try { if (sock != null) sock.close(); } catch (IOException e1) { e = e1; } DLog.w(mTAG, "open", "accept Exception :" + e.toString(), e); return null; } }5.服务线程的停止
 public synchronized boolean stop() {
        close();

        bHttpServerThreadFlag = false;

        DLog.i(mTAG, "stop", "Wait start for finishing thread !!!");
        if (mHttpServerThread != null && mHttpServerThread.isAlive()) {
            try {
                mHttpServerThread.join();
            } catch (InterruptedException e) {
                DLog.w(mTAG, "stop", "Thread Join InterruptedException", e);
            }
        }
        DLog.i(mTAG, "stop", "Wait done for finishing thread !!!");

        mHttpServerThread = null;

        // CTT 2.0
        // Accepted socket SHOULD be closed

        synchronized (mAcceptSocketList) {
            for (WeakReference
  
    sockRef : mAcceptSocketList) {
                try {
                    Socket sock = sockRef.get();
                    if (sock != null) {
                        sock.close();
                    }
                } catch (IOException e) {
                    DLog.w(mTAG, "stop", "Error: socket - IOException", e);
                }
            }

            mAcceptSocketList.clear();
        }

        return true;
    }
  
6.服务线程的关闭
public boolean close() {
        if (mServerSock == null)
            return true;
        try {
            mServerSock.close();
        } catch (Exception e) {
            DLog.w(mTAG, "open", "Close  - Exception :", e);
            return false;
        } finally {
            mServerSock = null;
            mBindPort = 0;
        }
        return true;
    }