473,395 Members | 1,526 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1604
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
0
by: David Burson | last post by:
Hi, I have a VB.NET windows app that needs to automatically send a simple text email when my users run a new version of the app for the first time. I thought this would be simple, but after...
11
by: Wenfei | last post by:
Hi, For socket, I know how to use select and recv, but I don't know how to use select and send? Is anybody know? Thanks, Wenfei
2
by: Piotr Bieniek | last post by:
Hello, I have a problem with UDP sockets. It concerns UdpClient class as well. It throws strange exceptions on subsequent Send calls. Exception is SocketException with native error code 10049. I...
1
by: GrantS | last post by:
I need to use a sendkeys key combination to automate the "accept files" that a remote user wants to send to me via Windows messenger. I am using automation to work with Windows Messenger client in...
3
by: Gerard | last post by:
Hello I have created a windows service to monitor a database, it starts some checks when a timer elapses. The checks send emails depending on their findings. My issue is that when I created a...
0
by: Markus Poehler | last post by:
Hi there! I have created NT Service that runs on a Server. It should NET SEND to a specifiv Client in a special case of environment. It does NOT net send. The Messenger Service is running....
3
by: ultr | last post by:
Hello, I have read that send() may not send whole message and returns the nuber of bytes sent, so that we can handle the rest of the message. Is it true, because i have tested sending even 1MB...
6
by: Harshpandya | last post by:
Hi all, I am working on the form in which you fill out the whole PHP form and e mail that details to someone. It is working fine. But now i want to send the same form to be sent to different...
0
by: Xionbox | last post by:
Hello everybody, The error I have seems very easy to solve, but for some odd reason I can't seem to solve it. Anyways, here's my "setup". I created a server running on localhost:1200 (telnet...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.