C# 无法将可变长度标头发送到服务器

C# 无法将可变长度标头发送到服务器,c#,winforms,sockets,C#,Winforms,Sockets,我目前正试图为一所学校建立计算机监控软件。 我现在可以按monitor,所有连接到我的服务器的设备都可以开始屏幕共享 它以字节的形式发送图像,并带有一个小的报头,表示它正在启动远程桌面流。“rdmstrea”服务器知道数据包的前16个字节将包含报头 然而,我需要在数据包中发送一些系统标识符,因为当他们在屏幕共享时,我不知道谁是谁 我一直在尝试发送类似“rdmstea§”+Dns.GetHostName()的命令 所以这里有一些身份证明 然而,由于服务器不知道报头可以有多长,因此无法正确地将报头从

我目前正试图为一所学校建立计算机监控软件。 我现在可以按monitor,所有连接到我的服务器的设备都可以开始屏幕共享

它以字节的形式发送图像,并带有一个小的报头,表示它正在启动远程桌面流。“rdmstrea”服务器知道数据包的前16个字节将包含报头

然而,我需要在数据包中发送一些系统标识符,因为当他们在屏幕共享时,我不知道谁是谁

我一直在尝试发送类似“rdmstea§”+Dns.GetHostName()的命令

所以这里有一些身份证明

然而,由于服务器不知道报头可以有多长,因此无法正确地将报头从数据包中切掉,而只是收集图像数据

下面是一些有关客户端何时发送命令以及服务器接收内容的代码示例

客户:

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


    }
    catch (Exception e) //Something went wrong
    {
        try
        {
            MessageBox.Show("Connection Ended");
            Thread.Sleep(3000);

            isDisconnect = true; //Disconnect from server

        }
        catch (Exception exc) //Something went really wrong
        {
            //Restart the application
            MessageBox.Show("Failed to send Screen  original ERROR : " + exc.Message);
            Thread.Sleep(10000);
            Application.Restart();
            return;
        }
    }
}
服务器:

try //Try
{
    string header = Encoding.Unicode.GetString(recBuf, 0, 8 * 2); //Get the header of the message

    if (header == "rdmstrea") //If it's a remote desktop stream
    {

        using (MemoryStream stream = new MemoryStream()) //Declare the new memory stream
        {
            stream.Write(recBuf, 8 * 2, recBuf.Length - 8 * 2); //Copy the data from the buffer, to the memory stream
                                                                //Console.WriteLine("multiRecv Length: " + recBuf.Length);
            Bitmap deskimage = (Bitmap)Image.FromStream(stream); //Create a bitmap image from the memory stream
            if (resdataav == 0) //If resolution data is not set
            {
                resx = deskimage.Width; //Set the resolution width to the image width
                resy = deskimage.Height; //Set the resolution height to the image height
                resdataav = 1; //The resolution data is set now
            }
            SetImage(deskimage); //Set the image of the remote desktop
            Array.Clear(recBuf, 0, received); //Clear the buffer
            ignoreFlag = true; //Set the ignore flag

            GC.Collect(); //Call the garbage collector
            GC.WaitForPendingFinalizers();
            System.Threading.Thread.SpinWait(5000);
        }

所有这些看起来都有点混乱,但基本上是因为在编写客户端时,我希望头是一个固定变量,我告诉服务器将16个字节检查到数据包“8*2”中。那么,如果每次头的长度都不同,服务器将如何检查头的结束和开始位置呢?

首先想到的选项是使用具有所需信息的对象,并使用二进制序列化/反序列化,或者最简单的方法是仅为标识符设置固定长度(比如说100个字符)并在客户端将其填充到该长度,然后在服务器上对其进行修剪。顺便说一句,只要在某个地方有一个全局变量,可以用于缓冲区大小,而不是到处都有
8*2
;)@Archer会将缓冲区设置为100个字符,然后看看它是如何工作的,我向我的服务器发送了多个不同长度的命令,因此我的变量数量将与8*2 xD相同。感谢您的帮助!不用担心-祝你好运:)