. Net remoting implements a simple "command line console" chat room

Source: Internet
Author: User

. Net remoting implements a simple console command line chat room

SetProgramIt consists of four main objects:

1. chatroom (chatroom. CS): The real remote service provider on the server side. It is responsible for broadcasting the message sent by sender by server push.
Revised to make the server side program more robust!
/*
Csc.exe chatroom. CS/T: Library chatroom. dll
*/

Using system;
Using system. runtime. remoting;

// [Serializable]
Public class chatroom: marshalbyrefobject
{
// Defines an event processing delegate named "chatroomeventhandler" and Its Parameter format signature.
Public Delegate void chatroomeventhandler (string S );

// Defines three "chatroomeventhandler Delegate types" events and remote callback Functions
Public event chatroomeventhandler messagereceive; // message receiving event
Public event chatroomeventhandler login; // logon event
Public event chatroomeventhandler logoff; // exit the event

// [System. runtime. remoting. messaging. oneway]
Public void onmessagereceive (string message)
{
If (messagereceive! = NULL)
{
// Trigger the messagereceive event of the consumer er client to broadcast all messages

Raiseevents (ref messagereceive, ref message );
// Messagereceive (Message );
}
Console. writeline ("server:" + message); // Server Message monitoring
}

// [System. runtime. remoting. messaging. oneway]
Public void onlogin (string user)
{
String S = "system say:" + User + "Login! ";
If (LOGIN! = NULL)
{
// Trigger the login event of the ER er client and broadcast the "login" Message
Raiseevents (ref login, ref S );
// Login ("system say:" + User + "Login! ");
}
Console. writeline ("server:" + S );
}

// [System. runtime. remoting. messaging. oneway]
Public void onlogoff (string user)
{
String S = "system say:" + User + "logoff! ";
If (logoff! = NULL)
{
// Trigger the logoff event of the consumer er client and broadcast the "quit" message.
// Logoff ("system say:" + User + "logoff! ");
Raiseevents (ref logoff, ref S );
}
Console. writeline ("server:" + S );
}
Public override object initializelifetimeservice ()
{
Return NULL;
}
// See 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)
{
Remotingconfiguration. Configure ("S. config ");
Console. writeline ("server..., press ENTER key to exit .");
Console. Readline ();
}
}

The following is the server configuration file (s. config ):
<Configuration>
<System. runtime. remoting>
<Application>
<Service>
<Wellknown mode = "Singleton" type = "chatroom, chatroom" objecturi = "chatroomurl"/>
</Service>
<Channels>
<Channel ref = "HTTP" Port = "8080">
<Serverproviders>
<Provider ref = "WSDL"/>
<Formatter ref = "Soap" typefilterlevel = "full"/>
<Formatter ref = "binary" typefilterlevel = "full"/>
</Serverproviders>
<Clientproviders>
<Formatter ref = "binary"/>
</Clientproviders>
</Channel>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

3. sender (sender. CS): a client message sender object that sends "broadcast" messages to remote service objects.
/*
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 .");
// Obtain the remote service object instance in configuration mode
// Remotingconfiguration. Configure ("C. config ");
// X = new chatroom ();

// Obtain 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 ");

// Log on first
Console. writeline ("make a name then login please :");
User = console. Readline ();

// Call this remote method to notify the server to trigger the login event of the login er client and broadcast the "login" message.
X. onlogin (User );

Console. writeline ("welcome" + User + ", send your message please :");

String s; // stores the text of the entered message

While (S = console. Readline ())! = "Q") // exit the loop if you type Q.
{
// Call this remote method to notify the server to trigger the messagereceive event of the consumer er client and broadcast the message you typed.
X. onmessagereceive (User + "say:" + S );
}

// Call this remote method to notify the server to trigger the logoff event of the Cycler client and broadcast the "quit" message.
X. onlogoff (User );
Console. writeline ("Bye bye" + User );
}
}

4. Receiver Er (receiver er. CS): the client Message Receiver object, which is responsible for "automatically" receiving all messages of remote service objects received and forwarded from the sender "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;

// The Explorer Object must inherit from the exploralbyrefobject object. As opposed to the server, the explorer object must also provide remote services for it.
Class extends Er: Implements albyrefobject
{
Chatroom X;

Public static void main ()
{
Receiver y = new receiver ();
Y. Run ();
}
 
Public void run ()
{
Remotingconfiguration. Configure ("C. config"); // equivalent to binding during later running
X = new chatroom ();
// Register the local callback function this. message_receive1 with the remote service object
X. messagereceive + = new chatroom. chatroomeventhandler (this. message_receive1 );
// Login logoff is the same as messagereceive Signature
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 to close the receiver

// Never forget to cancel the delegation at the end
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 the remote callback function of the server.
Public void message_receive1 (string S)
{
Console. writeline (s); // display the received broadcast message locally
}

Public override object initializelifetimeservice ()
{
Return NULL;
}
}

The following is the configuration file (C. config) of sender and consumer Er ):
<Configuration>
<System. runtime. remoting>
<Application>
<Client>
<Wellknown type = "chatroom, chatroom" url = "http: // server: 8080/chatroomurl"/>
</Client>
<Channels>
<Channel ref = "HTTP" Port = "0"/>
</Channels>
</Application>
</System. runtime. remoting>
</Configuration>

Compile:

CSC/T: Library chatroom. CS
CSC/R: chatroom. dll server. CS
CSC/R: chatroom. dll sender. CS
CSC/R: chatroom. dll receiver. CS

Usage:
Running sequence:

1. Run the server first:
Server.exe
2. Run the client again:
Sender.exe is used to log on, send messages, and exit.
Receiver.exe only uses and receives and displays messages

Note: Do not exit the program by closing the CLI Console window!

Server.exe: Press "enter" key to exit!

Sender.exe: Press "Q" key to exit!
Receiver.exe: Press "enter" key to exit!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.