title: Sending Serialized Data
body: <p>Basically I am doing some networking with a client and server sending "packets" back and forth to each other. I have it working with basic variable data such as ints or strings passing back and forth, however now I want to pass an object.</p>
  
  <p>So I know I have to serialize the data of the object to pass it through the socket. That is working as well (as I can get the correct information if I serialize then de-serialize right away) but the problem comes in when my server receives a packet.</p>
  
  <p>My server interprets packet data based on the first 2 characters of the packet. So <code>01foobar</code> is a type of packet correlating to whatever "01" is assigned to and <code>02foobar</code> is a different packet as well. So I don't know the best way to do this with an object attached. What is mean is this...</p>
  
  <p>The way I have tried to do it right now is, serialize my object and get it's string. Then append on 03 to the front. So basically I have a string that looks like <code>03[B@3e9513b7</code> (or whatever) then do getBytes() on that string which gives me another <code>byte[]</code> (so I can send it through the socket). Then when the server receives that information, I can append the 03 off and I'm left with just <code>[B@3e9513b7</code>. The problem is, <code>[B@3e9513b7</code> is now a string, and not a byte[] and in order to deserialize I need to send it the same <code>byte[]</code> as it gave me when it serialized that data. So that got me looking into a way to make <code>[B@3e9513b7</code> BE the <code>byte[]</code> (aka, so when I do <code>toString()</code> on that new <code>byte[]</code> it returns <code>[B@3e9513b7</code>) but was having issues assigning it like that because it would give me a new <code>byte[]</code> for <code>[B@3e9513b7</code> as a string. So obviously then, when I send it to be deserialized it has a <code>byte[]</code> that it doesn't know what to do with and throws an error.</p>
  
  <p>So I have to imagine there's a better way to do this, and I'm just making things more complicated than they should be. Any recommendations? I can provide code snippets if needed.</p>
  
  <p>Thanks guys! </p>
  
  <p>Edit: I guess I should mention that I am using Java with using UDP sockets.</p>