Formating a TCP Packet

ZeroEffect

Junior Contributor
Joined
Oct 24, 2004
Messages
204
Location
Detroit, MI
Thanks for taking a look.

Awhile back I made a program for spitting data and routing it to a few destinations each in a different ascii format, All and all it was still just a ascii format. Due to some upgrades, the data that will need to be in a specific format. I am not sure if I am explaining myself adiquitly, but the best thing I can compare it to would be how an mp3 has the id3 tag information stored only in a specific place in the file. At this point I am not 100% sure of the format I need to get the upgrade done first. I do know the packet will be a UDP packet.

Everything that I have ever made have either been in vb or vb.net. I do have C for .net, and this might be a good project to start learning it.

Any information, either a sample, where I might find and example. I'll be in the MSDN if you want to find me. :)

Thanks again

ZeroEffect
 
Not exctly sure what you are asking; do you have to format a packet to work with an existing system or are you creating a new structure from scratch?

\if it is an existing system then any relevant documentation should give the appropriate structures - classes such as the BinaryReader / BinaryWriter will be of assistance in writing out the correct format.
 
More Info

PlausiblyDamp said:
Not exctly sure what you are asking; do you have to format a packet to work with an existing system or are you creating a new structure from scratch?

\if it is an existing system then any relevant documentation should give the appropriate structures - classes such as the BinaryReader / BinaryWriter will be of assistance in writing out the correct format.

Thank you for the reply :)

Here is the way the packet is formated now. It is all the data for an mp3 id3 tag then that" tag is sent out UDP.

What I am going to be forced to switch this to use an XML format. A DLL file is doing this now. I do have the source code but I don't quite follow along. I am going to have to read it a couple of more time to see if I can get a grasp of it.

Does this help out some? I am guessing that in the end I'll have XML data converted to a byte array (I am guessing) then sending that out UDP.

Any thoughts?

Thanks again for the reply and any other help you my have to offer.

ZeroEffect
 
If you are in control of the packet format then converting any format to a byte array and sending that is a fairly easy thing to do. Are you looking for a sample on how to generate the byte array, the udp packet or how to actually communicate via sockets / udp?
 
I am looking for how to create the bite array. I guess it would be simler to taking and xml file then converting it to a bye array then sending it UDP. I have got the Send of UPD packets down but the Byte array I'm not sure of. So any help with that would be great.

Thanks

ZeroEffect
 
It sounds like you are talking about building your own header that utilizes a fixed byte structure (that is how ID3 does it).

This is really pretty easy to accomplish. At the front of every file you allocate a specific number of bytes, regardless of whether you will need to use them. Actually, an ID3 tag is more of a footer as its bytes are saved at the end of the file. I'm not completely sure but I think ID3 allocates 128 bytes. This may have changed with newer versions. The idea is the same regardless. I know for 128 bytes the layout is:

(using ASCII bytes)

0-2: the word "TAG"
3-32: Song title (30 ASCII bytes)
33-62: Artist (30 ASCII bytes)
63-92: Album Title (30 ASCII bytes)
93-96: Year (4 byte integer)
97-125: Comment (28 ASCII bytes)
126: Track number (byte)
127: Genre code (byte)
 
Gill Bates said:
It sounds like you are talking about building your own header that utilizes a fixed byte structure (that is how ID3 does it).

This is really pretty easy to accomplish. At the front of every file you allocate a specific number of bytes, regardless of whether you will need to use them. Actually, an ID3 tag is more of a footer as its bytes are saved at the end of the file. I'm not completely sure but I think ID3 allocates 128 bytes. This may have changed with newer versions. The idea is the same regardless. I know for 128 bytes the layout is:

(using ASCII bytes)

0-2: the word "TAG"
3-32: Song title (30 ASCII bytes)
33-62: Artist (30 ASCII bytes)
63-92: Album Title (30 ASCII bytes)
93-96: Year (4 byte integer)
97-125: Comment (28 ASCII bytes)
126: Track number (byte)
127: Genre code (byte)


Very cool. What I don't understand is how to accompilsh this. I not going to be able to sit down and do alot in the next couple of weeks, New baby comming so most of my time will be there. Is there an example of this somewhere or even a break down of the process?

Thanks
 
