473,385 Members | 1,582 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Parsing binary data (byte[])

Hi,

I'm pretty new to C#, and recently I have been experimenting with sockets.
However, I can't find a nice way (I've been fiddling with for-loops to no
avail) to parse the data that comes in over the socket. I realise this is
a bit of a general subject and there must be some good articles on it, so
any pointers to a good article dealing with this are welcome.

One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).

To make it slightly more complicated, the response also has a few 0x00
(SOH in ASCII) bytes in it, indicating different parts of the response
(kinda like a seperator, seperating general server info, server rules and
players). Also, the last part of the response has a few ASCII values
(player info) 'grouped' by having an extra byte (0x1d if memory serves me)
prepended.

Any help is greatly appreciated.
Nov 15 '05 #1
4 5576
Can you represent the packet as a struct? Post the actual data format of
the packet and maybe sample data and we could help better.

--
William Stacey, MVP

"Hans Nieser" <h.******@xs4all.invalid> wrote in message
news:3f*********************@news.xs4all.nl...
Hi,

I'm pretty new to C#, and recently I have been experimenting with sockets.
However, I can't find a nice way (I've been fiddling with for-loops to no
avail) to parse the data that comes in over the socket. I realise this is
a bit of a general subject and there must be some good articles on it, so
any pointers to a good article dealing with this are welcome.

One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).

To make it slightly more complicated, the response also has a few 0x00
(SOH in ASCII) bytes in it, indicating different parts of the response
(kinda like a seperator, seperating general server info, server rules and
players). Also, the last part of the response has a few ASCII values
(player info) 'grouped' by having an extra byte (0x1d if memory serves me)
prepended.

Any help is greatly appreciated.

Nov 15 '05 #2
William Stacey wrote:
Can you represent the packet as a struct? Post the actual data format of
the packet and maybe sample data and we could help better.


This is an example packet I captured:

http://www.nieser.net/files/bf1942-status.png

And this is an attempt at writing down the data format:

http://www.nieser.net/files/bf1942-status.txt (but since I have no clue on
how to write this kind of thing out, I wouldn't be surprised if noone
understood)

Also I made an error in my first post by saying SOH is 0x00 while it is
actually 0x01.
Nov 15 '05 #3
You can use MemoryStream to ease data access, if you can do with streaming
data access to get blocks of data of known size. Later you can use
System.Text.Encoding members to convert this byte stream to readable values.
I would suggest to use streaming data access and no data caching for better
performance (Its for a computer game) , but actually if slow access is
alright, you can go for structure declarations and methods to create its
instance using a byte array or byte stream or whatever u prefer to use.
"Hans Nieser" <h.******@xs4all.invalid> wrote in message
news:3f*********************@news.xs4all.nl...
William Stacey wrote:
Can you represent the packet as a struct? Post the actual data format of the packet and maybe sample data and we could help better.


This is an example packet I captured:

http://www.nieser.net/files/bf1942-status.png

And this is an attempt at writing down the data format:

http://www.nieser.net/files/bf1942-status.txt (but since I have no clue on
how to write this kind of thing out, I wouldn't be surprised if noone
understood)

Also I made an error in my first post by saying SOH is 0x00 while it is
actually 0x01.

Nov 15 '05 #4
Hans Nieser <h.******@xs4all.invalid> wrote:
One problem in particular was a response form a gameserver containing
status info. The response basically consists of ASCII values, where each
value has 1 byte prepended to it indicating the field's length (if the
value would have been 3 bytes, the length would be 4).


Right - so for each value, look at the first byte to see how long the
whole value is (so you know where the next one starts). Then using
Encoding.ASCII.GetString(byte[], int, int) to decode just the right
portion of your byte array.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Yeow | last post by:
hello, i was trying to use the fread function on SunOS and ran into some trouble. i made a simple test as follows: i'm trying to read in a binary file (generated from a fortran code) that...
9
by: Ching-Lung | last post by:
Hi all, I try to create a tool to check the delta (diff) of 2 binaries and create the delta binary. I use binary formatter (serialization) to create the delta binary. It works fine but the...
9
by: Hemang Shah | last post by:
Hello fellow Coders! ok, I"m trying to write a very simple application in C#. (Yes its my first program) What I want to do is : 1) Open a binary file 2) Search this file for a particular...
9
by: Stargate4004 | last post by:
Hi, I have am converting a VB6 application to VB.net and have run into a problem. The application connects to a TCP port on a Linux box and receives a data buffer. The data buffer contains...
2
by: Michael Kolias | last post by:
Hello everyone and happy holidays! I have a situation where I want to parse on the fly a stream of binary data. I am developing an httpmodule for asp.net for file uploading. I do not whant to...
8
by: Mark | last post by:
Hello. I am attempting to write binary data from a file to an OLE Object field, and then write the file back out from the database. I am reading and writing the files in binary mode, and using...
29
by: zoltan | last post by:
Hi, The scenario is like this : struct ns_rr { const u_char* rdata; }; The rdata field contains some fields such as :
4
by: Tom | last post by:
I am trying to update another developers code, and am stuck in a cludge. It works like this: A user uploads a file from the web page. Our code reads from a NetworkStream to a Filestream, and...
6
by: i_robot73 | last post by:
I have a file, containing hex values for dates (MMDDYYYY)<status code><??such as: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.