ttcp 是干啥的:測試2台機器間的網絡傳輸性能
- wiki
- 功能如下圖:
對應的視頻是:
-
4.回顧基礎的Sockets API.mkv
-
5.TTCP代碼概覽.mkv
-
6.使用TTCP進行網絡傳輸性能測試.mkv
代碼:
准備事項:
安裝boost庫,安裝方法
編譯方法:
cd recipes-master/tpc
./build.sh
執行時的參數說明:
Allowed options:
-h [ --help ] Help
-p [ --port ] arg (=5001) TCP port
-l [ --length ] arg (=65536) Buffer length
-n [ --number ] arg (=8192) Number of buffers
-t [ --trans ] arg Transmit
-r [ --recv ] Receive
-D [ --nodelay ] set TCP_NODELAY
接收端的運行方法:
./ttcp -r
發送端的運行方法:
while true; do ./ttcp -t 發送端機器的IP地址或者名字; done
學到的知識點:
1,recv的第四參數MSG_WAITALL的作用
int TcpStream::receiveAll(void* buf, int len)
{
// FIXME: EINTR
return ::recv(sock_.fd(), buf, len, MSG_WAITALL);
}
一端發送(send)了長度為100的數據,接收端如果沒有使用recv的MSG_WAITALL,那么調用一次recv后,就把數據都接收完了,再次在同樣的fd上接收數據會發送什么???
如果接收端使用了recv的MSG_WAITALL,那么就可以多次調用recv,直到接受完所有的數據。
比如第一次想接收前面20個字節的數據:
::recv(sock_.fd(), buf, 20, MSG_WAITALL);
然后第二次把剩余的100-20=80個字節的數據接受完:
::recv(sock_.fd(), buf, 80, MSG_WAITALL);
ttcp.cc文件的140行,一次send了4 +65536個字節的數據,
然后ttcp.cc文件的185行,先接收了4個字節;
然后ttcp.cc文件的192行,又接收了后面的65536個字節;
MSG_WAITALL (since Linux 2.2)
This flag requests that the operation block until the full
request is satisfied. However, the call may still return less
data than requested if a signal is caught, an error or discon‐
nect occurs, or the next data to be received is of a different
type than that returned. This flag has no effect for datagram
sockets.
2,如果結構體的最后一個元素是個長度為0的數組,這個結構體所占用的內存空間是由運行時期決定。
struct PayloadMessage
{
int32_t length;
char data[0];
};
//變量total_len是,別的機器發過來的長度,所以payload指向的內存空間的大小是運行時期才能夠決定的。
PayloadMessage* payload = static_cast<PayloadMessage*>(::malloc(total_len));