Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 375
  • Last Modified:

CSharp problem sending ethernet data

Hi,
My Csharp is not very good, but could someone help with the following problem.
The code fragment does work, but each time 'binwriter.write' is called, it forms a new TCP message and since this is wrapped in a loop, then it is flooding the receiver..

How can I use a single call to 'binwriter.write'  with all the data preloaded in the reg_write_message.


Message reg_write_message = new Message();
// Populate the message
reg_write_message.Message_Id = Global.UPLOAD_CODE;
reg_write_message.Length = sendlength;

sendaddress = (send_loops * sendlength);

using (BinaryWriter binWriter =
 new BinaryWriter(new NetworkStream(Global.m_socClient)))
 {

  [b]// this works but sends 5 seperate Ethernet Messages[/b]
  binWriter.Write(reg_write_message.Header);
  binWriter.Write(reg_write_message.Message_Id);
  binWriter.Write(reg_write_message.Length);
  binWriter.Write(reg_write_message.data);
  binWriter.Write(reg_write_message.Footer);

  [b]// this is what I want to do ( just sending a single ethernet message[/b]
  binWriter.Write(reg_write_message);
  // but get the following 2 error messages against the above line

Error	15	The best overloaded method match for 'System.IO.BinaryWriter.Write(bool)' has some invalid arguments	
Error	16	Argument '1': cannot convert from 'SentryClient.Message' to 'bool'

 }

	

public class Message
    {
        int header;
        int message_id;
        int length;
        byte[] data = new byte[Global.MAX_MESSAGE_SIZE]; // NOTE Just a placeholder, actual length is variable MAX_MESSAGE_SIZE]
        int footer;

        public int Header
        {
            get { return header; }
            set { header = value; }
        }

        public int Message_Id
        {
            get { return message_id; }
            set { message_id = value; }
        }

        public int Length
        {
            get { return length; }
            set { length = value; }
        }

        public byte[] Data
        {
            get { return data; }
            set { data = value; }
        }

        public int Footer
        {
            get { return footer; }
            set { footer = value; }
        }

        public Message()
        {
            // Create default message.
            header = 0x7ff7ffff;
            message_id = 0;
            length = 4;
            data[0] = 0xb;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[1] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[2] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[3] = 0xf;            // Note that this is just a placeholder, actual length is variable [0..1024]
            footer = Global.FTR_WRD;
        }
    }

Open in new window


Many Thanks
0
supertramp4
Asked:
supertramp4
  • 4
  • 3
  • 2
  • +1
1 Solution
 
badbearontourCommented:
Hello,

the BinaryWriter object does not have Write method that accepts an object as a parameter.

the easiest way around this is to serialise your  Message object using xmlSerialiser, into a string format and use the BinaryWriter.Write(xmlSerialisedObejctString).

BB
0
 
badbearontourCommented:
ah yeah, forgot to mention that you will need to deserialise it at the other end to get your object back

Bb
0
 
supertramp4Author Commented:
Sorry, the receiver ( which is a hardware device) can only accept raw data, so deserialising will not be suitable.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
badbearontourCommented:
hmmm... in that case then can the device handle a comma delimited string?

if you concatenate the obejct attributes into a single string?

if not then what does the unit accept and handle?

BB
0
 
withCommented:
It sounds like it only handles bytes in that particular sequence.

If that's the case, use the Write(byte[]) overload of the BinaryWriter.  You'll need a new byte[] array for the purposes of transmittal.  This array will contain everything (header, data, footer etc) in whatever order the hardware is expecting to receive it.

The transmittal byte array will need to be a new array that is the length of Data[] + 16 more bytes (since each of the 4 integers, Header, Message_Id, Length and Footer will be included).  Basically you would send the whole message exactly as you're sending just the "Data" part now, but with the other integers tacked onto the byte array in whatever order is appropriate.

Check out the System.Converter class for converting int32 into byte[].
0
 
supertramp4Author Commented:
'badbearontour'. you may be trying to overcomplicate the problem.  'with' is on the right lines

Let say that

            reg_write_message.header = 0x7ff7ffff;
            reg_write_message.message_id = 0x01020304;
            length = 0x00000004;
            data[0] = 0xb;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[1] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[2] = 0xe;            // Note that this is just a placeholder, actual length is variable [0..1024]
            data[3] = 0xf;

rather than sending 5 seperate messages, i want to send a single message where the data is concatonated like :
... fffff77f04030201040000000b0e0e0f....

Could you show me a core fragment please
0
 
puru1981Commented:
you can mark the class serializable and then you can use binaryformatter to serialize the data and send it across
0
 
supertramp4Author Commented:
Sorry, but swift with the paste. should read

            reg_write_message.header = 0x7ff7ffff;
            reg_write_message.message_id = 0x01020304;
            reg_write_message.length = 0x00000004;
            reg_write_message.data[0] = 0xb;            
            reg_write_message.data[1] = 0xe;            
            reg_write_message.data[2] = 0xe;            
            reg_write_message.data[3] = 0xf;

rather than sending 5 seperate messages, i want to send a single message where the data is concatonated like :
... fffff77f04030201040000000b0e0e0f....
0
 
withCommented:
Here's a sketch of theoretical method you could add to the Message class (code attached)

After which, you should be able to use binWriter thusly:

binWriter.Write(reg_write_message.Serialize());

Without the hardware to debug against I can't guarantee that'll work perfectly as-is, but that should get you going in the right direction I think.
public byte[] Serialize()
{
	byte[] wholemessage = new byte[this.Data.Length + 16];

	Array.Copy(System.BitConverter.GetBytes(this.header), 0, wholemessage, 0, 4); 
	Array.Copy(System.BitConverter.GetBytes(this.message_id), 0, wholemessage, 4, 4);
	Array.Copy(System.BitConverter.GetBytes(this.length), 0, wholemessage, 8, 4); 
	Array.Copy(this.data, 0, wholemessage, 12, this.data.Length);
	Array.Copy(System.BitConverter.GetBytes(this.footer), 0, wholemessage, 12 + this.data.Length, 4);

	return wholemessage;
}

Open in new window

0
 
supertramp4Author Commented:
'With' - Thanks this gave me the framework that I needed.
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

  • 4
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now