IPerf——網絡測試工具介紹與源碼解析(5)


本篇隨筆講述一下TCP協議下,雙向測試模式和交易測試模式下客戶端和服務端執行的情況;

雙向測試模式:

官方文檔的解釋

Run Iperf in dual testing mode. This will cause the server to connect back to the client on the port specified in the -L option (or defaults to the port the client connected to the server on). This is done immediately therefore running the tests simultaneously. If you want an alternating test try -r.

客戶端連接到服務端進行數據發送的同時,服務端通過客戶端設置的監聽端口(可通過-L選項另行設置)向客戶端發起連接進行數據發送,達成雙向測試的效果。其實換句話來說就是模擬全雙工通信模式。

交易測試模式:

官方文檔的解釋

Run Iperf in tradeoff testing mode. This will cause the server to connect back to the client on the port specified in the -L option (or defaults to the port the client connected to the server on). This is done following the client connection termination, therefore running the tests alternating. If you want an simultaneous test try -d.

 客戶端連接到服務端進行數據發送結束后,服務端隨即通過客戶端設置的監聽端口(可通過-L選項另行設置)向客戶端發起連接進行數據發送,相應的就是模擬半雙工通信模式。

 

兩者的區別在於服務端何時模擬客戶端的功能開始往回連接。這點在IPerf中很容易就實現了。

其實thread_Settings結構中存在兩個指向thread_Setting類型的指針變量,分別命名為runNow和runNext,以往介紹IPerf的隨筆中指出過,thread_Settings包涵了線程運行時所需的全部信息,程序是根據該類型的變量生成各種類型的線程,runNow表示在創建當前的線程之前需要先創建runNow指向的線程,而runNext表示當前線程結束后才創建runNext所指向的線程。定位到具體的代碼如下所示:

            if ( tempSettings != NULL ) 
            {
                client_init( tempSettings );
                if ( tempSettings->mMode == kTest_DualTest ) 
                {
#ifdef HAVE_THREAD
                    server->runNow =  tempSettings;
#else
                    server->runNext = tempSettings;
#endif
                } else // if tradoff mode
                {
                    server->runNext =  tempSettings;
                }
            }

 

雙向測試和交易測試其實就是:

1. 在客戶端添加服務端的功能,表現在開始時添加了一個監聽者線程,接收到服務端連接過來的套接字后添加了一個服務端線程;

2. 在服務端線程添加了客戶端線程。

需要注意的一點就是,客戶端監聽服務端連接過來的套接字時,不是隨便誰的連接都在接收后將其放入客戶端鏈表,它需要判斷對端的地址是否是當前這端客戶端線程所連接的服務端的地址,如果不是則將其丟棄,並重新監聽,具體定位到代碼表示如下:

            if ( client )
            {
                //檢測發起反向連接的對端是不是用戶指定的服務端
                if ( !SockAddr_Hostare_Equal( (sockaddr*) &mSettings->peer, (sockaddr*) &server->peer ) )
                {
                    // Not allowed try again
                    close( server->mSock );
                    if ( isUDP( mSettings ) ) 
                    {
                        mSettings->mSock = -1;
                        Listen();
                    }
                    continue;
                }
            }

 下面兩張圖展示雙向測試模式下客戶端和服務端的執行過程:

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM