public JobManagerFixture() { BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider(); serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; System.Collections.IDictionary ClientTcpChannelProperties = new System.Collections.Hashtable(); ClientTcpChannelProperties["name"] = "ClientTcpChan"; ChannelServices.RegisterChannel( new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(ClientTcpChannelProperties, new BinaryClientFormatterSinkProvider()), false); int port = 35010; System.Collections.IDictionary TcpChannelProperties = new System.Collections.Hashtable(); TcpChannelProperties["port"] = port; try { chan = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(TcpChannelProperties, serverProv); ChannelServices.RegisterChannel(chan, false); server = new JobServerImpl(); serverRef = RemotingServices.Marshal(server, "JobServer"); server.handlersAdded.Set(); } catch (SocketException e) { if (!e.Message.StartsWith("Only one usage of each socket address")) { throw; } } }
private void button1_Click(object sender, System.EventArgs e) { try { if(button1.Text == "Begin Listening") { Msg("Creating new channel..."); channel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(13075); System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(channel); Msg("Connecting remoting object to channel"); System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType( typeof(OysterDataBaseService.RemoteFunctions), "ODS_Functions", System.Runtime.Remoting.WellKnownObjectMode.Singleton ); button1.Text = "Stop Listening"; Msg("Listening for incoming request.."); } else { Msg("Disconnecting remoting object from channel"); System.Runtime.Remoting.Channels.ChannelServices.UnregisterChannel(channel); Msg("Unregistering channel.."); button1.Text = "Begin Listening"; Msg("Server no longer listening for request"); } } catch(Exception Err) { Msg("ERROR--" + Err.Message); } }
protected override void OnShown(EventArgs e) { base.OnShown(e); if (this.SingleInstanceProgram) { if (CloseIfNotMainInstance()) { return; } } try { lastInputLanguage = Application.CurrentInputLanguage; InputLanguage persianLang = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("fa-IR")); if (persianLang == null) { persianLang = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("fa")); } if (persianLang != null) { Application.CurrentInputLanguage = persianLang; } } catch { } if (this.DesignMode) { return; } if (Options.SettingInitializer == null) { throw new Exception("تنظیمات برنامه پیاده سازی نشده است"); } Options.SettingInitializer.GetProgramSetting().CloseSplashScreen(); if (Options.SettingInitializer != null) { if (!Options.SettingInitializer.GetUserSetting().Login()) { Options.SettingInitializer.GetProgramSetting().ShowExitDialog = false; this.Close(); return; } } UpdateDatabases(); DateTime serverDateTime = Options.SettingInitializer.GetDataAccess().Connection.GetServerDateTime(); TimeSpan timeDifference = serverDateTime.Subtract(DateTime.Now); do { if (timeDifference.TotalMinutes >= Options.SettingInitializer.GetProgramSetting().MinuteDifferenceWithServerToShowMessage || timeDifference.TotalMinutes <= -Options.SettingInitializer.GetProgramSetting().MinuteDifferenceWithServerToShowMessage) { MessageBox.VDialogResult dResult = PersianMessageBox.Show(this, string.Format("این سیستم {0} با سرور اختلاف ساعت دارد\r\nساعت سرور: {1}", timeDifference.GetText(), serverDateTime.ToLongTimeString()), "اختلاف ساعت", new MessageBox.VDialogButton[] { new MessageBox.VDialogButton(MessageBox.VDialogResult.OK, "تایید"), new MessageBox.VDialogButton("تغییر تاریخ / ساعت", ChangeSystemDate, true), new MessageBox.VDialogButton(MessageBox.VDialogResult.Cancel, "خروج از برنامه") }, MessageBox.VDialogIcon.Warning, MessageBox.VDialogDefaultButton.Button1, System.Windows.Forms.RightToLeft.Yes, false, null, null, null, null, null); if (dResult == MessageBox.VDialogResult.Cancel) { Options.SettingInitializer.GetProgramSetting().ShowExitDialog = false; this.Close(); return; } } Njit.Sql.DataAccess da = new Sql.DataAccess(Options.SettingInitializer.GetSqlSetting().DatabaseConnection.ConnectionString); serverDateTime = da.Connection.GetServerDateTime(); timeDifference = serverDateTime.Subtract(DateTime.Now); da.Dispose(); }while (timeDifference.TotalHours >= Options.SettingInitializer.GetProgramSetting().AllowableHourDifferenceWithServer || timeDifference.TotalHours <= -Options.SettingInitializer.GetProgramSetting().AllowableHourDifferenceWithServer); if (this.RegisterSystemUtilityServer) { int registeredChannels = 0; Exception exception = null; try { System.Runtime.Remoting.Channels.Tcp.TcpChannel _TcpChannel = new System.Runtime.Remoting.Channels.Tcp.TcpChannel(Options.SettingInitializer.GetProgramSetting().NetworkPort); System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_TcpChannel, false); registeredChannels++; } catch (Exception ex) { exception = ex; } try { System.Collections.IDictionary properties = new System.Collections.Hashtable(); properties["name"] = "tcp6"; properties["port"] = Options.SettingInitializer.GetProgramSetting().NetworkPort; properties["bindTo"] = "[::]"; System.Runtime.Remoting.Channels.Tcp.TcpServerChannel _TcpChannelV6 = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(properties, null); System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel(_TcpChannelV6, false); registeredChannels++; } catch (Exception ex) { exception = ex; } if (registeredChannels > 0) { try { System.Runtime.Remoting.RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off; System.Runtime.Remoting.RemotingConfiguration.RegisterWellKnownServiceType(typeof(Njit.Common.SystemUtility), Options.SettingInitializer.GetProgramSetting().NetworkName, System.Runtime.Remoting.WellKnownObjectMode.Singleton); } catch (Exception ex) { PersianMessageBox.Show(this, "خطا در راه اندازی سرور" + "\r\n\r\n" + ex.Message); } } else { PersianMessageBox.Show(this, "خطا در راه اندازی سرور" + "\r\n\r\n" + exception.Message); } } try { Cursor = Cursors.WaitCursor; Njit.Common.SystemUtility systemUtility = Options.SystemUtility; systemUtility.SendMessage(System.Net.Dns.GetHostName(), "--HelloWorld"); } catch (Exception ex) { PersianMessageBox.Show(this, "خطا در اتصال به سرور" + "\r\n" + "اتصال به نرم افزار بر روی سرور با خطا مواجه شده است" + "\r\n" + "توجه داشته باشید که این نرم افزار باید بر روی سرور اجرا شده باشد و یک کاربر وارد سیستم شده باشد" + "\r\n\r\n" + ex.Message); } finally { Cursor = Cursors.Default; } }
static void Main(string[] args) { var server = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(9009); }
static void Main(string[] argv) { try { Dictionary<string, string> settings = ParseArguments(argv); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); BinaryServerFormatterSinkProvider serverProv = new BinaryServerFormatterSinkProvider(); serverProv.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full; System.Collections.IDictionary ClientTcpChannelProperties = new System.Collections.Hashtable(); ClientTcpChannelProperties["name"] = "ClientTcpChan"; ChannelServices.RegisterChannel( new System.Runtime.Remoting.Channels.Tcp.TcpClientChannel(ClientTcpChannelProperties, new BinaryClientFormatterSinkProvider()), false); //ChannelServices.RegisterChannel( // new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(35010), false); //RemotingConfiguration.RegisterWellKnownServiceType(typeof(JobServer), // "JobServer", WellKnownObjectMode.Singleton); foreach (var item in ChannelServices.RegisteredChannels) { string name = item.ChannelName; } int port = 35010; // preferred string strPort = string.Empty; if (settings.TryGetValue("-P", out strPort)) { int.TryParse(strPort, out port); } port = GetPort(port); if (port == -1) { // there is no free tcp port } System.Collections.IDictionary TcpChannelProperties = new Dictionary<string, object>(); TcpChannelProperties["port"] = port; TcpChannelProperties["bindTo"] = System.Net.IPAddress.Loopback.ToString(); var tcpServerChannel = new System.Runtime.Remoting.Channels.Tcp.TcpServerChannel(TcpChannelProperties, serverProv); ChannelServices.RegisterChannel(tcpServerChannel, false); JobServerImpl server = new JobServerImpl(); RemotingServices.Marshal(server, "JobServer"); JobManager manager = new JobManager(server, settings); Console.Out.WriteLine("JobManager has started"); manager.Text = "JobManager (Port:" + port.ToString() + ")"; Application.Run(manager); ChannelServices.UnregisterChannel(tcpServerChannel); } catch (Exception ex) { Console.Out.WriteLine("JobManager failed to start. Exception: {0}", ex); MessageBox.Show( ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error); } }