UDP message formate problem

G

Guest

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

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

and the struct is

[StructLayout(LayoutKind.Explicit)]
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._checkSum 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.
 
P

Peter Duniho

Rain said:
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 "MsgProperties"
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 "MsgProperties" structure.

If you have an *initialized* "MsgProperties" 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._checkSum in one
variable? what type? how?

It's not clear to me, once again, what you're asking. The "MsgProperties"
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
 
G

Guest

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 GetMessageLength()
{
return 2 + 2 + 2 + data.Length;
}

public byte[] ToBytes()
{
byte[] msgBytes = new byte[this.GetMessageLength()];
MemoryStream ms = new MemoryStream(msgBytes);
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(sequenceNumeber);
bw.Write((short)data.Length);
bw.Write(checksum);
bw.Write(Encoding.ASCII.GetBytes(data));
bw.Close();
return msgBytes;
}

public static Message FromBytes(byte[] msgBytes)
{
Message msg = new Message();
MemoryStream ms = new MemoryStream(msgBytes);
BinaryReader br = new BinaryReader(ms);
msg.sequenceNumeber = br.ReadInt16();
msg.length = br.ReadInt16();
msg.checksum = br.ReadInt16();
msg.data = Encoding.ASCII.GetString(br.ReadBytes(msg.length));
br.Close();
return msg;
}
}

The above structure can be used like this...

Message msg = new Message();
msg.sequenceNumeber = 1;
msg.data = "Aditya";
msg.checksum = 90;
msg.length = (short) msg.data.Length;
byte[] msgBytes = msg.ToBytes();
Message msgTemp = Message.FromBytes(msgBytes);
Console.WriteLine(msg.data);

Let me know if you have any doubts.
 
G

Guest

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

Adityanand Pasumarthi said:
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 GetMessageLength()
{
return 2 + 2 + 2 + data.Length;
}

public byte[] ToBytes()
{
byte[] msgBytes = new byte[this.GetMessageLength()];
MemoryStream ms = new MemoryStream(msgBytes);
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(sequenceNumeber);
bw.Write((short)data.Length);
bw.Write(checksum);
bw.Write(Encoding.ASCII.GetBytes(data));
bw.Close();
return msgBytes;
}

public static Message FromBytes(byte[] msgBytes)
{
Message msg = new Message();
MemoryStream ms = new MemoryStream(msgBytes);
BinaryReader br = new BinaryReader(ms);
msg.sequenceNumeber = br.ReadInt16();
msg.length = br.ReadInt16();
msg.checksum = br.ReadInt16();
msg.data = Encoding.ASCII.GetString(br.ReadBytes(msg.length));
br.Close();
return msg;
}
}

The above structure can be used like this...

Message msg = new Message();
msg.sequenceNumeber = 1;
msg.data = "Aditya";
msg.checksum = 90;
msg.length = (short) msg.data.Length;
byte[] msgBytes = msg.ToBytes();
Message msgTemp = Message.FromBytes(msgBytes);
Console.WriteLine(msg.data);

Let me know if you have any doubts.

--
Regards,
Aditya.P


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

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

and the struct is

[StructLayout(LayoutKind.Explicit)]
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._checkSum 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.
 
D

DC

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


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

Adityanand Pasumarthi said:
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 GetMessageLength()
{
return 2 + 2 + 2 + data.Length;
}

public byte[] ToBytes()
{
byte[] msgBytes = new byte[this.GetMessageLength()];
MemoryStream ms = new MemoryStream(msgBytes);
BinaryWriter bw = new BinaryWriter(ms);
bw.Write(sequenceNumeber);
bw.Write((short)data.Length);
bw.Write(checksum);
bw.Write(Encoding.ASCII.GetBytes(data));
bw.Close();
return msgBytes;
}

public static Message FromBytes(byte[] msgBytes)
{
Message msg = new Message();
MemoryStream ms = new MemoryStream(msgBytes);
BinaryReader br = new BinaryReader(ms);
msg.sequenceNumeber = br.ReadInt16();
msg.length = br.ReadInt16();
msg.checksum = br.ReadInt16();
msg.data = Encoding.ASCII.GetString(br.ReadBytes(msg.length));
br.Close();
return msg;
}
}

The above structure can be used like this...

Message msg = new Message();
msg.sequenceNumeber = 1;
msg.data = "Aditya";
msg.checksum = 90;
msg.length = (short) msg.data.Length;
byte[] msgBytes = msg.ToBytes();
Message msgTemp = Message.FromBytes(msgBytes);
Console.WriteLine(msg.data);

Let me know if you have any doubts.

--
Regards,
Aditya.P


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

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

and the struct is

[StructLayout(LayoutKind.Explicit)]
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._checkSum 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.
 

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