passing structs through Sockets

A

Arun Kumar

Hi

In .NET 2.0 I am trying to send a datarow or a data struct through sockets
to all connected clients.
for example heres the data struct
struct
{
string seqno
double SysTime
string Description
}

I want to send this struct to clients. I know its very simple.

Can someone guide me how it can be done.

Thanks
 
W

William Stacey [MVP]

IIRC, I have used BinarySerializer before for this.

--
William Stacey [MVP]

| Hi
|
| In .NET 2.0 I am trying to send a datarow or a data struct through sockets
| to all connected clients.
| for example heres the data struct
| struct
| {
| string seqno
| double SysTime
| string Description
| }
|
| I want to send this struct to clients. I know its very simple.
|
| Can someone guide me how it can be done.
|
| Thanks
|
|
 
W

William Stacey [MVP]

You could do something like below.

private void button1_Click(object sender, EventArgs e)
{
MyStruct ms = new MyStruct(30, "wjs");
byte[] buf = ms.ToBytes();
Console.WriteLine(Convert.ToBase64String(buf));

MyStruct ms2 = MyStruct.FromBytes(buf);
Console.WriteLine(ms2.ToString());
}

-- MyStruct.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace HIXSales
{
[Serializable]
public class MyStruct
{
public int Age;
public string Name;

public MyStruct(int age, string name)
{
this.Age = age;
this.Name = name;
}

public byte[] ToBytes()
{
using (MemoryStream ms = new MemoryStream())
{
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, this);
return ms.ToArray();
}
}

public static MyStruct FromBytes(byte[] bytes)
{
using (MemoryStream ms = new MemoryStream(bytes, false))
{
BinaryFormatter br = new BinaryFormatter();
MyStruct newStruct = (MyStruct)br.Deserialize(ms);
return newStruct;
}
}

public override string ToString()
{
return string.Format("Name:{0} Age:{1}", this.Name, this.Age);
}
}
}

--
William Stacey [MVP]

| Peter,
|
| Thanks for the tip.
| this is the link i got from googling.
|
http://msdn.microsoft.com/library/d...formattersbinarybinaryformatterclasstopic.asp
|
| I do see they have a sample which shows serialize HashTable to file and
| deSerialize.
|
| Do I use the same way to Serialize Struct. Is it possible for you to paste
a
| code snippet.
|
| Thanks.
|
| | > Arun,
| > Look up the BinaryFormatter class in the MSDN documentation or your
local
| > help.
| > There is sample code provided. Basically you are going to serialize your
| > struct to or from a byte array, which is what is send over the wire.
| > Peter
| >
| > --
| > Co-founder, Eggheadcafe.com developer portal:
| > http://www.eggheadcafe.com
| > UnBlog:
| > http://petesbloggerama.blogspot.com
| >
| >
| >
| >
| > "Arun Kumar" wrote:
| >
| >> Do you have sample or link which can guide?
| >>
| >> Thanks
| >>
| >> | >> > IIRC, I have used BinarySerializer before for this.
| >> >
| >> > --
| >> > William Stacey [MVP]
| >> >
| >> > | >> > | Hi
| >> > |
| >> > | In .NET 2.0 I am trying to send a datarow or a data struct through
| >> > sockets
| >> > | to all connected clients.
| >> > | for example heres the data struct
| >> > | struct
| >> > | {
| >> > | string seqno
| >> > | double SysTime
| >> > | string Description
| >> > | }
| >> > |
| >> > | I want to send this struct to clients. I know its very simple.
| >> > |
| >> > | Can someone guide me how it can be done.
| >> > |
| >> > | Thanks
| >> > |
| >> > |
| >> >
| >> >
| >>
| >>
| >>
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top