It is pretty straight forward. You just carve out enough bytes for the tag and then insert the right bytes for the various parts. For example, you might have a method to make sure that you don't overflow a particular section of the tag structure:
C#:
private static byte[] GetBytes(string text, int max)
{
    byte[] bytes = Encoding.ASCII.GetBytes(text);

    byte[] ret = new byte[max];

    if (bytes.Length > max)
    {
        Array.Copy(bytes, ret, max);
    }
    else
    {
        Array.Copy(bytes, ret, bytes.Length);
    }

    return ret;
}
You could then build up the tag like this:
C#:
// carve out an array of bytes for the ID3 tag
byte[] tag = new byte[128];

byte[] title = GetBytes("Renegades of Funk", 30);
byte[] artist = GetBytes("Rage Against the Machine", 30);
byte[] year = BitConverter.GetBytes((Int32)2000);

Array.Copy(title, 0, tag, 3, title.Length);
Array.Copy(artist, 0, tag, 33, artist.Length);
Array.Copy(year, 0, tag, 93, year.Length);
// do the same for the other tag parts

// add the tag array on to the end of the binary data for the .mp3 file
 
Gill Bates said:
It is pretty straight forward. You just carve out enough bytes for the tag and then insert the right bytes for the various parts. For example, you might have a method to make sure that you don't overflow a particular section of the tag structure:
C#:
private static byte[] GetBytes(string text, int max)
{
    byte[] bytes = Encoding.ASCII.GetBytes(text);

    byte[] ret = new byte[max];

    if (bytes.Length > max)
    {
        Array.Copy(bytes, ret, max);
    }
    else
    {
        Array.Copy(bytes, ret, bytes.Length);
    }

    return ret;
}
You could then build up the tag like this:
C#:
// carve out an array of bytes for the ID3 tag
byte[] tag = new byte[128];

byte[] title = GetBytes("Renegades of Funk", 30);
byte[] artist = GetBytes("Rage Against the Machine", 30);
byte[] year = BitConverter.GetBytes((Int32)2000);

Array.Copy(title, 0, tag, 3, title.Length);
Array.Copy(artist, 0, tag, 33, artist.Length);
Array.Copy(year, 0, tag, 93, year.Length);
// do the same for the other tag parts

// add the tag array on to the end of the binary data for the .mp3 file

Ok I'm using VB.net I am sort of getting it. This is above my knowledge level but I am trying. Could you give some explination to the code as what is doing what?


I see that in the private sub getbytes is putting them into order but I am not following how it is working together.

Thanks

Sorry if I'm being a pain.

ZeroEffect
 
The GetBytes method just carves out an array of bytes. This array has [max] elements in it.

The string that you pass to this method is converted into ASCII bytes and I do a check to make sure that the resulting byte array will fit into the array of size [max] before we return it.

The other part just copies the byte arrays into the main byte array for the actual ID3 tag. So:
C#:
Array.Copy(title, 0, tag, 3, title.Length);
is just saying copy the "title" byte array to the "tag" byte array. In doing so, start reading from index 0 of the "title" array and start copying to index 3 of the "tag" array. We need to copy the data to particular locations of the tag array in order to adhere to the byte structure I mentioned before.
 
Gill Bates said:
The GetBytes method just carves out an array of bytes. This array has [max] elements in it.

The string that you pass to this method is converted into ASCII bytes and I do a check to make sure that the resulting byte array will fit into the array of size [max] before we return it.

The other part just copies the byte arrays into the main byte array for the actual ID3 tag. So:
C#:
Array.Copy(title, 0, tag, 3, title.Length);
is just saying copy the "title" byte array to the "tag" byte array. In doing so, start reading from index 0 of the "title" array and start copying to index 3 of the "tag" array. We need to copy the data to particular locations of the tag array in order to adhere to the byte structure I mentioned before.


Ok Let see, I am under standing what
C#:
byte[] title = GetBytes("Renegades of Funk", 30);
does now and looking at
C#:
Array.Copy(title, 0, tag, 3, title.Length);
you say it places the "title" array in to index 3 of the "tag" array. How do I know what index to put it in?

Thanks again

ZeroEffect
 
Look at my first post in this thread, I laid out the byte structure. You will notice that I said the title is in bytes "3-32".
 
Ahhh ok I get it! I'm a little slow today The indexes follow the standard for the ID3 tag. Very cool, now what I have to do is convert this to VB.NET then take this princable and apply it to an XML format.

Thank you Both for all the help and direction :)

ZeroEffect
 
Hmm, sounds interesting I know a little about the stream writer. I'll look into. At this point in the project I don't have the form/layout of the XML file.

Thank you again for your help


ZeroEffect
 
Back
Top