打印输出tcp拥塞窗口

打印输出tcp拥塞窗口

在内核的函数tcp_getsockopt的代码中,可以看到这个选项TCP_INFO,返回了几乎所有的参数,同时还有其他的许多参数可以得到一些其他的信息。具体每个参数的含义可以参考内核中的注释。

示例

#include <string>
#include <string.h>
#include <cstdlib>
#include <ctime>
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/time.h>
#include <linux/ip.h>

void read_cwnd(int tcp_socket)
{
    struct tcp_info info;
    int length = sizeof(struct tcp_info);
   
    if (getsockopt( tcp_socket, IPPROTO_TCP, TCP_INFO, (void *)&info, (socklen_t *)&length ) == 0 )
    {
       printf("%u %u %u %u %u %u %u %u %u %u %u %u\n",
            info.tcpi_snd_cwnd,
            info.tcpi_snd_ssthresh,
            info.tcpi_rcv_ssthresh,
            info.tcpi_rtt,
            info.tcpi_rttvar,
            info.tcpi_unacked,
            info.tcpi_sacked,
            info.tcpi_lost,
            info.tcpi_retrans,
            info.tcpi_fackets,
            info.tcpi_ca_state,
            info.tcpi_reordering
           );  
   } 
}

int main()
{
    sockaddr_in in;
    memset(&in,'\0',sizeof(in));
    in.sin_family=AF_INET;
    in.sin_port=htons(8888);
    in.sin_addr.s_addr=INADDR_ANY;
   
    int reuse0=1;
    int serv=socket(AF_INET, SOCK_STREAM, 0);
   
    read_cwnd(serv);
    return 1;
}

原文

http://www.cnblogs.com/fll/archive/2009/03/20/1418091.html

posted @ 2013-04-18 02:15  浪里飞  阅读(2869)  评论(0编辑  收藏  举报