0

我试图连续发送图像,但不知何故它不起作用。问题是,有时当接收器尝试将大小读入字符串时,它会读取一些未知的 6 个字节。我不知道它们来自哪里,因为我认为当读取所有字节时网络流是空的。

/// <summary>
/// This is the class that captures the screen and sends it
/// </summary>
public class TCPClient
{
    TcpClient me;
    bool lauf = true;
    NetworkStream ns;
    Thread mainThread;
    int ind = 0;

    public TCPClient()
    {
        mainThread = new Thread(new ThreadStart(sendThread));
        mainThread.Start();
    }

    public void sendThread()
    {
        try
        {
            me = new TcpClient("127.0.0.1",16500);
            ns = me.GetStream();
            while (lauf)
            {
                MemoryStream tempMS = this.getScreen();
                byte[] package = tempMS.ToArray();
                String tempS = "" + package.Length;
                byte[] tempBS = System.Text.Encoding.UTF8.GetBytes(tempS);
                ns.Write(tempBS, 0, tempBS.Length);
                ns.Write(package, 0, package.Length);    
                Console.WriteLine("Paket gesendet " + package.Length);
                //package = null;
                Console.WriteLine("Memoryuse: " + Convert.ToDouble(((Process.GetCurrentProcess().WorkingSet64))/1024)/1024);
                Thread.Sleep(1000);
                //Die ms müssen für die Steuerung evtl runtergeregelt werden,da sonst eine komfortable steuerung nicht möglich ist
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            Console.ReadKey();
            mainThread.Abort();
            ns.Close();
            me.Close(); 
        }
    }

    public MemoryStream getScreen() 
    {
        Bitmap bitmap = new Bitmap(SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);

        using (Graphics graphics = Graphics.FromImage(bitmap))
        {
            graphics.CopyFromScreen(0,0,0,0,bitmap.Size);
            Cursors.Default.Draw(graphics,new Rectangle(Cursor.Position,Cursor.Current.Size));
            graphics.Dispose();
        }

        MemoryStream memoryStream = new System.IO.MemoryStream();
        bitmap.Save(memoryStream,System.Drawing.Imaging.ImageFormat.Png);

        bitmap.Dispose();
        //memoryStream.Flush();
        memoryStream.Position = 0;
        return memoryStream;
    }

    public void setLauf(bool pLauf)
    {
        lauf = pLauf;
    }
}

public class TcpReceiver
{
    private TcpClient me;
    private MainForm MF;
    private bool lauf = true;
    private Thread recieveThread;

    public TcpReceiver(MainForm tMF)
    {
        MF = tMF;
        recieveThread = new Thread(new ThreadStart(recieveImage));
        recieveThread.Start();
    }

    public void recieveImage()
    {
        try
        {           
            TcpListener tempListener = new TcpListener(IPAddress.Any,16500);
            tempListener.Start();
            me = tempListener.AcceptTcpClient();
            //MF.newClient(me.Client.AddressFamily.ToString());
            NetworkStream ns = me.GetStream();

            while(lauf)
            {
                if (ns.DataAvailable)
                {
                    byte[] tempSA = new byte[6];
                    ns.Read(tempSA, 0, tempSA.Length);
                    String tempS = System.Text.Encoding.UTF8.GetString(tempSA);
                    int size = Int32.Parse(tempS);
                    byte[] tempBA = new byte[size];
                    ns.Read(tempBA, 0, tempBA.Length);

                    MemoryStream tempMS = new MemoryStream(tempBA);
                    Bitmap tempBitmap = new Bitmap(tempMS);
                    MF.showData(tempBitmap);

                    Thread.Sleep(100);
                }
            }
        }
        catch(Exception e)
        {

        }
    }

    public void setLauf(bool tLauf)
    {
        lauf = tLauf;
    }
}
4

0 回答 0