473,387 Members | 3,820 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,387 software developers and data experts.

send struct through network

Hey,

I have following struct:

struct Packet
{
string Command;
string[] Args;
byte[] Data;
}

how can I send (and receive) this throug a network? (some example code)

thx
Nov 16 '05 #1
3 7724
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I have following struct:

struct Packet
{
string Command;
string[] Args;
byte[] Data;
}

how can I send (and receive) this throug a network? (some example code)


It depends how exactly you want to send it. Will serialization do? If so,
then just mark it with the serializable attribute.

The transport mechanism will very much depend on how you take the struct
apart and put it back together again.
--
Regards,

Tim Haughton

Chief Technical Officer
Agitek Ltd
www.agitek.co.uk
Nov 16 '05 #2
Dirk,

I would serialize the thing and then send that array of bytes over the
network.

First, declare your structure as serializable:

[Serializable]
struct Packet
{
string Command;
string[] Args;
byte[] Data;
}

When you want to send it, serialize it to a MemoryStream, and get the
array of bytes:

private static byte[] SerializeToByteArray(object graph)
{
// Create a memory stream, and serialize.
using (MemoryStream stream = new MemoryStream())
{
// Create a binary formatter.
IFormatter formatter = new BinaryFormatter();

// Serialize.
formatter.Serialize(stream, graph);

// Now return the array.
return stream.ToArray();
}
}

You can then send that array of bytes over the socket. To reverse the
process, just deserialize the array. I would place a length descriptor
before the content, so you know how much to read.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
Hey,

I have following struct:

struct Packet
{
string Command;
string[] Args;
byte[] Data;
}

how can I send (and receive) this throug a network? (some example code)

thx

Nov 16 '05 #3
Yes,

can somebody write some little piece of code, how to serialize an send it...
I think the receiver side I get myself

"Tim Haughton" <ti******************@omnibus.SPAMco.uk> schrieb im
Newsbeitrag news:Si*******************@news.easynews.com...
"Dirk Reske" <_F*******@gmx.net> wrote in message
news:O8*************@TK2MSFTNGP09.phx.gbl...
I have following struct:

struct Packet
{
string Command;
string[] Args;
byte[] Data;
}

how can I send (and receive) this throug a network? (some example code)


It depends how exactly you want to send it. Will serialization do? If so,
then just mark it with the serializable attribute.

The transport mechanism will very much depend on how you take the struct
apart and put it back together again.
--
Regards,

Tim Haughton

Chief Technical Officer
Agitek Ltd
www.agitek.co.uk

Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Tom | last post by:
I try to write a simple tcp based job queue server. It's purpose is to get commands from a client and store them in a queue (STL). The server has a thread which checks periodically the queue,...
1
by: Amadej | last post by:
Hello everyone, I'm having some odd problems with a little program I wrote for sending/receiving bytes across the network. I am using synchronous sockets, and it seems that when I send byte...
2
by: Shreddy | last post by:
Hi, I'm trying (or struggling) to convert some C code to C#. The existing C client is sending a structure via a TCP socket to a network server. The structure contains a mix of int and char...
20
by: newbie | last post by:
Hi all, http://www.aswin.be/nictransfer.JPG How can I get these numbers with VB.NET ? I thought somewhere in WMI but I can't find them. Thank you, Aswin
5
by: nicolas ETIENNE | last post by:
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
8
by: shapper | last post by:
Hello, I am trying to send an email using Asp.Net 2.0. I am getting the following error: System.Net.Mail.SmtpFailedRecipientException: Mailbox unavailable. The server response was: No such...
2
by: =?Utf-8?B?Y2hpZWtv?= | last post by:
Hello, I have Asp.net 2.0 app that needs to send an email. It works fine when the app tries to send inside of the network, but I can’t send email outside. Does anyone know how I can resolve...
9
by: iceman | last post by:
Hi, I am having a varible in a structure as uint8_t eg struct eg { uint8_t a; uint16_t b; } I send this I use htons for uint16 to align it .I left uint8_as it is.But, when I sent it through...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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.