用实际案例来理解netstat -nao中的Recv-Q和Send-Q

972 篇文章 329 订阅
148 篇文章 34 订阅

       我们先来看看:

 

xxxxxx$ netstat -ano | head             
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       Timer
tcp        0      0 127.0.0.1:42222         0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:48369     0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:13942     0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:10586     0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:63227     0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 0.0.0.0:8765            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:20126     0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp        0      0 10.100.70.140:23456     0.0.0.0:*               LISTEN      off (0.00/0/0)

       第二列表内核recv缓冲区中的字节数(接收缓冲区), 第三列表示内核send缓冲区中的字节数(发送缓冲区)。 所以, 对于一个tcp连接的两端而言, 有四个内核缓冲区。

 

 

       来看程序, 服务端:

 

#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>

int main()
{
	int sockSrv = socket(AF_INET, SOCK_STREAM, 0);

	struct sockaddr_in addrSrv;
	addrSrv.sin_family = AF_INET;
	addrSrv.sin_addr.s_addr = INADDR_ANY; 
	addrSrv.sin_port = htons(8765);

	bind(sockSrv, (const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));

	listen(sockSrv, 5);

	struct sockaddr_in addrClient;
	int len = sizeof(struct sockaddr_in);

	int sockConn = accept(sockSrv, (struct sockaddr *)&addrClient, (socklen_t*)&len);

    while(1)    
    {    
        getchar();    
        char szRecvBuf[1001] = {0};    
        int iRet = recv(sockConn, szRecvBuf, sizeof(szRecvBuf) - 1, 0);    
        printf("iRet is %d\n", iRet);     
    }

	getchar();
	close(sockConn);
	close(sockSrv);
	
	return 0;
}

       客户端:

 

 

#include <unistd.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <malloc.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <stdarg.h>
#include <fcntl.h>


int main()
{
    int sockClient = socket(AF_INET, SOCK_STREAM, 0);

    struct sockaddr_in addrSrv;
    addrSrv.sin_addr.s_addr = inet_addr("10.100.70.140");
    addrSrv.sin_family = AF_INET;
    addrSrv.sin_port = htons(8765);
    connect(sockClient, ( const struct sockaddr *)&addrSrv, sizeof(struct sockaddr_in));

	#define N 2000
	char szSendBuf[N] = {0};
	for(unsigned int i = 0; i < N; i++) //×Ö·ûÊý×é×îºóÒ»¸ö×Ö·û²»ÒªÇóÊÇ¡®\0¡¯
	{
		szSendBuf[i] = 'a';	
	}

	int total = 0;
	while(1)
	{
		int iRet = send(sockClient, szSendBuf, sizeof(szSendBuf) , 0); 
		total += iRet;
		printf("iRet is %d, total send is %d\n", iRet, total);
		getchar();
	}

    close(sockClient);

    return 0;
}

       我们先开启服务端, 再看起客户端, 此时客户端给服务端发送了2000字节, 但服务端没有去取出这2000字节, 我们来看看服务端的情况:

 

 

xxxxxx$ netstat -ano | grep 8765
tcp        0      0 0.0.0.0:8765            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp     2000      0 10.100.70.140:8765      10.100.70.139:43634     ESTABLISHED off (0.00/0/0)

       再看看客户端, 客户端都发送完了, 没有字节积压, 所以发送缓冲区中么有字节, 如下:

 

xxxxxx$ netstat -ano | grep 8765
tcp        0      0 10.100.70.139:43634     10.100.70.140:8765      ESTABLISHED off (0.00/0/0)

       
        此时, 如果我们在服务端用recv函数取出1000字节, 会怎样呢?  显然, 内核缓冲区中还剩100字节, 如下:

 

xxxxxx$ netstat -ano | grep 8765
tcp        0      0 0.0.0.0:8765            0.0.0.0:*               LISTEN      off (0.00/0/0)
tcp     1000      0 10.100.70.140:8765      10.100.70.139:43634     ESTABLISHED off (0.00/0/0)

 


        如何才能在客户端上看到内核缓冲区中的数据呢? 很简单, 让客户端一只发发发, 服务端的内核缓冲区数据塞满后, 自然开始在客户端的内核缓冲区积压了。 有兴趣的朋友可以试试, 这对理解tcp很有帮助。

 

 

       

 


 

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值