Jump to content
  • Advertisement
Sign in to follow this  
ArchG

[.net] Class (with an array) To Bytes

This topic is 4158 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to work with some Networking (something i've never really done) and have been able to send back and forth, byte arrays. So I thought maybe i'd make a class, and convert it a byte array...and send that back and forth. I hit my problem when I couldn't convert my class to bytes (because of an array). I wanted to make a class similar to this
class Packet
{
    //an enum (int) here
    PacketType type;
    byte[] data;

    public Packet(PacketType Type, byte[] Data)
    {
          type = Type;
          data = Data;
    }
}
I hit my compile error on this line (of my Serialization Function)
Marshal.SizeOf(Packet)
saying it can't figure out the size of Packet. so I'm not quite sure what to do to be able to convert this to bytes, and then send it over the network. I think maybe something to do the with the InteropServices Namespace, but I don't know what anything is in there. Any help would be appreciated. Thanks, ArchG

Share this post


Link to post
Share on other sites
Advertisement
This should do what you are trying to do.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary

[Serializable]
class Packet : ISerializable
{
//an enum (int) here
PacketType type;
byte[] data;

public Packet(PacketType Type, byte[] Data)
{
type = Type;
data = Data;
}

public Packet(SerializationInfo info, StreamingContext ctxt)
{
type = info.GetValue("PacketType", typeof(PacketType));
int dataLength = info.GetValue("DataLength", typeof(int));
byte[] data = new byte[dataLength];
for(int a = 0; a < dataLength; a++)
{
string temp_string = "Data" + a.ToString();
data[a] = info.GetValue(temp_string, typeof(byte));
}
}

/// <summary>
/// Serialization function.
/// </summary>
/// <param name="info"></param>
/// <param name="ctxt"></param>
new public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("PacketType", type);
info.AddValue("DataLength", data.Length);
for(int a = 0; a < data.Length; a++)
{
string temp_string = "Data" + a.ToString();
info.AddValue(temp_string, data[a]);
}

}
}





If you have any questions, just ask.
theTroll

Share this post


Link to post
Share on other sites
Thanks for your response TheTroll.

Adding your information to my class, and remaking my Serialize and DeSerialize functions...I got it to mostly work..expect the data (byte array inside the class...is always null

My Serialize and DeSerialize Functions

public static byte[] SerializeObject(object ClassToSerialize)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bfm = new BinaryFormatter();
bfm.Serialize(ms, ClassToSerialize);
return ms.ToArray();
}
public static object DeSerializeObject(byte[] ByteArray)
{
MemoryStream ms = new MemoryStream(ByteArray);
BinaryFormatter bf1 = new BinaryFormatter();
ms.Position = 0;
return bf1.Deserialize(ms);
}



This is how i'm sending the data

Packet P = new Packet(PacketType.IDRequest, Encoding.UTF8.GetBytes(textBox1.Text));

byte[] toSend = KLib.kCommon.SerializeObject(P);
mainSocket.Send(toSend, toSend.Length, SocketFlags.None);



And this is how i'm trying to receive the data


int amountReceived = 0;
Socket S = (Socket)result.AsyncState;
amountReceived = S.EndReceive(result);
Packet P = (Packet)KLib.kCommon.DeSerializeObject(buffer);

Decoder d = Encoding.UTF8.GetDecoder();
char[] chars = new char[P.data.Length + 1];

int charlen = d.GetChars(P.data, 0, P.data.Length, chars, 0);
String dataString = new System.String(chars);



I'm probably wrong on both ends..but trying my hardest to fix them.

I don't really understand any of the 'Serialization' stuff at the moment, but i'm researching it as I speak..so hopefully that can shed some light on what i'm doing.

[Edited by - ArchG on July 25, 2007 6:09:13 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!