Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 849
Lloyd Powell Avatar asked Aug 05 '09 15:08

Lloyd Powell


1 Answers

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.

like image 157
bernhof Avatar answered Oct 26 '22 22:10

bernhof