Beispiel #1
0
        void writer_ClientDisconnected(object sender, EventArgs e)
        {
            MjpegWriter writer = (MjpegWriter)sender;

            try
            {
                Clients.Remove(writer.Sock);
                Client_Writers.Remove(writer);

                writer = null;
            }
            catch
            {
            }
        }
Beispiel #2
0
        /// <summary>
        /// Each client connection will be served by this thread.
        /// </summary>
        /// <param name="client"></param>
        private void ClientThread(object client)
        {
            Socket socket = (Socket)client;

            System.Diagnostics.Debug.WriteLine(string.Format("New client from {0}", socket.RemoteEndPoint.ToString()));



            lock (Clients)
                Clients.Add(socket);

            OnClientConnected(new ClientConnectedEventArgs(new ClientHandler()));

            MjpegWriter writer = new MjpegWriter(socket);

            lock (Client_Writers)
                Client_Writers.Add(writer);

            writer.ClientDisconnected += writer_ClientDisconnected;

            try
            {
                writer.WriteHeader();

                while (socket.Connected)
                {
                    byte[] buf = new byte[1024];
                    socket.Receive(buf);
                }
            }
            catch
            {
                try
                {
                    socket.Disconnect(false);
                }
                catch
                {
                }
            }
            finally
            {
                lock (Clients)
                    Clients.Remove(socket);

                OnClientDisonnected(new ClientDisonnectedEventArgs(new ClientHandler()));
            }
        }