/// <summary> /// 停止服务 /// </summary> private void StopServer() { if (server != null) { server.SessionStart -= new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionStart); server.SessionStop -= new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionStop); server.SessionConnect -= new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionConnect); server.ClientPushOpened -= new Smobiler.Core.ClientPushOpenedEventHandler(MobileGlobal.OnPushCallBack); server.StopServer(); MobileGlobal.OnServerStop(server); } }
/// <summary> /// 开始服务 /// </summary> private void StartServer() { try { //服务起始界面 server.StartUpForm = typeof(zxcApp.MyStudy.frmSmobiler_Main); //服务TCP端口,默认为2323 this.txtTcpPort.Text = server.Setting.TcpServerPort.ToString(); //服务HTTP端口,默认为2324 this.txtHTTPPort.Text = server.Setting.HttpServerPort.ToString(); //绑定事件 server.SessionStart += new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionStart); server.SessionStop += new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionStop); server.SessionConnect += new Smobiler.Core.SmobilerSessionEventHandler(MobileGlobal.OnSessionConnect); server.ClientPushOpened += new Smobiler.Core.ClientPushOpenedEventHandler(MobileGlobal.OnPushCallBack); //启用服务 server.StartServer(); MobileGlobal.OnServerStart(server); } catch (System.Net.HttpListenerException ex) { //遇到HTTP监听无法添加的异常时,需要注册HTTP监听,一般原因是由于当前用户没有管理员权限 //运行下面的代码,会提示用户账户控制(网络命令外壳),这时请点击是 //如果出现拒绝访问并且没有提示用户账户控制,请在 控制面板\安全性与维护\更改用户账户控制设置 选择 仅当应用尝试更改我的计算机时通知我(默认) ProcessStartInfo psi = new ProcessStartInfo("netsh", "http delete urlacl url=http://+:" + server.Setting.HttpServerPort.ToString() + "/"); psi.Verb = "runas"; psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = true; Process.Start(psi).WaitForExit(); psi = new ProcessStartInfo("netsh", "http add urlacl url=http://+:" + server.Setting.HttpServerPort.ToString() + "/ user="******"\\" + Environment.UserName); psi.Verb = "runas"; psi.CreateNoWindow = true; psi.WindowStyle = ProcessWindowStyle.Hidden; psi.UseShellExecute = true; Process.Start(psi).WaitForExit(); server.StartServer(); } }