⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chatserverimpl.cs

📁 一个聊天程序
💻 CS
字号:
namespace RemotingChat
{
    using System;
	using System.Runtime.Remoting;
	using System.Runtime.Remoting.Channels.TCP;
	using System.Collections;
	using System.Threading;
    /// <summary>
    ///    Summary description for ChatServerImpl.
    /// </summary>
	public 	class ChatServerImpl : MarshalByRefObject,ChatServer 
	{
		private ObjectList  chatters = new ObjectList();
		public ChatServerImpl () : base() 
		{
		   Console.WriteLine("Initializing Server...");
		}

		public static void Main(String[] args){

		   try {
		      //Start the Remoting Server
				TCPChannel chan = new TCPChannel(8085);
				ChannelServices.RegisterChannel(chan);
				RemotingServices.RegisterWellKnownType("RemotingChat", "RemotingChat.ChatServerImpl", "SayHello",WellKnownObjectMode.Singleton);
				Console.WriteLine("Server Ready");
				System.Console.WriteLine("Hit <enter> to exit...");
				System.Console.ReadLine();
		   } catch (Exception e)
		   {
				Console.WriteLine("This name is already bound: " + e);
		   } 
		   
		}

		public void register(Chat c, String name) {
			Monitor.Enter(this);
			chatters.Add(new Talker(c,name));
			Monitor.Exit(this);
		}
		public  String[] listChatters() {
		      String[] list = new String[chatters.Count];
		      Talker c;
		      for(int i=0; i< list.Length; i++){
			   c=(Talker)chatters[i];
			   list[i] = c.getChatterName();
		      }
		      return list;
		}

		public void postMessage(Message m)
		{
			//Console.WriteLine("Hi I am here in PostMessage");
			try
			{
				Monitor.Enter(this);
				Talker t;
				for(int i=0; i < chatters.Count ; i++)
				{
				   t = (Talker)chatters[i];
				   if(!t.addMessage(m))
				     chatters.RemoveAt(i--);
				}
				Monitor.Exit(this);
			}
			catch(Exception e)
			{
				Console.WriteLine(e);
				throw e;
			}
		}
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -