Link to home
Start Free TrialLog in
Avatar of noulouk
noulouk

asked on

Read a NetworkStream to byte[] array

Hi Experts,

Here is my method:
private byte[] ReadNetworkStream(NetworkStream networkStream, int buffer)
        {
            ArrayList arrayList = new ArrayList();
            if (networkStream.CanRead)
            {
                byte[] readBuffer = new byte[buffer];
                int bytesRead = 0;

                // Incoming message may be larger than the buffer size.
                do
                {
                    Console.WriteLine("reading");
                    bytesRead = networkStream.Read(readBuffer, 0, buffer);
                    Console.WriteLine("readed");
                    if (bytesRead == buffer)
                        arrayList.Add(readBuffer);
                    else if (bytesRead > 0 && bytesRead < buffer)
                    {
                        byte[] endRead = new byte[bytesRead];
                        Array.Copy(readBuffer, endRead, bytesRead);
                        arrayList.Add(endRead);
                        break;
                    }
                    else
                        break;
                }
                while (networkStream.DataAvailable);
            }
            return (byte[])arrayList.ToArray(typeof(byte));
        }

But it doesn't work. (I don't want to use Encoding, because I only want binary datas)

Thanks in advance for your help.
ASKER CERTIFIED SOLUTION
Avatar of pallosp
pallosp

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial