[20200310]測試網路狀態TIME_WAIT(windows).txt

lfree發表於2020-03-10

[20200310]測試網路狀態TIME_WAIT(windows).txt

--//簡單探究windows下出現TIME_WAIT時,需要多少時間消失。我前面的測試:http://blog.itpub.net/267265/viewspace-2678684/
--//實際上這個時間是寫死在net/tcp.h標頭檔案裡面的,參考:/usr/src/kernels/2.6.39-300.26.1.el5uek/include/net/tcp.h可以發現如下:

#define TCP_TIMEWAIT_LEN (60*HZ) /* how long to wait to destroy TIME-WAIT
                  * state, about 60 seconds */
#define TCP_FIN_TIMEOUT TCP_TIMEWAIT_LEN
                                 /* BSD style FIN_WAIT2 deadlock breaker.
                  * It used to be 3min, new value is 60sec,
                  * to combine FIN-WAIT-2 timeout with
                  * TIME-WAIT timer.
                  */
--//除非你修改標頭檔案並且重新編譯核心,否則都是無效的。

1.測試環境:
--//測試windows下的情況,我的測試環境是windows 7:
--//client: 192.168.98.6
--//server: 192.168.100.78 linux

--// 批處理 ts.bat指令碼如下,注意安裝gawk,可以安裝UnxUtils包。
@echo off
gawk "{ print strftime(\"[%%Y-%%m-%%d %%H:%%M:%%S]\"), $0 }"

2.測試:
SCOTT@78> @ spid
       SID    SERIAL# PROCESS                  SERVER    SPID                     PID  P_SERIAL# C50
---------- ---------- ------------------------ --------- -------------------- ------- ---------- --------------------------------------------------
        30        671 5592:5488                DEDICATED 42739                     26        252 alter system kill session '30,671' immediate;
 
--//記下spid=42739:

$ netstat -nopt  2>/dev/null | grep 42739
tcp        0      0 192.168.100.78:1521         192.168.98.6:52233          ESTABLISHED 42739/oraclebook    off (0.00/0/0)

--//這樣就知道client使用的埠號42739。實際上不需要這麼麻煩,client僅僅1個連線。
--//在客戶端執行:
R:\>seq 1000 | xargs -iQ cmd /c "netstat -na | grep 192.168.100.78:1521  | ts && sleep 1"
[2020-03-10 10:12:37]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:12:38]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:12:40]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:12:41]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:12:42]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:12:43]   TCP    192.168.98.6:52233     192.168.100.78:1521    ESTABLISHED
--//在sqlplus下執行quit退出,出現state=TIME_WAIT.
[2020-03-10 10:12:45]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:12:46]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:12:47]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
...

[2020-03-10 10:14:37]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:14:38]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:14:39]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:14:41]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:14:42]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:14:43]   TCP    192.168.98.6:52233     192.168.100.78:1521    TIME_WAIT
--//可以看出2分鐘消失,也就是120秒。
--//另外注意:這個情況出現state=TIME_WAIT僅僅發生在客戶端。

3.修改註冊看看:
--//網路上查了一些資料,提示在登錄檔(regedit)的HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters上添
--//加一個DWORD型別的值TcpTimedWaitDelay,值就是秒數,即可。重啟機器才能測試:
--//timewait.reg
REGEDIT4

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Tcpip\Parameters]
"TcpTimedWaitDelay"=dword:0000001e

R:\>seq 1000 | xargs -iQ cmd /c "netstat -na | grep 192.168.100.78:1521  | ts && sleep 1"
[2020-03-10 10:37:28]   TCP    192.168.98.6:49387     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:37:29]   TCP    192.168.98.6:49387     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:37:31]   TCP    192.168.98.6:49387     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:37:32]   TCP    192.168.98.6:49387     192.168.100.78:1521    ESTABLISHED
[2020-03-10 10:37:33]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:34]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:35]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:37]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:38]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:39]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:40]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:42]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:43]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:44]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:45]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:46]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:48]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:49]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:50]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:51]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:52]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:54]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:55]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:56]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:57]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:58]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:37:59]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
[2020-03-10 10:38:01]   TCP    192.168.98.6:49387     192.168.100.78:1521    TIME_WAIT
--//30秒消失.補充測試我發現最小設定30秒,我設定10秒無效。
--//查詢一些連結提示:在Parameters子鍵下建立或修改名為TcpTimedWaitDelay的REG_DWORD值,該值的範圍是從0到300,建議將該值設
--//置為30。

4.總結:
--//在測試時我遇到很奇怪的情況,開機第一次連線後退出不會出現state=TIME_WAIT的情況。
--//另外我做了其它測試,發現一些奇怪的情況。有時間另外寫一篇。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2679356/,如需轉載,請註明出處,否則將追究法律責任。

相關文章