private void button_Close_Click(object sender, EventArgs e)
 {
     if (tcpServerEngine == null)
     {
         return;
     }
     this.tcpServerEngine.Stop();
     this.ShowListenStatus();
     this.textBox_port.ReadOnly = false;
     this.textBox_port.SelectAll();
     this.textBox_port.Focus();
     this.tcpServerEngine.Dispose();
     this.tcpServerEngine            = null;
     this.button_StartListen.Enabled = true;
 }
Ejemplo n.º 2
0
        private void button_StartListen_Click(object sender, EventArgs e)
        {
            try
            {
                //方法一、采用当前应用程序中的【App.config】文件。
                //var bootstrap = BootstrapFactory.CreateBootstrap();

                //=>方法二、采用自定义独立【SuperSocket.config】配置文件
                var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config");
                if (!bootstrap.Initialize())
                {
                    ShowMessage("Failed to initialize!");
                    return;
                }
                StartResult startResult = bootstrap.Start();
                if (startResult == StartResult.Success)
                {
                    this.ShowMessage("服务启动成功!");
                    tcpServerEngine = bootstrap.AppServers.Cast <MyAppServer>().FirstOrDefault();
                    if (tcpServerEngine != null)
                    {
                        tcpServerEngine.NewSessionConnected += tcpServerEngine_NewSessionConnected;

                        /* 同时你要移除请求处理方法的注册,因为它和命令不能同时被支持:
                         * http://docs.supersocket.net/v1-6/zh-CN/A-Telnet-Example
                         * 如果你的服务器端包含有很多复杂的业务逻辑,这样的switch/case代码将会很长而且非常难看,并且没有遵循OOD的原则。
                         * 在这种情况下,SuperSocket提供了一个让你在多个独立的类中处理各自不同的请求的命令框架。
                         */
                        //tcpServerEngine.NewRequestReceived += tcpServerEngine_NewRequestReceived;
                        ECHO.ECHOMessageRecevied      += ECHO_ECHOMessageRecevied;
                        tcpServerEngine.SessionClosed += tcpServerEngine_SessionClosed;
                        this.ShowListenStatus();
                    }
                    else
                    {
                        this.ShowMessage("请检查配置文件中是否又可用的服务信息!");
                    }
                }
                else
                {
                    this.ShowMessage("服务启动失败!");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }
        private void button_StartListen_Click(object sender, EventArgs e)
        {
            try
            {
                //方法一、采用当前应用程序中的【App.config】文件。
                //var bootstrap = BootstrapFactory.CreateBootstrap();

                //=>方法二、采用自定义独立【SuperSocket.config】配置文件
                var bootstrap = BootstrapFactory.CreateBootstrapFromConfigFile("SuperSocket.config");
                if (!bootstrap.Initialize())
                {
                    ShowMessage("Failed to initialize!");
                    return;
                }
                StartResult startResult = bootstrap.Start();
                if (startResult == StartResult.Success)
                {
                    this.ShowMessage("服务启动成功!");
                    tcpServerEngine = bootstrap.AppServers.Cast <MyAppServer>().FirstOrDefault();
                    if (tcpServerEngine != null)
                    {
                        tcpServerEngine.NewSessionConnected += tcpServerEngine_NewSessionConnected;
                        tcpServerEngine.NewRequestReceived  += tcpServerEngine_NewRequestReceived;
                        tcpServerEngine.SessionClosed       += tcpServerEngine_SessionClosed;
                        this.ShowListenStatus();
                    }
                    else
                    {
                        this.ShowMessage("请检查配置文件中是否有可用的服务信息!");
                    }
                }
                else
                {
                    this.ShowMessage("服务启动失败!");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }