java网络编程Socket中SO_LINGER选项的用法解读
生活随笔
收集整理的這篇文章主要介紹了
java网络编程Socket中SO_LINGER选项的用法解读
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://blog.sina.com.cn/s/blog_6b1990eb0101171o.html
1:設置該選項: public void setSoLinger(boolean on, int seconds) throws SocketException;
?????讀取該選項:public int getSoLinger() throws SocketException
?????SO_LINGER選項用來控制Socket關閉時的行為,默認情況下,執行Socket的close方法,該方法會立即返回,但底層的Socket實際上并不會立即關閉,他會立即延遲一段時間,知道發送完剩余的數據,才會真正的關閉Socket,斷開連接。
????setSoLinger(true, 0): 執行該方法,那么執行Socket的close方法,該方法也會立即返回,但底層的Socket也會立即關閉,所有未發送完的剩余數據被丟棄
????setSoLinger(true, 3600): 那么執行Socket的close方法,該方法不會立即返回,而進入阻塞狀態,同時,底層的Socket也會嘗試發送剩余的數據,只有滿足下面的兩個條件之一,close方法才會返回:
????(1):底層的Socket已經發送完所有的剩余數據
????(2): 盡管底層的Socket還沒有發送完所有的剩余數據,但已經阻塞了3600秒,close()方法的阻塞時間超過3600秒,也會返回,剩余未發送的數據被丟棄。
?
2:類SimpleClient
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException;
public class SimpleClient {
?public static void main(String[] args) throws UnknownHostException, IOException { ??Socket socket = new Socket("localhost", 8000); ??//socket.setSoLinger(true, 0); ???????????//Socket關閉后,底層Socket立即關閉 ??//socket.setSoLinger(true, 3600); ????????//Socket關閉后,底層Socket延遲3600秒再關閉 ??OutputStream os = socket.getOutputStream(); ??StringBuffer sb = new StringBuffer(); ??for(int i = 0; i < 10000; i++) { ???sb.append(i); ??} ??os.write(sb.toString().getBytes()); ????????//發送一萬個字符 ??System.out.println("開始關閉Socket"); ??long begin = System.currentTimeMillis(); ??socket.close(); ??long end = System.currentTimeMillis(); ??System.out.println("關閉socket所使用的時間為:" + (end - begin) + "ms"); ?}
}
?
3:類SimpleServer
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class SimpleServer {
?public static void main(String[] args) throws IOException, InterruptedException { ????ServerSocket serverSocket = new ServerSocket(8000); ????Socket socket = serverSocket.accept(); ???? ????Thread.sleep(5000); ??????????????????//睡眠5秒鐘后再讀輸入流 ???? ????InputStream inputStream = socket.getInputStream(); ????ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ????byte[] buff = new byte[1024]; ????int len = -1; ????do { ?????len = inputStream.read(buff); ?????System.out.println(len); ?????if(len != -1) { ??????buffer.write(buff, 0, len); ?????} ????} while(len != -1); ????System.out.println(new String(buffer.toByteArray())); ?????//把字節數組轉換為字符串 ?} }
?
4:測試方法
???(1)先啟動SimpleServer進程,再啟動SimpleClient進程
???(2)把 //socket.setSoLinger(true, 0); ???????????//Socket關閉后,底層Socket立即關閉 ???????????的注釋去掉,再次先后啟動SimpleServer和SimpleClient 進程,這樣當Socket關閉時,會強行關閉底層的Socket,
???????????所有未發送完的數據丟失
???(3)把//socket.setSoLinger(true, 3600); ????????//Socket關閉后,底層Socket延遲3600秒再關閉 ???????????的注釋去掉,再次先后啟動SimpleServer和SimpleClient進程,這樣當SimpleClient執行Socket的close方法時,
???????????會進入阻塞狀態,直到等待了3600秒,或者底層Socket已經把所有未發送的剩余數據發送完畢,才會從close返回
1:設置該選項: public void setSoLinger(boolean on, int seconds) throws SocketException;
?????讀取該選項:public int getSoLinger() throws SocketException
?????SO_LINGER選項用來控制Socket關閉時的行為,默認情況下,執行Socket的close方法,該方法會立即返回,但底層的Socket實際上并不會立即關閉,他會立即延遲一段時間,知道發送完剩余的數據,才會真正的關閉Socket,斷開連接。
????setSoLinger(true, 0): 執行該方法,那么執行Socket的close方法,該方法也會立即返回,但底層的Socket也會立即關閉,所有未發送完的剩余數據被丟棄
????setSoLinger(true, 3600): 那么執行Socket的close方法,該方法不會立即返回,而進入阻塞狀態,同時,底層的Socket也會嘗試發送剩余的數據,只有滿足下面的兩個條件之一,close方法才會返回:
????(1):底層的Socket已經發送完所有的剩余數據
????(2): 盡管底層的Socket還沒有發送完所有的剩余數據,但已經阻塞了3600秒,close()方法的阻塞時間超過3600秒,也會返回,剩余未發送的數據被丟棄。
?
2:類SimpleClient
import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.UnknownHostException;
public class SimpleClient {
?public static void main(String[] args) throws UnknownHostException, IOException { ??Socket socket = new Socket("localhost", 8000); ??//socket.setSoLinger(true, 0); ???????????//Socket關閉后,底層Socket立即關閉 ??//socket.setSoLinger(true, 3600); ????????//Socket關閉后,底層Socket延遲3600秒再關閉 ??OutputStream os = socket.getOutputStream(); ??StringBuffer sb = new StringBuffer(); ??for(int i = 0; i < 10000; i++) { ???sb.append(i); ??} ??os.write(sb.toString().getBytes()); ????????//發送一萬個字符 ??System.out.println("開始關閉Socket"); ??long begin = System.currentTimeMillis(); ??socket.close(); ??long end = System.currentTimeMillis(); ??System.out.println("關閉socket所使用的時間為:" + (end - begin) + "ms"); ?}
}
?
3:類SimpleServer
import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; import java.net.Socket;
public class SimpleServer {
?public static void main(String[] args) throws IOException, InterruptedException { ????ServerSocket serverSocket = new ServerSocket(8000); ????Socket socket = serverSocket.accept(); ???? ????Thread.sleep(5000); ??????????????????//睡眠5秒鐘后再讀輸入流 ???? ????InputStream inputStream = socket.getInputStream(); ????ByteArrayOutputStream buffer = new ByteArrayOutputStream(); ????byte[] buff = new byte[1024]; ????int len = -1; ????do { ?????len = inputStream.read(buff); ?????System.out.println(len); ?????if(len != -1) { ??????buffer.write(buff, 0, len); ?????} ????} while(len != -1); ????System.out.println(new String(buffer.toByteArray())); ?????//把字節數組轉換為字符串 ?} }
?
4:測試方法
???(1)先啟動SimpleServer進程,再啟動SimpleClient進程
???(2)把 //socket.setSoLinger(true, 0); ???????????//Socket關閉后,底層Socket立即關閉 ???????????的注釋去掉,再次先后啟動SimpleServer和SimpleClient 進程,這樣當Socket關閉時,會強行關閉底層的Socket,
???????????所有未發送完的數據丟失
???(3)把//socket.setSoLinger(true, 3600); ????????//Socket關閉后,底層Socket延遲3600秒再關閉 ???????????的注釋去掉,再次先后啟動SimpleServer和SimpleClient進程,這樣當SimpleClient執行Socket的close方法時,
???????????會進入阻塞狀態,直到等待了3600秒,或者底層Socket已經把所有未發送的剩余數據發送完畢,才會從close返回
總結
以上是生活随笔為你收集整理的java网络编程Socket中SO_LINGER选项的用法解读的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ByteArrayOutputStrea
- 下一篇: 查看网络负载