简体   繁体   中英

Measuring the TCP Bytes waiting to be read by the Socket

I have a socket connection that receives data, and reads it for processing.
When data is not processed/pulled fast enough from the socket, there is a bottleneck at the TCP level, and the data received is delayed (I can tell by the tmestamps after parsing).

How can I see how much TCP bytes are awaiting to be read by the socket ? (via some external tool like WireShark or else)

 private void InitiateRecv(IoContext rxContext)
    {
        rxContext._ipcSocket.BeginReceive(rxContext._ipcBuffer.Buffer, rxContext._ipcBuffer.WrIndex,
            rxContext._ipcBuffer.Remaining(), 0, CompleteRecv, rxContext);
    }

    private void CompleteRecv(IAsyncResult ar)
    {
        IoContext rxContext = ar.AsyncState as IoContext;
        if (rxContext != null)
        {
               int rxBytes = rxContext._ipcSocket.EndReceive(ar);

                if (rxBytes > 0)
                {
                    EventHandler<VfxIpcEventArgs> dispatch = EventDispatch;
                        dispatch (this, new VfxIpcEventArgs(rxContext._ipcBuffer));

                    InitiateRecv(rxContext);

                }
          }
     }

The fact is that I guess the "dispatch" is somehow blocking the reception until it is done, ending up in latency (ie, data that is processed bu the dispatch is delayed, hence my (false?) conclusion that there was data accumulated on the socket level or before.

How can I see how much TCP bytes are awaiting to be read by the socket

By specifying a protocol that indicates how many bytes it's about to send. Using sockets you operate a few layers above the byte level, and you can't see how many send() calls end up as receive() calls on your end because of buffering and delays.

If you specify the number of bytes on beforehand, and send a string like "13|Hello, World!" , then there's no problem when the message arrives in two parts, say "13|Hello" and ", World!" , because you know you'll have to read 13 bytes.

You'll have to keep some sort of state and a buffer in between different receive() calls.

You can use socket.Available if you are using normal Socket class. Otherwise you have to define a header byte which gives number of bytes to be sent from other end.

When it comes to external tools like Wireshark, they cannot know how many bytes are left in the socket. They only know which packets have passed by the network interface. The only way to check it with Wireshark is to actually know the last bytes you read from the socket, locate them in Wireshark, and count from there.

However, the best way to get this information is to check the Available property on the socket object in your .NET application.

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