Read a NetworkStream to byte[] array

Posted on 2006-04-06
Last Modified: 2008-02-01
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.
                    bytesRead = networkStream.Read(readBuffer, 0, buffer);
                    if (bytesRead == buffer)
                    else if (bytesRead > 0 && bytesRead < buffer)
                        byte[] endRead = new byte[bytesRead];
                        Array.Copy(readBuffer, endRead, bytesRead);
                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.
Question by:noulouk
    LVL 9

    Accepted Solution

    The DataAvailable indicates if the network buffer is not empty. Your method may return prematurely without reading everything.
    The data is only read completely, when the Read method returns 0.
    LVL 11

    Assisted Solution

    Hello noulouk,

      I believe if you use ArrayList.AddRange instead of ArrayList.Add the method will work as you intended.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Sending a Secure fax is easy with eFax Corporate ( First, Just open a new email message.  In the To field, type your recipient's fax number You can even send a secure international fax — just include t…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now