473,831 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7807
"Dirk Reske" <_F*******@gmx. net> wrote in message
news:O8******** *****@TK2MSFTNG P09.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[] SerializeToByte Array(object graph)
{
// Create a memory stream, and serialize.
using (MemoryStream stream = new MemoryStream())
{
// Create a binary formatter.
IFormatter formatter = new BinaryFormatter ();

// Serialize.
formatter.Seria lize(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.co m
"Dirk Reske" <_F*******@gmx. net> wrote in message
news:O8******** *****@TK2MSFTNG P09.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******** ***********@new s.easynews.com. ..
"Dirk Reske" <_F*******@gmx. net> wrote in message
news:O8******** *****@TK2MSFTNG P09.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
3253
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, executes the commands in it and removes them from the queue after done so. It also has a thread for every client connection. I am using the low level SOCKET API. My server is a Win32 console app and my client is MFC. The followinf struct is passed...
1
2908
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 after byte too fast, the packet gets lots somewhere (as odd as that sounds). Here's the sending code:
2
10365
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 data types . I can re-create the structure in c# with LayoutKind.Explicit, but how do I convert this structure to a byte array for use with the Socket.Send() method?
20
5396
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
1626
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
7289
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 user here at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify,
2
1898
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 the problem? My code is as follows: Dim objMail As New System.Net.Mail.MailMessage 'populage info objMail.From = New MailAddress(“admin@mail.com”)
9
9090
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 the socket.
0
5130
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 localhost 1200 proves that it is up and running) and I coded a client which is supposed to connect to that server and send it data. The odd part is that the client connects to the server (via connect()) without any problem, but three lines later, it...
0
9794
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
1
10539
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5788
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3968
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.