无法向服务器

时间:2018-04-11 10:14:57

标签: c# winforms sockets

我目前正在尝试为学校建立计算机监控软件。 我现在可以按下显示器,连接到我服务器的所有设备都会启动屏幕共享。

它将图像作为字节发送,带有一个小标题,表示它启动远程桌面流。“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;
        }
    }
}

SERVER:

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);
        }

All似乎有点令人困惑,但基本上,因为在编程客户端时,我希望标头是一个固定变量,我告诉服务器检查16个字节到数据包“8 * 2”。那么如果每次标题长度不同,服务器如何检查标题的结束和开始?

0 个答案:

没有答案