设为首页 加入收藏

TOP

c/c++ 网络编程 陈硕老师视频理解之ttcp
2019-05-23 15:04:06 】 浏览:65
Tags:c/c 网络编程 陈硕 老师 视频 理解 ttcp

ttcp 是干啥的:测试2台机器间的网络传输性能

  • wiki
  • 功能如下图:

对应的视频是:

  • 4.回顾基础的Sockets API.mkv

  • 5.TTCP代码概览.mkv
  • 6.使用TTCP进行网络传输性能测试.mkv

代码:

github

准备事项:

? 安装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));

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇NOIP专题复习1 图论-最短路 下一篇Tarjan算法初探 (1):Tarjan如何求..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目