UDP message formate problem

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

    UDP message formate problem

    Im using a UDPClient Send to send message to the server heres my code:

    MsgProperties _msgPro = new MsgProperties() ;
    byte[] bs = System.Text.Enc oding.UTF8.GetB ytes( _msgPro._seqNo +
    _msgPro._msgLen + Message);
    x = thisClient.Send (bs,(int) bs.Length, RemoteEndPoint) ;

    and the struct is

    [StructLayout(La youtKind.Explic it)]
    public struct MsgProperties
    {
    [FieldOffset(0)]
    public short _seqNo;
    [FieldOffset(2)]
    public short _msgLen;
    [FieldOffset(4)]
    public short _checkSum;
    }

    the problem is:

    1.) i want _msgPro._seqNo + _msgPro._msgLen + Message to be concatenated and
    not plus. I need it to be on the right bytes in sending and in receiving..
    any good ideas?

    2.) How can i get for example the 1st 2 bytes or the next 2 bytes of a
    string... ?

    3.) Can i place _msgPro._seqNo, _msgPro._msgLen & _msgPro._checkS um in one
    variable? what type? how?

    I would really appreciate any help.. i really cant understand how to do
    this.. please help.. than you so much in advance.
  • Peter Duniho

    #2
    Re: UDP message formate problem

    "Rain" <Rain@discussio ns.microsoft.co mwrote in message
    news:A1825433-9ACA-4F38-87D7-A6ED300E37C1@mi crosoft.com...
    1.) i want _msgPro._seqNo + _msgPro._msgLen + Message to be concatenated
    and
    not plus. I need it to be on the right bytes in sending and in receiving..
    any good ideas?
    It's not clear from your post what you want to do. The code you posted
    seems to be trying to send a string representation of the "MsgPropert ies"
    structure, but if you're going to send the data as a string, there's no
    reason to bother with the explicitly layed-out structure you've got.

    Regardless of how you want to send the data, you appear to be initializing
    your byte array using an uninitialized "MsgPropert ies" structure.

    If you have an *initialized* "MsgPropert ies" structure called "_msgPro",
    *and* you want a string that represents the values in the structure, then
    you need to convert each field of the structure to a string, along the lines
    of "_msgPro._seqNo .ToString()". Of course, without formatting, the length
    of this string will be variable. So you either need to impose some
    formatting (using String.Format() instead of the ToString() method of the
    fields), or you need to delimit the data with the string somehow (perhaps by
    putting spaces between each number).

    If you're not actually looking for a string that represents your structure,
    then you should forget about using the string stuff altogether. Just send
    the structure you've defined.
    2.) How can i get for example the 1st 2 bytes or the next 2 bytes of a
    string... ?
    It's not clear what you are looking for here. The first two bytes of a
    String (Unicode) compose the first character of that string. So if you
    really want the first two bytes, you just get the first character. Likewise
    the next two bytes. If you need to actually convert those to two separate
    bytes, the BitConverter class can handle that for you.
    3.) Can i place _msgPro._seqNo, _msgPro._msgLen & _msgPro._checkS um in one
    variable? what type? how?
    It's not clear to me, once again, what you're asking. The "MsgPropert ies"
    structure you've defined does exactly that. The "_msgPro" variable you've
    declared stores exactly those values. It seems very odd to me for you to
    ask for a different variable to store those values in. If you can explain
    more specifically what else you are looking for beyond that, it would help
    with respect to getting an actual answer that makes sense.

    Pete


    Comment

    • Adityanand Pasumarthi

      #3
      RE: UDP message formate problem

      Hi Rain,

      Probably the following self contained structure will help you in your efforts.

      struct Message
      {
      public short sequenceNumeber ;
      public short length;
      public short checksum;
      public string data;

      public int GetMessageLengt h()
      {
      return 2 + 2 + 2 + data.Length;
      }

      public byte[] ToBytes()
      {
      byte[] msgBytes = new byte[this.GetMessage Length()];
      MemoryStream ms = new MemoryStream(ms gBytes);
      BinaryWriter bw = new BinaryWriter(ms );
      bw.Write(sequen ceNumeber);
      bw.Write((short )data.Length);
      bw.Write(checks um);
      bw.Write(Encodi ng.ASCII.GetByt es(data));
      bw.Close();
      return msgBytes;
      }

      public static Message FromBytes(byte[] msgBytes)
      {
      Message msg = new Message();
      MemoryStream ms = new MemoryStream(ms gBytes);
      BinaryReader br = new BinaryReader(ms );
      msg.sequenceNum eber = br.ReadInt16();
      msg.length = br.ReadInt16();
      msg.checksum = br.ReadInt16();
      msg.data = Encoding.ASCII. GetString(br.Re adBytes(msg.len gth));
      br.Close();
      return msg;
      }
      }

      The above structure can be used like this...

      Message msg = new Message();
      msg.sequenceNum eber = 1;
      msg.data = "Aditya";
      msg.checksum = 90;
      msg.length = (short) msg.data.Length ;
      byte[] msgBytes = msg.ToBytes();
      Message msgTemp = Message.FromByt es(msgBytes);
      Console.WriteLi ne(msg.data);

      Let me know if you have any doubts.

      --
      Regards,
      Aditya.P


      "Rain" wrote:
      Im using a UDPClient Send to send message to the server heres my code:
      >
      MsgProperties _msgPro = new MsgProperties() ;
      byte[] bs = System.Text.Enc oding.UTF8.GetB ytes( _msgPro._seqNo +
      _msgPro._msgLen + Message);
      x = thisClient.Send (bs,(int) bs.Length, RemoteEndPoint) ;
      >
      and the struct is
      >
      [StructLayout(La youtKind.Explic it)]
      public struct MsgProperties
      {
      [FieldOffset(0)]
      public short _seqNo;
      [FieldOffset(2)]
      public short _msgLen;
      [FieldOffset(4)]
      public short _checkSum;
      }
      >
      the problem is:
      >
      1.) i want _msgPro._seqNo + _msgPro._msgLen + Message to be concatenated and
      not plus. I need it to be on the right bytes in sending and in receiving..
      any good ideas?
      >
      2.) How can i get for example the 1st 2 bytes or the next 2 bytes of a
      string... ?
      >
      3.) Can i place _msgPro._seqNo, _msgPro._msgLen & _msgPro._checkS um in one
      variable? what type? how?
      >
      I would really appreciate any help.. i really cant understand how to do
      this.. please help.. than you so much in advance.

      Comment

      • Rain

        #4
        RE: UDP message formate problem

        Thanks man, you're great.. hehe its definitely helped a lot. Thank
        again!!!!!!!!!! !!!

        "Adityanand Pasumarthi" wrote:
        Hi Rain,
        >
        Probably the following self contained structure will help you in your efforts.
        >
        struct Message
        {
        public short sequenceNumeber ;
        public short length;
        public short checksum;
        public string data;
        >
        public int GetMessageLengt h()
        {
        return 2 + 2 + 2 + data.Length;
        }
        >
        public byte[] ToBytes()
        {
        byte[] msgBytes = new byte[this.GetMessage Length()];
        MemoryStream ms = new MemoryStream(ms gBytes);
        BinaryWriter bw = new BinaryWriter(ms );
        bw.Write(sequen ceNumeber);
        bw.Write((short )data.Length);
        bw.Write(checks um);
        bw.Write(Encodi ng.ASCII.GetByt es(data));
        bw.Close();
        return msgBytes;
        }
        >
        public static Message FromBytes(byte[] msgBytes)
        {
        Message msg = new Message();
        MemoryStream ms = new MemoryStream(ms gBytes);
        BinaryReader br = new BinaryReader(ms );
        msg.sequenceNum eber = br.ReadInt16();
        msg.length = br.ReadInt16();
        msg.checksum = br.ReadInt16();
        msg.data = Encoding.ASCII. GetString(br.Re adBytes(msg.len gth));
        br.Close();
        return msg;
        }
        }
        >
        The above structure can be used like this...
        >
        Message msg = new Message();
        msg.sequenceNum eber = 1;
        msg.data = "Aditya";
        msg.checksum = 90;
        msg.length = (short) msg.data.Length ;
        byte[] msgBytes = msg.ToBytes();
        Message msgTemp = Message.FromByt es(msgBytes);
        Console.WriteLi ne(msg.data);
        >
        Let me know if you have any doubts.
        >
        --
        Regards,
        Aditya.P
        >
        >
        "Rain" wrote:
        >
        Im using a UDPClient Send to send message to the server heres my code:

        MsgProperties _msgPro = new MsgProperties() ;
        byte[] bs = System.Text.Enc oding.UTF8.GetB ytes( _msgPro._seqNo +
        _msgPro._msgLen + Message);
        x = thisClient.Send (bs,(int) bs.Length, RemoteEndPoint) ;

        and the struct is

        [StructLayout(La youtKind.Explic it)]
        public struct MsgProperties
        {
        [FieldOffset(0)]
        public short _seqNo;
        [FieldOffset(2)]
        public short _msgLen;
        [FieldOffset(4)]
        public short _checkSum;
        }

        the problem is:

        1.) i want _msgPro._seqNo + _msgPro._msgLen + Message to be concatenated and
        not plus. I need it to be on the right bytes in sending and in receiving..
        any good ideas?

        2.) How can i get for example the 1st 2 bytes or the next 2 bytes of a
        string... ?

        3.) Can i place _msgPro._seqNo, _msgPro._msgLen & _msgPro._checkS um in one
        variable? what type? how?

        I would really appreciate any help.. i really cant understand how to do
        this.. please help.. than you so much in advance.

        Comment

        • DC

          #5
          Re: UDP message formate problem

          for a more rigorous checksum look at CRC32 (there will be a managed version
          out there somewhere or you could roll your own)


          "Rain" <Rain@discussio ns.microsoft.co mwrote in message
          news:1B298533-4812-4CB9-BA63-0BB5EBFB0CB9@mi crosoft.com...
          Thanks man, you're great.. hehe its definitely helped a lot. Thank
          again!!!!!!!!!! !!!
          >
          "Adityanand Pasumarthi" wrote:
          >
          >Hi Rain,
          >>
          >Probably the following self contained structure will help you in your
          >efforts.
          >>
          >struct Message
          >{
          >public short sequenceNumeber ;
          >public short length;
          >public short checksum;
          >public string data;
          >>
          >public int GetMessageLengt h()
          >{
          >return 2 + 2 + 2 + data.Length;
          >}
          >>
          >public byte[] ToBytes()
          >{
          >byte[] msgBytes = new byte[this.GetMessage Length()];
          >MemoryStream ms = new MemoryStream(ms gBytes);
          >BinaryWriter bw = new BinaryWriter(ms );
          >bw.Write(seque nceNumeber);
          >bw.Write((shor t)data.Length);
          >bw.Write(check sum);
          >bw.Write(Encod ing.ASCII.GetBy tes(data));
          >bw.Close();
          >return msgBytes;
          >}
          >>
          >public static Message FromBytes(byte[] msgBytes)
          >{
          >Message msg = new Message();
          >MemoryStream ms = new MemoryStream(ms gBytes);
          >BinaryReader br = new BinaryReader(ms );
          >msg.sequenceNu meber = br.ReadInt16();
          >msg.length = br.ReadInt16();
          >msg.checksum = br.ReadInt16();
          >msg.data = Encoding.ASCII. GetString(br.Re adBytes(msg.len gth));
          >br.Close();
          >return msg;
          >}
          >}
          >>
          >The above structure can be used like this...
          >>
          >Message msg = new Message();
          >msg.sequenceNu meber = 1;
          >msg.data = "Aditya";
          >msg.checksum = 90;
          >msg.length = (short) msg.data.Length ;
          >byte[] msgBytes = msg.ToBytes();
          >Message msgTemp = Message.FromByt es(msgBytes);
          >Console.WriteL ine(msg.data);
          >>
          >Let me know if you have any doubts.
          >>
          >--
          >Regards,
          >Aditya.P
          >>
          >>
          >"Rain" wrote:
          >>
          Im using a UDPClient Send to send message to the server heres my code:
          >
          MsgProperties _msgPro = new MsgProperties() ;
          byte[] bs = System.Text.Enc oding.UTF8.GetB ytes( _msgPro._seqNo +
          _msgPro._msgLen + Message);
          x = thisClient.Send (bs,(int) bs.Length, RemoteEndPoint) ;
          >
          and the struct is
          >
          [StructLayout(La youtKind.Explic it)]
          public struct MsgProperties
          {
          [FieldOffset(0)]
          public short _seqNo;
          [FieldOffset(2)]
          public short _msgLen;
          [FieldOffset(4)]
          public short _checkSum;
          }
          >
          the problem is:
          >
          1.) i want _msgPro._seqNo + _msgPro._msgLen + Message to be
          concatenated and
          not plus. I need it to be on the right bytes in sending and in
          receiving..
          any good ideas?
          >
          2.) How can i get for example the 1st 2 bytes or the next 2 bytes of a
          string... ?
          >
          3.) Can i place _msgPro._seqNo, _msgPro._msgLen & _msgPro._checkS um in
          one
          variable? what type? how?
          >
          I would really appreciate any help.. i really cant understand how to do
          this.. please help.. than you so much in advance.

          Comment

          Working...