Need help reading bytes from a network stream

Posted on 2011-10-11
Last Modified: 2012-06-21
I am trying to read bytes out of a stream I am recieving from the network. Basically, I am conecting to a delphi service that sends a stream of bytes to my C# application. I then read those into a byte array.

Once I have the byte array, is there any way to read these and determine the data types, or do I have to decipher each byte to determine the data type and value? I haven't really worked with byte arrays beofre and would appreciate any help I can get.

This is a Windows Application.
Question by:CantonMarko
    LVL 19

    Accepted Solution

    I don't think there is any way to tell just from a byte what type it is.  Your best bet is to match the stream against a pre-determined structure or format.  For example, you know you're going to get some array of X-structures then you can read in X-structure-size worth of bytes, map the bytes to the data types in the structure, etc. for each of the number of structures in the stream.
    LVL 40

    Expert Comment

    by:Jacques Bourgeois (James Burger)
    There is not way to get the type from a byte array, since the type is not recorded in the data. You need to know the format of the file to know how many bytes to read for each values. That is one of the reasons XML was invented.

    In fact, the best way to read out of a binary stream is to use a System.IO.BinaryReader, something such as the following:
    System.IO.BinaryReader data = new System.IO.BinaryReader(yourNetWorkStream);
    int i;
    string s;
    bool b;
    i = data.ReadInt32();
    s = data.ReadString();
    b = data.ReadBoolean();

    Open in new window

    You need to know that the data is int followed by string followed by boolean.


    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Join & Write a Comment

    Suggested Solutions

    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!
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
    The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

    746 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

    13 Experts available now in Live!

    Get 1:1 Help Now