passing structs through Sockets

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arun Kumar

    #1

    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


  • William Stacey [MVP]

    #2
    Re: passing structs through Sockets

    IIRC, I have used BinarySerialize r before for this.

    --
    William Stacey [MVP]

    "Arun Kumar" <arunpv@hotmail .com> wrote in message
    news:%237fpoaTG GHA.648@TK2MSFT NGP14.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
    |
    |


    Comment

    • Arun Kumar

      #3
      Re: passing structs through Sockets

      Do you have sample or link which can guide?

      Thanks

      "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
      news:uJCUtgTGGH A.1628@TK2MSFTN GP12.phx.gbl...[color=blue]
      > IIRC, I have used BinarySerialize r before for this.
      >
      > --
      > William Stacey [MVP]
      >
      > "Arun Kumar" <arunpv@hotmail .com> wrote in message
      > news:%237fpoaTG GHA.648@TK2MSFT NGP14.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
      > |
      > |
      >
      >[/color]


      Comment

      • Peter Bromberg [C# MVP]

        #4
        Re: passing structs through Sockets

        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:

        UnBlog:





        "Arun Kumar" wrote:
        [color=blue]
        > Do you have sample or link which can guide?
        >
        > Thanks
        >
        > "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
        > news:uJCUtgTGGH A.1628@TK2MSFTN GP12.phx.gbl...[color=green]
        > > IIRC, I have used BinarySerialize r before for this.
        > >
        > > --
        > > William Stacey [MVP]
        > >
        > > "Arun Kumar" <arunpv@hotmail .com> wrote in message
        > > news:%237fpoaTG GHA.648@TK2MSFT NGP14.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
        > > |
        > > |
        > >
        > >[/color]
        >
        >
        >[/color]

        Comment

        • CSharpNewBie

          #5
          Re: passing structs through Sockets

          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]" <pbromberg@yaho o.nospammin.com > wrote in message
          news:9AF5BB90-5D06-4EE6-AF21-1E76C1F6E3E8@mi crosoft.com...[color=blue]
          > 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:
          >[color=green]
          >> Do you have sample or link which can guide?
          >>
          >> Thanks
          >>
          >> "William Stacey [MVP]" <william.stacey @gmail.com> wrote in message
          >> news:uJCUtgTGGH A.1628@TK2MSFTN GP12.phx.gbl...[color=darkred]
          >> > IIRC, I have used BinarySerialize r before for this.
          >> >
          >> > --
          >> > William Stacey [MVP]
          >> >
          >> > "Arun Kumar" <arunpv@hotmail .com> wrote in message
          >> > news:%237fpoaTG GHA.648@TK2MSFT NGP14.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
          >> > |
          >> > |
          >> >
          >> >[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • William Stacey [MVP]

            #6
            Re: passing structs through Sockets

            You could do something like below.

            private void button1_Click(o bject sender, EventArgs e)
            {
            MyStruct ms = new MyStruct(30, "wjs");
            byte[] buf = ms.ToBytes();
            Console.WriteLi ne(Convert.ToBa se64String(buf) );

            MyStruct ms2 = MyStruct.FromBy tes(buf);
            Console.WriteLi ne(ms2.ToString ());
            }

            -- MyStruct.cs
            using System;
            using System.Collecti ons.Generic;
            using System.Text;
            using System.Runtime. Serialization.F ormatters.Binar y;
            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(by tes, false))
            {
            BinaryFormatter br = new BinaryFormatter ();
            MyStruct newStruct = (MyStruct)br.De serialize(ms);
            return newStruct;
            }
            }

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

            --
            William Stacey [MVP]

            "CSharpNewB ie" <CSharpNewBie@d otNET> wrote in message
            news:O5QYaSYGGH A.3856@TK2MSFTN GP12.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]" <pbromberg@yaho o.nospammin.com > wrote in message
            | news:9AF5BB90-5D06-4EE6-AF21-1E76C1F6E3E8@mi crosoft.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]" <william.stacey @gmail.com> wrote in message
            | >> news:uJCUtgTGGH A.1628@TK2MSFTN GP12.phx.gbl...
            | >> > IIRC, I have used BinarySerialize r before for this.
            | >> >
            | >> > --
            | >> > William Stacey [MVP]
            | >> >
            | >> > "Arun Kumar" <arunpv@hotmail .com> wrote in message
            | >> > news:%237fpoaTG GHA.648@TK2MSFT NGP14.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
            | >> > |
            | >> > |
            | >> >
            | >> >
            | >>
            | >>
            | >>
            |
            |


            Comment

            Working...