socket通訊控制代碼泄漏與read阻塞

來源:互聯網
上載者:User

用戶端程式
public class SocketClient {
 public static void main(String[] args) throws Exception {
  while(true){
   Thread.sleep(5000);
   new ClientThread().start();
  }
 }
}

class ClientThread extends Thread {
 public void run() {
  Socket socket = null;
  InputStream in = null;
  OutputStream out = null;
  try {
   socket = new Socket("10.1.24.147", 8888);
   in = socket.getInputStream();
   out = socket.getOutputStream();
   System.out.println("Established a connection...");
   String text = "B7A30350BE4F9F58482576B600239D54?Open";
   byte[] block = new byte[500];
   System.out.println("begin to write");
   out.write(text.getBytes());
   out.flush();
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   System.out.println("begin to read");
   int num = in.read(block);  //A
   while (num > 0) {
    System.out.println("in while");
    bOut.write(block, 0, num);
    num = in.read(block);
   }
   System.out.println("read finished");
   System.out.println(bOut.toString());
  } catch (Exception e) {
   e.printStackTrace();
  }finally {
   try {
    //out.close();   //B
   } catch (Exception e) {
   }
   try {
    //in.close();    //B
   } catch (Exception e) {
   }
   try {
    //socket.close(); //B
   } catch (Exception e) {
   }
  }
 }
}

服務端程式
public class SocketServer {
 public SocketServer() {
  try {
   ServerSocket server = null;
   try {
    server = new ServerSocket(8888);
    System.out.println("Server starts...port = "+server.getLocalPort());
   } catch (Exception e) {
    System.out.println("Can not listen to. " + e);
   }
   while (true) {
    new ServerThread(server.accept()).start();
    System.out.println("server.accept() starts...");
   }
  } catch (Exception e) {
   System.out.println("Error. " + e);
  }
 }

 public static void main(String[] args) {
  new SocketServer();
 }
}

class ServerThread extends Thread {
 Socket socket = null;

 public ServerThread(Socket socket) {
  this.socket = socket;
 }

 public void run() {
  InputStream in = null;
  OutputStream out = null;
  try {
   in = socket.getInputStream();
   out = socket.getOutputStream();
   byte[] block = new byte[500];
   ByteArrayOutputStream bOut = new ByteArrayOutputStream();
   System.out.println("begin to read");
   int num = in.read(block);
   while (num > 0) {
    System.out.println("in while");
    bOut.write(block, 0, num);
    System.out.println("in while,begin to read");
    num = in.read(block);    //H
   }
   System.out.println("read finished");
   System.out.println(bOut.toString());
   System.out.println("begin to write");   
   out.write(bOut.toString().getBytes());
   out.flush();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   try {
    //out.close();    //I
   } catch (Exception e) {
   }
   try {
    //in.close();     //I
   } catch (Exception e) {
   }
   try {
    //socket.close(); //I
   } catch (Exception e) {
   }
  }
 }
}
觀察到的現象
服務端讀取不到用戶端發送的資料;用戶端也讀取不到服務端應答的資料。
用戶端堵在A處,服務端堵在H處。
服務端阻塞在H處,服務端已經read了一次,並取到資料,準備read第二次的時候,由於流中已經沒有資料,而read總是嘗試讀取更多的資料,所以堵在H處。
用戶端堵在A處,是因為服務端沒有寫資料,因此流中沒有資料可讀。

也就是說,當socket流中沒有資料的時候,read會阻塞,並等待逾時,如果沒有設定逾時,就永遠等待(這和讀取檔案不同)。

Client-->Server
如果server連不上,Client只會佔用一個控制代碼,不管Client發起了多少個串連。
java    12827  oms    6u  sock    0,0          168883381 can't identify protocol

如果server連上了,Client每次串連只要不關閉就會一直佔用一個控制代碼,lsof結果如下:
java    11544  oms  113u  IPv4 168861112                TCP TEST3050:35686->10.1.24.147:8888 (ESTABLISHED)
java    11544  oms  114u  IPv4 168862353                TCP TEST3050:35687->10.1.24.147:8888 (ESTABLISHED)

如果此時Server端進程殺掉,Client端lsof結果如下:
java    11544  oms  180u  sock       0,0          168845122 can't identify protocol
java    11544  oms  181u  sock       0,0          168845124 can't identify protocol
由於client沒有關閉socket,導致串連泄漏。

如果去掉B處的注釋,即使不去掉I處的注釋,用戶端的控制代碼也都釋放了。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.