Code:
struct Data
{
public string strName;
publci string strMessage;
}
Now what I need to do is to change the data to a stream of bytes and transmit it. And, at the receiver end organize it back into the structure.
How can I do this
struct Data
{
public string strName;
publci string strMessage;
}
Convert a struct to stream of bytes
mrmckeb
Can you please help :)
Black_Keyboard
The problem with serialization is that it inserts a kind of bookmark into the stream and no other app can read it.
And, can you please tell me how would it work if my struct had an int, float, and a double
struct Data
{
string strName;
string strMsg;
int n;
float f;
double d;
}
Thanks.
kazys
hi,
i guess the same object (data stract) should be in both your server and client sides
http://msdn2.microsoft.com/en-us/library/72hyey7b(VS.80).aspx
in those articles i don't remember were exactly i have read something which disappointed me a little bit to use serialization it was about security permission because serialization expose your class, that could expose you to millicious code or something, so i don't use serialization except when i have to. serialization aim is to make you able to deal with reference types (classes) as value types (struct), since what you have is a struct so i guess its better to add 2 methods one to compose bytes , the other one is to compose your struct from bytes without depending on serialization.
because your struct depend on strings i guess you have to add variable for the struct length like for example
void byte[] ToByte()
{
List<byte> result = new List<byte>();
result.AddRange(BitConverter.GetBytes(strName.Length));
result.AddRange(BitConverter.GetBytes(strMessage.Length));
result.AddRange(Text.Encoding.UTF8.GetBytes(strName));
result.AddRange(Text.Encoding.UTF8.GetBytes(strMessage));
return result.ToArray()
}
//constractor in your data struct to read
internal Data(byte[] data)
{
int nameLen = BitConverter.ToInt32(data, 0);
int MsgLen = BitConverter.ToInt32(data, 4)
this.strName = Text.Encoding.UTF8.GetString(data, 8, nameLen);
this.strMsg = Text.Encoding.UTF8.GetString(data, 8 + nameLen, msgLen);
}
hope this helps
JustbobChico
Regards.
Mark Rendle
I think that you have not comletely read my post. See this again here is the solution you want:
"For this 2 work you need to have a same assembly (Version must also be same). You need to create a seperate DLL to Serialize and Desrialize object and in any application use the same dll to work fine. Since Serialization process puts some type of water mark in serialized object itself so when desializating it checks for the same Assembly with the same version which serialized this object. If it is the same then it desrializes it successfully else raise the exception you are getting."
I hope you understand the concept and know what to do now!
Best Regards,
dsallow
Quite same question has been answered here:
http://forums.microsoft.com/MSDN/ShowPost.aspx PostID=888375&SiteID=1
I hope this will solve your problem!
Best Regards,
Rizwan
Oliwa
But, when the server tries to deserialize the data the following exception occurs:
Please guide me...
Brian_W
hi,
most of datatypes(double, int. unint ...etc) conversion to/from bytes you use
convert type to byte array :
byte[] b = BitConverter.GetBytes(type);
Convert from byte array to type
type t = BitConverter.ToType(byteArray, startIndex);
except strings you use
byte[] b = System.Text.Encoding.<encoding>.GetBytes(string);
string s = System.Text.Encoding.<encoding>.GetString(byteArray);
hope this helps
Ondrej Vaverka
Trust me, its more than simple that you can think of if you have worked in MFC/Win32. Just create a new project of type Class Library just put your Serialization/Deseralization code in a class in that project, build the project. It'll out put a dll. Now in your previous application in Solution Exporer you'll see the node of reference, just right click and add a reference browse that dll path and its added to you project. Now you can use that class you created in that dll. Make sure to use correct namespace of that dll project. Do this with both application which want to serialize and deserialize.
I hope you'll get it working!
Best regards,
Rizwan