简体   繁体   中英

How to get the tcp window size of a socket in linux?

Is there a way in linux c api to get the incoming window size of your tcp socket?

To be clear, by incoming I mean from the perspective of my linux server. It seems that the SND and RCV values in linux in my accepted socket are flipped, ie RCV means from the server to the client. In this case I need the client to server side, which is SND.

SND_BUF and RCV_BUF seem unrelated to the actual value that is sent, and TCP_WINDOW_CLAMP isn't accurate.

You can read the internal tcp socket parameters with this (linux-specific, non-portable) socket option:

struct tcp_info ti;
socklen_t tisize = sizeof(ti);
getsockopt(fd, IPPROTO_TCP, TCP_INFO, &ti, &tisize);

The structure tcp_info is defined in linux/tcp.h and its member ti.tcpi_rcv_space contains the advertised tcp receive window for the corresponding socket.

Since Linux 4.8 there is a new tcp socket option TCP_REPAIR_WINDOW , which allows to read (and even set) the send and receive window directly:

struct tcp_repair_window trw;
socklen_t trwsize = sizeof(trw);
getsockopt(fd, IPPROTO_TCP, TCP_REPAIR_WINDOW, &trw, &trwsize);

Now the TCP send and receive window can be read from trw.snd_wnd and trw.rcv_wnd .

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