public void ReceiveCallBack(IAsyncResult ar)
 {
     UdpClient udpClient = null;
     UdpState udpState = null;
     try
     {
         udpState = (UdpState)ar.AsyncState;
         udpClient = udpState.udpclient;
         byte[] data = udpClient.EndReceive(ar, ref udpState.remoteIpEndPoint);
         string strdata = Encoding.ASCII.GetString(data);
         string[] arrdata = strdata.Split(':');
         if (arrdata.Length == 4)
         {
             //if (arrdata[0] == "hi")//!!!!!!!!!!!!!!!!!!!!!!!!!!!
             {
                 int port = int.Parse(arrdata[1]);
                 if (arrdata[3] == "0")
                 {
                     //del
                     if (lstServer.ContainsKey(port))
                     {
                         //stop server only!
                         lstServer[port].Stop();
                         lstServer.Remove(port);
                         Console.WriteLine(String.Format("server {0} stopping...", port));
                     }
                 }
                 if (arrdata[3] == "1")
                 {
                     //new
                     if (lstServer.ContainsKey(port))
                     {
                         if (lstServer[port].config.password != arrdata[2])
                         {
                             //Restarted
                             Console.WriteLine(String.Format("server {0} already run but passwd changed", port));
                             lstServer[port].Stop();
                             lstServer.Remove(port);
                             Console.WriteLine(String.Format("server {0} stopping...", port));
                             Config config = this.config.Copy();
                             config.server_port = port;
                             config.password = arrdata[2];
                             lstServer[port] = new Server(config);
                             lstServer[port].Start();
                             Console.WriteLine(String.Format("server {0} starting...", port));
                         }
                         else
                         {
                             Console.WriteLine(String.Format("server {0} already run", port));
                         }
                     }
                     else
                     {
                         Config config = this.config.Copy();
                         config.server_port = port;
                         config.password = arrdata[2];
                         lstServer[port] = new Server(config);
                         lstServer[port].Start();
                         Console.WriteLine(String.Format("server {0} starting...", port));
                     }
                 }
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.ToString());
     }
     finally
     {
         try
         {
             udpClient.BeginReceive(ReceiveCallBack, udpState);
         }
         catch (Exception e)
         {
             Console.WriteLine(e.ToString());
         }
     }
 }
 public Handler(Server server)
 {
     this.server = server;
 }
 public void Close()
 {
     lock (this)
     {
         if (closed)
         {
             return;
         }
         closed = true;
     }
     if (connection != null)
     {
         try
         {
             Console.WriteLine("close:" + connection.RemoteEndPoint.ToString());
             connection.Shutdown(SocketShutdown.Both);
             connection.Close();
             connection = null;
         }
         catch (SocketException)
         {
             //
         }
     }
     if (remote != null)
     {
         try
         {
             remote.Shutdown(SocketShutdown.Both);
             remote.Close();
             remote = null;
         }
         catch (SocketException)
         {
             //
         }
     }
     lock (this)
     {
         if (encryptor != null)
         {
             encryptor.Dispose();
             encryptor = null;
         }
     }
     config = null;
     remoteBuffer = null;
     connetionBuffer = null;
     destAddr = null;
     timeOutTimer = null;
     server = null;
 }
        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Text = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
            try
            {
                Config config = Config.Load();
                Config.Save(config);
                this.config = config;
                SetConfigLab(config);
                try
                {
                    Encryptor encryptor = new Encryptor(config.method, config.password);
                    encryptor.Dispose();
                }
                catch(Exception)
                {
                    MessageBox.Show("Open SSL library init failed!");
                    return;
                }

                if (config.multiuser_pylisten == true)
                {
                    this.controlserver = new ControlServer(config);
                    controlserver.Start();
                }
                else
                {
                    server = new Server(config);
                    server.Start();
                }
                thReleaseMem = new Thread(new ThreadStart(ReleaseMemory));
                thReleaseMem.Start();
                thReleaseMem.IsBackground = true;

                this.Hide();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }