How to tell how much data is in a Socket's sen

2019-05-07 09:54发布

问题:

I can tell how much data is in a Socket's receive buffer by calling Socket.IOControl(IOControlCode.DataToRead, null, outValue);

Is there an equivalent to tell how much data is in a Socket's send buffer?

I need to send as many UDP packets as possible over a bandwidth and latency-constrained network, and I am running into a situation where I am overflowing the send buffer, and so I need to implement some form of throttling.

回答1:

UDP socket does not have a send buffer. The meaning of the SO_SNDBUF socket option for UDP is the limit on the size of the datagram you can send. The kernel usually does have a packet queue per NIC, but that is combined for all protocols, and there's no user-land API to query its size. IP layer, and then the hardware, can drop packets silently. TCP knows how to deal with that, UDP doesn't. So unless .Net or whatever other Microsoft miracle has such buffering in user-land libraries, you are out of luck.

The recourse is for receivers to signal the sender about dropped packets, request resends, fall back to TCP, etc. This also implies having some sort of sequencing from the sender.

Hope this helps.



回答2:

Can you analyze the header? There might be information about content length.