简体   繁体   中英

How to find out the bytes sent for a connected tcp socket?

I want to find out how many bytes are transmitted on a tcp socket (I'm using Ubuntu 18.04, by the way). I tried the ss command, but it seems ss can only show the bytes_received, not bytes_sent. So is there any way to show bytes_sent for a socket? And why is this number not shown in ss? I think bytes_received and bytes_sent are closely related.

> ss -ti

ESTAB    0   0   host1:ssh              host2:7703
     cubic wscale:2,8 rto:384 rtt:181.622/16.823 ato:40 mss:1452 cwnd:10 
     ssthresh:118 bytes_acked:120873541 bytes_received:1151501 segs_out:87562 
     segs_in:39194 send 639.6Kbps lastsnd:11732 lastrcv:11564 lastack:11564 
     pacing_rate 1.3Mbps retrans:0/203 reordering:56 rcv_rtt:11516 rcv_space:71360

I've been looking into this same issue, and have reached the conclusion that it depends on the kernel you are using.

From my tests, I have been able to get bytes_sent , bytes_acked , and bytes_recieved on Linux kernels 4.xx and 5.xx, but not on 3.xx. See example below on Debian GNU/Linux with kernel 4.19.0-6-amd64 :

$ ss -itpn

State           Recv-Q           Send-Q                      Local Address:Port                      Peer Address:Port
ESTAB           0                0                          172.16.177.135:22                        172.16.177.1:60928            
users:(("sshd",pid=700,fd=4))
    bytes_sent:50872 bytes_acked:50872 bytes_received:11165

*I cut out the other info that wasn't relevant to this issue

After digging through sock_diag and ss's source code , it looks like ss uses INET_DIAG_INFO of type struct tcp_info to get this info.

I hope this helps and will let you know if I figure anything else out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM