NET Remoting implements simple console command line chat room This set of procedures consists of four main objects: 1. Chatroom (chatroom.cs): The server-side real-to-range service is responsible for the server to push (PUSH) "Broadcast" sent by Sender. / * Csc.exe ChatRoom.cs / t: library ChatRoom.dll * / using System; using System.Runtime.Remoting; [Serializable] public class ChatRoom: MarshalByRefObject {// define a called "the event processing request ChatRoomEventHandler "parameter format and its signature public delegate void ChatRoomEventHandler (string s); // defines three" ChatRoomEventHandler delegate type "of events and remote callback public event ChatRoomEventHandler MessageReceive; // message received event public event ChatRoomEventHandler Login; // Login events public event ChatRoomEventHandler Logoff; // exit public void OnMessageReceive (string message) {if (MessageReceive = null!) {// trigger Receiver client MessageReceive event, broadcast all messages MessageReceive (message);} Console.WriteLine ( "Server : " Message); // Server Message Monitor} Public VOID ONLOGIN (String User) {if (login! = Null) {// Trigger the Receiver client login event, broadcast" login "message login (" System Say: " User "login!");} Console.writeline ("Server:" user "login!");} Public void online {if (logoff! = Null) {// trigger Receiver client logoff Event, broadcast "exit" message logoff ("System Say:" US ER "logoff!");} console.writeline ("Server:" user "logoff!");} public override object initializelifetimeService () {return null;}} 2. Server: server. The host program of the remote service object.
/ * Csc.exe server.cs * / using system; using system.runtime.remoting; class server {public static void main (string [] args) {transoTingConfiguration.configure ("s.config"); console.writeline Server ...., press enter key to exit. "); Console.readline ();}} The following is the configuration file (s.config):
/ * Csc.exe /r:ChatRoom.dll Sender.cs * / using System; using System.Timers; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http Class sender {chatroom x; public static void main (String [] args) {sender y = new sender (); y.Run ();} String User; Public void Run () {Console.writeline ("Messages Sender. .., Press 'q' to exit chatting. "); // Get remote service object instance // RemotingConfiguration.configure (" c.config "); // x = new chatroom (); // Programming way to get the remote service object instance System.Runtime.Remoting.Channels.ChannelServices.RegisterChannel (new System.Runtime.Remoting.Channels.Http.HttpChannel ()); x = (ChatRoom) System.Activator.GetObject (typeof (ChatRoom), "http:// server: 8080 / chatroomurl"); // first log in to console.writeline ("Make a name the login please:"); user = console.readline (); // call this remote method, notify the server trigger Receiver client login event, broadcast "login" message x.onlogin (user); "Welcome" user ", send your message please:"); string s; // Stores typed message while ( (s = console .Readline ())! = "Q") // If you type Q to exit cycle {// call this remote method, notify the server to trigger the Receiver client MessageReceive event, broadcast the message you typed X.onMessageRecept (user "SAY: " S);} // Call this remote method, notify the server to trigger the Receiver client logoff event, broadcast" exit "message x.onlogoff (user); console.writeLine (" Bye Bye " user);}} 4. Receiver (receiver.cs): The client message receiver object is responsible for receiving remote service objects from Sender and forwards all messages of "Broadcast". / * Csc.exe /r:ChatRoom.dll Receiver.cs * / using System; using System.Runtime.Remoting; using System.Runtime.Remoting.Channels; using System.Runtime.Remoting.Channels.Http; // Receiver objects MarshalByrefObject must be inherited, because of the server, the Receiver object also provides remote service.
Class Receiver: MarshalByrefObject {Chatroom x; public static void main () {receiver y = new receiver (); y.Run ();} public void run () {transkingconfiguration.configure ("c.config"); // quite binding x = new ChatRoom () in the late running; // "registered" to the remote service object in the local callback this.Message_Receive1 x.MessageReceive = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1); // Login Logoff and MessageReceive signature Like x.Login = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1); x.Logoff = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1); Console.WriteLine ( "Messages Receiver ..., Press Enter key to exit.") Console.readLine (); // Exit Close Receiver // Don't Cancel the Delegate X.MessageReceive - = New Chatroom.ChatroomeventHandler (this) = new chatroom.chatroomeVentrandler (this .Message_Receive1); x.Logoff - = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1);} // this is the callback function of the remote server public void Message_Receive1 (string s) {Console.WriteLine (s); // local Display received broadcast message} public override object initiali ZelifetimeService () {return null;}} The following is the configuration file (c.config) of Sender and Receiver:
Compilation: CSC / T: library chatom.cscsc /r:chatroom.dll server.cscsc /R :Chatroom.dll sender.cscsc /r:Chatroom.dll receiver.cs usage: Run sequence: 1. First run the server: Server Run the client: sender.exe Used to log in, send a message, exiting the receiver.exe only with reception and displaying message Note: Do not exit the program by closing the command line console window! Server.exe: press "Enter" Key to EXIT! Sender.exe: Press "Q" key to exit! receiver.exe: Press "Enter" key to exit! Author Blog:
http://blog.9cbs.net/web_gus/
related articles
NET Remoting Realistic "Command Line Console" chat room Selection from Playyuer blog show () And showdialog () Linux under C language programming - Basics Linux system command masteric guide (primary) CommandText statement basic usage, number and ""