Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 353
  • Last Modified:

Need help reading bytes from a network stream

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.
0
CantonMarko
Asked:
CantonMarko
1 Solution
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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.
0
 
Jacques Bourgeois (James Burger)PresidentCommented:
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.

0

Featured Post

Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

Tackle projects and never again get stuck behind a technical roadblock.
Join Now