.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 object is really providing the remote service object, which is responsible for "broadcast" "broadcast" "broadcast" on the server push. 2004-12-29 Revision, so that the Server Side program is more robust! / * Csc.exe chatroom.cs / t: library chatroom.dll * /
Using system; using system.runtime.remoting;
// [Serializable] public class chat: MarshalByrefObject {// Defines 1 "Chatr OMEVENTHANDLER" and its parameter format Sign PUBLIC DELEGATE VOID CHATROOMEVENTHANDLER (String s);
// define the three "ChatRoomEventHandler delegate type" of events and remote callback public event ChatRoomEventHandler MessageReceive; // message received event public event ChatRoomEventHandler Login; // login event public event ChatRoomEventHandler Logoff; // exit event
// [system.Runtime.Remoting.Messaging.oneway] public void onMessageReceive (String message) {if (MessageReceive! = Null) {// Trigger the Receiver client MessageReceive event, broadcast all messages
Raiseevents (Ref MessageReceive, Ref Message); // MessageReceive (Message);} console.writeline ("Server:" message); // server message monitoring}
// [System.Runtime.Remoting.Messaging.oneway] public void online {string s = "system say:" user "login!"; If (login! = Null) {// triggers Receiver Customer End Login event, broadcast "login" message raiseeevents (Ref login, ref s); // login ("System Say:" User "login!");} Console.writeline ("Server:" s);}
// [system.Runtime.Remoting.Messaging.oneway] public void online {string s = "system say:" user "logoff!"; If (logoff! = Null) {// trigger Receiver customers End logoff event, broadcast "exit" message // logoff ("System Say:" "logoff!"); RaiseEvents (Ref logoff, ref s);} console.writeline ("Server: s);} public override object InitializeLifetimeService () {return null;} // see Ingo Rammer "Advanced .NET Remoting" public void RaiseEvents (ref ChatRoomEventHandler e, ref string Message) {ChatRoomEventHandler creh = null; int i = 1; Delegate [] D = E.GETINVOCATIONList (); Foreach (DELEGATE D IN D) {Try {CREH = (chatroomeventhandler) D; CREH (Message);} catch {message = " i.tostring () " subscriber error, The system cancels its event subscription! "; E - = creH;} i ;}}} 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) {transole.writeline ("Server ...., press enter key to exit."); console.readline () }}
The following is the configuration file (S.config):
Using system; using system.runtime.remoting; use system.runtime.remoting.channels; use means.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 a remote service object instance // RemotingConfiguration.configure (" c.config "); // x = new chatroom ();
// Get the remote service object instance programmatically 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, Console.Writeline ("Make a name the login please:"); user = console.readline (); // call this remote method, notify the server to trigger the Receiver client login event, broadcast "login" message x. Onlogin (User);
Console.WriteLine ("Welcome" User ", Send Your Message Please:");
String S; // Store Type Message Text
While ((s = console.readline ())! = "Q") // If you type q Exit loop {// 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); "bye bye" user);}}
4. Receiver (receiver.cs): Client Message Receiver object, responsible for receiving remote service objects from Sender and forwarding all messages of "Broadcast". / * Csc.exe /r: Chatroom.dll receiver.cs * /
Using system; using system.Runtime.remoting; using.Runtime.Remoting.Channels; use system.runtime.remoting.channels.http;
// Receiver object must inherit MarshalByrefObject, due to 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"); // is equivalent to binding X = new ChatRoom (); // "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 as 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
// Do not forget to cancel the commission at the end of the relationship x.MessageReceive - = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1); x.Login - = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1); x.Logoff - = new ChatRoom.ChatRoomEventHandler (this.Message_Receive1);} // This is a function of the server-side remote callback Public void message_receive1 (String s) {console.writeline (//); // Display received broadcast message locally}
Public Override Object InitializelifetimeService () {return null;}}
The following is the configuration file (c.config) of Sender and Receiver:
Compile:
CSC / T: library chatroom.cscsc /r:chatroom.dll server.cscsc /r:Chatroom.dll sender.cscsc /R :Chatroom.dll receiver.cs
Usage: Operation order:
1. First run the server side: server.exe2. Run the client: sender.exe is used to log in, send a message, exit Receiver.exe only with reception and display 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! Received "Enter" key to exit!