I have code like this:
public byte[] Read()
{
try
{
if (ClientSocket.Available != 0)
{
var InBuffer = new byte[ClientSocket.Available];
ClientSocket.Receive(InBuffer);
return InBuffer;
}
else
{
return null;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
I'd like to make an async equivalent without totally changing the flow of the code (warts and all) and I am running into issues, I also want to switch to NetworkStream as it has built in async methods
I'd like the sig to be Task<byte[]> Read() but:
NetworkStream.ReadAsyncexpects to be passed abyte[]but doesn't return it, so I can't simplyreturn stream.Read(...)NetworkStreamdoesn't appear to tell you how many bytes are available to be read.- If there is no data available I don't want to call
stream.Readjust pass back null.
So regardless of issues in the above method - I know it is not optimal - how might I do this?
The aim being I can do, or equivalent.
byte [] bytes = await x.Read();