chatserverimpl.cs

来自「一个聊天程序」· CS 代码 · 共 75 行

CS
75
字号
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 + =
减小字号Ctrl + -
显示快捷键?