Пример #1
0
        private void button1_Click(object sender, EventArgs e)
        {
            this.button1.SetPropertry <bool>("Enabled", false);
            this.button2.SetPropertry <bool>("Enabled", true);
            string        serverHost   = this.txtServerHost.Text.Trim();
            int           serverPort   = this.txtServerPort.Text.ToInt32();
            int           remotePort   = this.txtRemotePort.Text.ToInt32();
            string        localHost    = this.txtLocalHost.Text.Trim();
            int           localPort    = this.txtLocalPort.Text.ToInt32();
            ProtocolTypes protocolType = ProtocolTypes.TCP;

            if (cboIsHTTP.Checked)
            {
                protocolType = ProtocolTypes.HTTP;
            }
            try
            {
                service = new ConnectionServerService()
                {
                    ServerHost   = serverHost,
                    ServerPort   = serverPort,
                    RemotePort   = remotePort,
                    LocalHost    = localHost,
                    LocalPort    = localPort,
                    ProtocolType = protocolType,
                };
                service.ExitServiceHandle += Service_ExitServiceHandle;
                service.Start();

                //反写配置信息
                string val = string.Format("ServerHost={0};ServerPort={1};RemotePort={2}; LocalHost={3};LocalPort={4};ProtocolType={5}"
                                           , serverHost, serverPort, remotePort, localHost, localPort, (int)protocolType);
                Program.SaveConfig(new KeyValuePair <string, string>("item0", val));
            }
            catch (Exception ex)
            {
                this.button1.SetPropertry <bool>("Enabled", true);
                this.button2.SetPropertry <bool>("Enabled", false);
                Console.WriteLine(ex.Message);
            }
        }
Пример #2
0
        private static void ConsoleStart()
        {
            //ServerHost=test.evfort.cn;ServerPort=9201;RemotePort=9203;LocalHost=127.0.0.1;LocalPort=5005;
            List <ConnectionServerService> services = new List <ConnectionServerService>();

            for (int i = 0; i < 100; i++)
            {
                string key  = string.Format("item{0}", i);
                string temp = System.Configuration.ConfigurationManager.AppSettings.Get("item" + i.ToString());
                if (string.IsNullOrEmpty(temp))
                {
                    break;
                }
                try
                {
                    //ServerHost = 远程服务器地址;
                    //ServerPort = 服务器通讯端口;
                    //RemotePort = 外网映射端口;
                    //LocalHost = 局域网主机地址;
                    //LocalPort = 局域网主机端口;
                    string        ServerHost   = "";
                    int           ServerPort   = 0;
                    int           RemotePort   = 0;
                    string        LocalHost    = "";
                    int           LocalPort    = 0;
                    ProtocolTypes ProtocolType = ProtocolTypes.TCP;
                    temp.ToLower().Split(';').ToList().ForEach((str) =>
                    {
                        string[] items = str.Split('=');
                        if (items.Length > 1)
                        {
                            string k = items[0];
                            string v = items[1];
                            switch (k)
                            {
                            case "serverhost":
                                ServerHost = v;
                                break;

                            case "serverport":
                                ServerPort = v.ToInt32();
                                break;

                            case "remoteport":
                                RemotePort = v.ToInt32();
                                break;

                            case "localhost":
                                LocalHost = v;
                                break;

                            case "localport":
                                LocalPort = v.ToInt32();
                                break;

                            case "protocoltype":
                                if ("2".Equals(v))
                                {
                                    ProtocolType = ProtocolTypes.HTTP;
                                }
                                break;

                            default:
                                break;
                            }
                        }
                    });
                    ConnectionServerService service = new ConnectionServerService()
                    {
                        ServerHost   = ServerHost,
                        ServerPort   = ServerPort,
                        RemotePort   = RemotePort,
                        LocalHost    = LocalHost,
                        LocalPort    = LocalPort,
                        ProtocolType = ProtocolType
                    };
                    services.Add(service);
                    service.Start();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("启动失败,请检查配置信息:{0}", temp);
                }
            }
            Console.WriteLine("服务启动成功,按任意键结束!");
            Console.ReadLine();
            services.ForEach((service) =>
            {
                service.Stop();
            });
        }