示例#1
0
        public void Run()
        {
            IPAddress address = ((IPEndPoint)client.Client.RemoteEndPoint).Address;

            if (server.getListBox().Items.Contains(address))
            {
                client.GetStream().WriteByte((byte)'x');
                client.GetStream().Close();
                client.Close();
                return;
            }
            server.getListBox().Items.Add(address);
            server.triggerRecover(address);

            MySQLServer.Insert("Windows: client from [ip:" + address + "] booted at [" + DateTime.Now + "]."); // 原子函数

            try
            {
                ns = client.GetStream();
                server.getStream[address] = ns;
                const int MAX_LEN = 1 << 20;
                byte[]    jpg     = new byte[MAX_LEN], head = new byte[4];
                while (true)
                {
                    int c = ns.Read(head, 0, 4);
                    if (c == 0)
                    {
                        server.getFrame.ContainsKey(address);
                        FormMonitor fm = server.getFrame[address];
                        server.getFrame.Remove(address);
                        fm.Close();
                        break;
                    }
                    else
                    {
                        int offset, rcv = -1;
                        for (offset = c; rcv != 0 && offset != 4; offset += rcv)
                        {
                            rcv = ns.Read(head, offset, 4 - offset);
                        }
                        int length = (head[3] << 24) | (head[2] << 16) | (head[1] << 8) | head[0];

                        for (offset = 0; rcv != 0 && offset != length; offset += rcv)
                        {
                            rcv = ns.Read(jpg, offset, length - offset);
                        }
                        if (server.getFrame.ContainsKey(address))
                        {
                            FormMonitor  frame = server.getFrame[address];
                            MemoryStream ms    = new MemoryStream();
                            ms.Write(jpg, 0, length);
                            Image image = Image.FromStream(ms);
                            frame.setImage(image);
                            Thread.Sleep(40);
                            ns.WriteByte(frame.target);
                        }
                    }
                }
            }
            catch
            {
            }
            MySQLServer.Insert("Windows: client from [ip:" + address + "] exited at [" + DateTime.Now + "]."); // 原子函数
            server.getStream.Remove(address);
            server.getListBox().Items.Remove(address);
        }