コード例 #1
0
 /// <summary>
 /// 开启服务
 /// </summary>
 private void Start()
 {
     try
     {
         String ip   = "0.0.0.0"; //监听本地
         ushort port = 6080;      //设置一个非主流的端口号,避免占用其他应用程序
         server.IpAddress = ip;
         server.Port      = port;
         // 写在这个位置是上面可能会异常
         SetAppState(AppState.Starting);
         // 启动服务
         if (server.Start())
         {
             SetAppState(AppState.Started);
             LogHelper.logRecord(string.Format("服务启动成功 -> {0}({1})", ip, port));
         }
         else
         {
             SetAppState(AppState.Stoped);
             LogHelper.logRecord(string.Format("服务启动失败 -> {0}({1})", server.ErrorMessage, server.ErrorCode));
         }
     }
     catch (Exception ex)
     {
         LogHelper.logRecord(ex.Message);
     }
 }
コード例 #2
0
ファイル: frmServer.cs プロジェクト: zyh329/HP-Socket
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                String ip   = this.txtIpAddress.Text.Trim();
                ushort port = ushort.Parse(this.txtPort.Text.Trim());

                // 写在这个位置是上面可能会异常
                SetAppState(AppState.Starting);
                server.IpAddress = ip;
                server.Port      = port;
                // 启动服务
                if (server.Start())
                {
                    this.Text = string.Format("{2} - ({0}:{1})", ip, port, title);
                    SetAppState(AppState.Started);
                    AddMsg(string.Format("$Server Start OK -> ({0}:{1})", ip, port));
                }
                else
                {
                    SetAppState(AppState.Stoped);
                    throw new Exception(string.Format("$Server Start Error -> {0}({1})", server.ErrorMessage, server.ErrorCode));
                }
            }
            catch (Exception ex)
            {
                AddMsg(ex.Message);
            }
        }
コード例 #3
0
        private void buttonStart_Click(object sender, EventArgs e)
        {
            try
            {
                string ip   = textBoxIPAdress.Text.Trim();
                ushort port = ushort.Parse(textBoxPort.Text.Trim());

                appState = AppState.Starting;

                server.IpAddress = ip;
                server.Port      = port;

                if (server.Start())
                {
                    appState = AppState.Started;
                    SetControlState();
                }
                else
                {
                    appState = AppState.Stoped;
                    SetControlState();
                    throw new Exception(string.Format("服务端启动失败:{0},{1}", server.ErrorMessage, server.ErrorCode));
                }
            }
            catch (Exception exc)
            {
                ShowMSG(exc.Message);
            }
        }
コード例 #4
0
        public bool Start(string IpAddress, ushort Port)
        {
            server.IpAddress = IpAddress;
            server.Port      = Port;

            try
            {
                if (server.Start())
                {
                    Console.WriteLine(string.Format("服务启动成功->({0}:{1})", server.IpAddress, server.Port));
                    return(true);
                }
                else
                {
                    throw new Exception(string.Format("服务启动失败->{0}({1})", server.ErrorMessage, server.ErrorCode));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("服务启动失败:" + ex.Message);
                return(false);
            }
        }