.NET blocking socket read until X bytes are availa

2019-02-24 10:57发布

Assume I have simple protocol implemented over TCP, where each message is made up of:

  1. An int indicating data length.
  2. Binary data, of the length specified in 1.

Reading such a message I would like something like:

int length = input.ReadInt();
byte[] data = input.ReadBytes(length);

Using Socket.Receive or NetworkStream.Read the available number of bytes is read. I want the call to ReadBytes to block until length bytes are available.

Is there a simple way to do this, without having to loop over the read, restarting at an offset waiting for the remaining data?

In a real application the read should probably be done Async or on a background thread, but I've ignored that for now. The important thing is to be able to have the read not complete until all data is available.

Edit

I know that I can buffer the data myself, and I know how to do it. It's just a loop around Receive that continues at the next offset. What I am asking for is if there is a reusable implementation of such a loop, without the need for an own loop of any kind (or alternatively a reusable Async implemenation that finishes when all data is available).

6条回答
霸刀☆藐视天下
2楼-- · 2019-02-24 11:31

it sounds like the yield statement could suit this scenario just fine.

i.e. say there's a loop watching the incomming stream, and once you hit each length number, you give back control to the caller via 'yield' to IEnumerable / foreach.

Perhaps the yield could in turn signal via an event for an alternative decoupling to IEnumerable. I find IEnumerable to be convenient though.

查看更多
▲ chillily
3楼-- · 2019-02-24 11:31

Socket.Available will do the trick, if you don't mind a tight loop with a wait in?

http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.available%28VS.71%29.aspx

查看更多
Bombasti
4楼-- · 2019-02-24 11:41

You need to buffer the data yourself, the specification of read is that it can read any amount between 1 bytes and the buffer size when it returns.

查看更多
劫难
5楼-- · 2019-02-24 11:50

Take a look on the following link: http://blog.stephencleary.com/2009/04/tcpip-net-sockets-faq.html

In this link you will find excelent info on sockets and also a library.

The one thing I would make sure to implement is a timeout mechanism in order to avoid getting stuck when the network goes bad.

查看更多
ゆ 、 Hurt°
6楼-- · 2019-02-24 11:51

Something, somewhere is going to have to loop. After all, multiple socket reads could be required.

I believe that BinaryReader.Read will keep looping until either it's read as much as you've asked for or hit the end of the stream, but assuming you would want to throw an exception if you reached the end of the stream, I'd write personally write a separate method. It's easy enough to implement in one place and reuse, after all.

查看更多
▲ chillily
7楼-- · 2019-02-24 11:55

well, kind stinks we have to write a loop but I would have liked nonetheless if someone put a loop in this forum post so I could have cut and paste it real quick...here was mine(and yeah, the i thing I would rather base in time but it is only for unit tests and they control a simulated server so I don't care that much+ I don't want my unit test to get stuck on a failure when it calls my Read method)

        int numBytes = 0;
        int i = 0;
        while(numBytes != length)
        {
            numBytes += latestClient.Receive(body, numBytes, length-numBytes, SocketFlags.None);                
            if(i == 10000)
                throw new Exception("Could not read enough data.  read="+numBytes+" but expected="+length);
        }
查看更多
登录 后发表回答