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. / * Csc.exe chatroom.cs / t: library chatroom.dll * /
Using system; using system.runtime.remoting;
[Serializable] public class chat: MarshalByrefObject {// Defines 1 "chatroomeenthandler" 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
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 online {if (login! = Null) {// triggered the Receiver client login event, broadcast "login" message login ("System Say:" User "Login!");} Console .Writeline ("Server:" User "Login!");} Public void online {if (logoff! = Null) {// triggered the Receiver client logoff event, broadcast "exit" message logoff (" System Say: " User " 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) {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!