简体   繁体   English

如何用空字符填充Byte标头?

[英]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. 但是,由于计算机名称可能会因尝试创建一个特定长度(如100个字节)的字节数组的每台计算机而改变。

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. 这意味着一旦我有了字符串头“rdmstream§” + Dns.GetHostName()”,我就需要在末尾添加x字节的字节数或作为填充开始,因此总字节数= 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. 如您所见,只要消息只有8个字符,就可以正常工作。 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. 然后将其用作您的GetBytes调用的输入。

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. 如果您担心数据包碎片: Socket有重载,可以在单个操作中发送缓冲区段列表 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. 为此,通常需要在标头上进行某种形式的构架-标记值(可能是末尾CR / LF / CRLF),或者是字符串的字节数前缀len


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. 如果确实不可能...只需遍历数组未使用的部分,并将其设置为Array.Clear如果零为OK,则使用Array.Clear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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