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;
                }
            }

 下面两张图展示双向测试模式下客户端和服务端的执行过程:

原文地址:https://www.cnblogs.com/liaoguifa/p/5437406.html