471,337 Members | 843 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How can I send this

Hello,

I want to send through Socket a struct

So, i'd like to do, as I did before in C++, something like

send((void*)myStruct);

where myStruct is a struct with basic types

But, I can't do this in c#

So what's the solution ?
How to send my struct ?

And the recepter side, how to match byte[] to a struct ?

Thanks for all

N.E Toulouse
Jan 12 '06 #1
5 1478
Nicolas,
In C#, you would need to :

1) Mark your struct with the [Serializable] Attribute.
2) Using the BinaryFormatter, Serialize the struct to a byte array.
3) Send the byte array via the socket.
4) At the other end Receive the byte array
5) Again, using the BinaryFormatter, Deserialize the byte array back to an
instance
of your struct type.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"nicolas ETIENNE" wrote:
Hello,

I want to send through Socket a struct

So, i'd like to do, as I did before in C++, something like

send((void*)myStruct);

where myStruct is a struct with basic types

But, I can't do this in c#

So what's the solution ?
How to send my struct ?

And the recepter side, how to match byte[] to a struct ?

Thanks for all

N.E Toulouse

Jan 12 '06 #2
THat's what I did...

My struct is :
public enum ESender
{
SCA,
SGI,
OTHER
}

[Serializable]
[ StructLayout( LayoutKind.Sequential )]
public unsafe struct MessageFormat
{
public ESender sender;
public int data1;
public int data2;
public double value;
}

My sender does this :

MessageFormat msg = new MessageFormat();

msg.sender = ESender.SGI;
msg.data1 = 10;
msg.data2 = 20;
msg.value = 12323.435f;
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();
fmt.Serialize(io, msg);
byte[] b = io.GetBuffer();

ClientSocket.Send(b);

and my receiver do this :

MessageFormat message;
if (msg.Length > 0)
{

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();

io.Write(msg, 0, msg.Length);
message = (MessageFormat)fmt.Deserialize(io);
}

but I only get a "End of Stream encountered before parsing was
completed."

So where Am i wrong in my code ?

Thanks a lot for your answer

Nicolas
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com
Jan 12 '06 #3
Use
io.Seek(0,0);

to reset to the beginning of the stream before deserializing.
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"nicolas" wrote:
THat's what I did...

My struct is :
public enum ESender
{
SCA,
SGI,
OTHER
}

[Serializable]
[ StructLayout( LayoutKind.Sequential )]
public unsafe struct MessageFormat
{
public ESender sender;
public int data1;
public int data2;
public double value;
}

My sender does this :

MessageFormat msg = new MessageFormat();

msg.sender = ESender.SGI;
msg.data1 = 10;
msg.data2 = 20;
msg.value = 12323.435f;
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();
fmt.Serialize(io, msg);
byte[] b = io.GetBuffer();

ClientSocket.Send(b);

and my receiver do this :

MessageFormat message;
if (msg.Length > 0)
{

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();

io.Write(msg, 0, msg.Length);
message = (MessageFormat)fmt.Deserialize(io);
}

but I only get a "End of Stream encountered before parsing was
completed."

So where Am i wrong in my code ?

Thanks a lot for your answer

Nicolas
--
Sent via .NET Newsgroups
http://www.dotnetnewsgroups.com

Jan 12 '06 #4
Peter Bromberg [C# MVP] <pb*******@yahoo.nospammin.com> wrote:
Use
io.Seek(0,0);

to reset to the beginning of the stream before deserializing.


Or (IMO clearer - which parameter means which, without looking them
up?):

io.Position = 0;

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jan 12 '06 #5
>1) Mark your struct with the [Serializable] Attribute.
2) Using the BinaryFormatter, Serialize the struct to a byte array.
3) Send the byte array via the socket.
4) At the other end Receive the byte array
5) Again, using the BinaryFormatter, Deserialize the byte array back to an
instance of your struct type.
Peter
That's what I did...

My code :

public enum ESender

{

SCA,

SGI,

OTHER

}

[Serializable]

[ StructLayout( LayoutKind.Sequential )]

public unsafe struct MessageFormat

{

public ESender sender;

public int data1;

public int data2;

public double value;

}

This is the Struct

Somewhere in the sender , I do this :

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();

fmt.Serialize(io, msg);

byte[] b = io.GetBuffer();

ClientSocket.Send(b);

This seems to work fine

And, in the receiver side, I do this ...

MessageFormat message;

if (msg.Length > 0)

{

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter fmt = new

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

MemoryStream io = new MemoryStream();

io.Write(msg, 0, msg.Length);

message = (MessageFormat)fmt.Deserialize(io);

}

But the fmt.Deserialize(io) line leads when executing to this : End of
Stream encountered before parsing was completed.

So, where am i wrong ? what's the deal I missed somehow ?

Nicolas

"nicolas ETIENNE" <ni*************@oktal.fr> a écrit dans le message de
news: dq**********@apollon.grec.isp.9tel.net... Hello,

I want to send through Socket a struct

So, i'd like to do, as I did before in C++, something like

send((void*)myStruct);

where myStruct is a struct with basic types

But, I can't do this in c#

So what's the solution ?
How to send my struct ?

And the recepter side, how to match byte[] to a struct ?

Thanks for all

N.E Toulouse

Jan 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by David Burson | last post: by
11 posts views Thread by Wenfei | last post: by
reply views Thread by Markus Poehler | last post: by
3 posts views Thread by ultr | last post: by

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.