简体   繁体   中英

How do you fill a Byte header with empty characters?

Im currently trying to add additional bytes to a byte array.

Im trying to send a header to a server that contains the computer name. However because the computer name could change for every machine im trying to create a byte array that is a specific length like 100 bytes.

Which means once i have my string header "rdmstream§" + Dns.GetHostName()" I need to add x amounts of bytes at the end or start as padding so the overall byte length = 100.

I was wondering if this was possible?

Below is an example of my code for having a set header length:

 public static void SendMultiScreen(byte[] img)
    {
        try
        {

            //string command = ("rdmstream§" + Dns.GetHostName()); //This is what I want to add.

            byte[] send = new byte[img.Length + 16]; //Create a new buffer to send to the server
            byte[] header = Encoding.Unicode.GetBytes("rdmstrea"); //Get the bytes of the header
            Buffer.BlockCopy(header, 0, send, 0, header.Length); //Copy the header to the main buffer
            fps = 800;
            Buffer.BlockCopy(img, 0, send, header.Length, img.Length); //Copy the image to the main buffer
            _clientSocket.Send(send, 0, send.Length, SocketFlags.None); //Send the image to the server


        }

As you can see as long as the message is only 8 Characters long this works fine. However I want the characters in the message to be variable.

I don't have much knowledge on bytes if im honest so any additional help would be much appreciated.

Thankyou in advance.

One can argue about it if padding is the right way to go, but you could pad the name of your host

string hostName = "OhWhatEver".PadRight(100)

then use this as input for your GetBytes call.

Edit: If you can't live with the spaces use that:

byte[] header = new byte[100];
byte[] hostname = System.Text.Encoding.Unicode.GetBytes("rdmstream§" + System.Net.Dns.GetHostName());
Array.Copy(hostname, header, hostname.Length);

If your concern is packet fragmentation: Socket has overloads to send a list of buffer segments in a single operation. That means you can do something like:

var segments = new List<ArraySegment<byte>>();
segments.Add(header);
segments.Add(img);

Note that it is not necessary for the header to be the full array; you can send a part of an array, which allows you to re-use the same buffer; for example:

byte[] buffer = new byte[MaxLength];
var segments = new List<ArraySegment<byte>>();
segments.Add(default); // placeholder
segments.Add(img);
foreach(...) {
    string val = ...
    int len = encoding.GetBytes(val, 0, val.Length, buffer, 0);
    segments[0] = new ArraySegment<byte>(buffer, 0, len);
    thisSocket.Send(segments);
}

However! to do this usually requires some kind of framing on the header - either a sentinel value (perhaps a trailing CR/LF/CRLF), or a prefix of the number of bytes that are the string - len here.


If that really isn't possible... just loop over the unused part of the array and set it to what you want, or use Array.Clear if zero is OK.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM