473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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.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.
Oct 19 '06 #1
4 1653
"Rain" <Ra**@discussions.microsoft.comwrote in message
news:A1**********************************@microsof t.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 "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
Oct 19 '06 #2
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" wrote:
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.
Oct 19 '06 #3
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 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" wrote:
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.
Oct 23 '06 #4
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" <Ra**@discussions.microsoft.comwrote in message
news:1B**********************************@microsof t.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 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" wrote:
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.

Oct 23 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
1
by: Gary Wessle | last post by:
Hi import string import re accumulator = pattern = '(\S*)\s*(\S*)\s*(\S*)' for each text file in dir openfile and read into text
3
by: raahat | last post by:
hi, i need to do desperately the way of storing american DATE formate( like 27-02-2007) in mysql table.i searched on the web a lot,but didnt find anything.PLZ i need someone's help.
6
by: Steve Ryan | last post by:
can anyone send me to some good examples of edit in place for data frid columns i see dot net let me assign a control in the IDE any good sources of reading on editing columns with controls...
1
by: icemani | last post by:
Is there any advantages that storing data in hexadecimal octal formate in data base? By Manikandan.
0
by: dinesh4u | last post by:
i have got a data in notepad in formate like 1AAMA04BC 2008 REAL ESTATE REPORT PAGE: 1 02/19/08 0412 ...
2
by: phpfreak2007 | last post by:
Hi everyone... I want to copy text from clipboard and paste into my project.I am able to do it but problem is Its not in same formate..That means I want bold letter as bold and with same...
5
by: pyroguanyu | last post by:
this is the first time i have used access. and i have been assighnd the job of getting queries of hh/mm/ss, there is 20487 entrees that i need to dived up into four time frames but the problem is...
2
by: kkshansid | last post by:
i want to know how to change date formate in visual basic project from control panel???? from mm/dd/yyyy to dd/mm/yyyy in vb calender control
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.