471,618 Members | 1,070 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

passing structs through Sockets

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
Jan 14 '06 #1
5 3261
IIRC, I have used BinarySerializer before for this.

--
William Stacey [MVP]

"Arun Kumar" <ar****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
| 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
|
|
Jan 14 '06 #2
Do you have sample or link which can guide?

Thanks

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uJ**************@TK2MSFTNGP12.phx.gbl...
IIRC, I have used BinarySerializer before for this.

--
William Stacey [MVP]

"Arun Kumar" <ar****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
| 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
|
|

Jan 14 '06 #3
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

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uJ**************@TK2MSFTNGP12.phx.gbl...
IIRC, I have used BinarySerializer before for this.

--
William Stacey [MVP]

"Arun Kumar" <ar****@hotmail.com> wrote in message
news:%2***************@TK2MSFTNGP14.phx.gbl...
| 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
|
|


Jan 15 '06 #4
Peter,

Thanks for the tip.
this is the link i got from googling.
http://msdn.microsoft.com/library/de...classtopic.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.

"Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
news:9A**********************************@microsof t.com...
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

"William Stacey [MVP]" <wi************@gmail.com> wrote in message
news:uJ**************@TK2MSFTNGP12.phx.gbl...
> IIRC, I have used BinarySerializer before for this.
>
> --
> William Stacey [MVP]
>
> "Arun Kumar" <ar****@hotmail.com> wrote in message
> news:%2***************@TK2MSFTNGP14.phx.gbl...
> | 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
> |
> |
>
>


Jan 15 '06 #5
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]

"CSharpNewBie" <CSharpNewBie@dotNET> wrote in message
news:O5**************@TK2MSFTNGP12.phx.gbl...
| Peter,
|
| Thanks for the tip.
| this is the link i got from googling.
|
http://msdn.microsoft.com/library/de...classtopic.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.
|
| "Peter Bromberg [C# MVP]" <pb*******@yahoo.nospammin.com> wrote in message
| news:9A**********************************@microsof t.com...
| > 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
| >>
| >> "William Stacey [MVP]" <wi************@gmail.com> wrote in message
| >> news:uJ**************@TK2MSFTNGP12.phx.gbl...
| >> > IIRC, I have used BinarySerializer before for this.
| >> >
| >> > --
| >> > William Stacey [MVP]
| >> >
| >> > "Arun Kumar" <ar****@hotmail.com> wrote in message
| >> > news:%2***************@TK2MSFTNGP14.phx.gbl...
| >> > | 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
| >> > |
| >> > |
| >> >
| >> >
| >>
| >>
| >>
|
|
Jan 16 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by Mike M | last post: by
8 posts views Thread by Oliver Gerlich | last post: by
17 posts views Thread by Christopher Benson-Manica | last post: by
12 posts views Thread by manochavishal | last post: by
5 posts views Thread by ravi.sathyam | last post: by
2 posts views Thread by william.w.oneill | last post: by
3 posts views Thread by iu2 | last post: by
reply views Thread by leo001 | 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.