Lloyd Powell
Lloyd Powell

Reputation: 18780

Sockets, Get Number of Bytes Available (c#)

Good afternoon,

OK, short and sweet.

I need to get the number of bytes available for read from a socket. I have setup a NetworkStream on my Socket Client but can't seem to find how to get the number of bytes that are available to be read, at the moment I can only get a boolean stating "Yes I have Bytes", or "No Bytes this time". But this is all but useful for the task I require.

Could anyone put me out of my misery and provide me with my need?

Thanks in advance, appreciated.

Upvotes: 5

Views: 2892

Answers (2)

bernhof
bernhof

Reputation: 6320

As specified in this MSDN article about the NetworkStream.Length property:

Gets the length of the data available on the stream. This property always throws a NotSupportedException.

This is due to the fact that data is fed to the stream as it arrives, thus having no actual length.

However, the Socket.Available property tells you how many bytes are available to be read at this exact moment. The value may change at any given time if new data arrives.

Upvotes: 8

Thomas Levesque
Thomas Levesque

Reputation: 292425

Socket.Available

Upvotes: 4

Related Questions