
C# ПІДРУЧНИКИ / c# / MS Press - Msdn Training Programming Net Framework With C#
.pdf

Module 8: Delegates and Events |
33 |
|
|
|
|
Exercise 1
Creating a Simple Chat Server Using Delegates
In this exercise, you will implement a simple chat server and clients by using delegates.
Scenario
Each client will register a delegate with the server. Then, when a client sends a message to the server, the server forwards the message to all specified clients. This implementation uses delegates as a callback mechanism.
! Create a Visual Studio .NET project
•Open Visual Studio .NET and create a new C# Console Application project named chat1 in <install folder>\Labs\Lab08.
! Create the chat server class
1.Create the chat server class and name it DChatServer.
2.Declare within the DChatServer class a delegate that will be used to invoke the callback function in the client when a chat message arrives.
a.Name the delegate OnMsgArrived.
b.Declare this delegate public and reflect the callback function’s signature. The callback’s signature is a void return value with a single argument of type string.
3.Define a field to store the connected clients’ delegates by performing the following steps.
a.Name the field onMsgArrived.
b.Make the field a private static reference to the type of the delegate,
OnMsgArrived.
4.Define the method that will be called by a client to connect that client to the server by performing the following steps.
a.Name the method ClientConnect.
b.Declare the method public static void.
c.Include an argument of type OnMsgArrived.
d.Create the method’s implementation by using the delegate’s Combine method to add this new delegate to the delegate stored in the onMsgArrived invocation list.
As an alternative to the Combine method, you can use the += operator.
5.Define a method that will be called by a client to disconnect that client from the server by performing the following steps.
a.Name the method ClientDisconnect.
b.Declare the method public static void.
c.Declare the method so that it has the same signature as ClientConnect.
d.Create the method’s implementation by using the delegate’s Remove method to remove the delegate from the onMsgArrived invocation list.
As an alternative to the Remove method, you can use the -= operator.

34Module 8: Delegates and Events
6.Define a method that will send a specified message to the connected clients, with the possible exception of one client, by performing the following steps.
a.Name the method SendMsg.
b.Declare the method public static void.
c.Declare the method so that it takes two arguments: a string argument for the message and an object argument for the excluded client.
d.Create the method’s implementation.
i.If the excluded client argument is null, invoke the multicast delegate to send the message to all the clients.
ii.If the excluded client argument is not null, iterate through the onMsgArrived delegate’s invocation list and invoke only those delegates that do not match the excluded client argument.
!Create the chat client class
1.Create the chat client class and name it DChatClient.
2.Implement within the DChatClient class a callback method that the server will invoke by a delegate when the server receives a chat message.
a.Name the method onMsgArrived.
b.Declare the method private void.
c.Declare the method so that it takes a single string argument.
d.Create the method’s implementation so that it prints the name of the client along with the string argument to the console.
3.Add a constructor that will receive the name of the client as a parameter and connect the client with the server.
a.Declare the constructor so that it takes a string argument that represents the client’s name.
b.Create the constructor’s implementation.
i.Store the client’s name in a private field.
ii.Connect the client to the server by calling the server’s
ClientConnect method.
The call’s argument should be an instance of the delegate type DChatServer.OnMsgArrived that was instantiated with the client’s callback method: onMsgArrived.
Module 8: Delegates and Events |
35 |
|
|
|
|
! Create the program’s main entry point
1.Name the class that contains the program’s standard Main entry point,
Application.
Typically, a Visual Studio .NET project creates the class with the Main entry point for you. You may only need to rename this class.
2.Write to the console a line of text to indicate that the program is starting.
3.Instantiate three DChatClient objects, passing the client names “1”, “2”, and “3” to the constructor.
4.Send a message to the server by invoking the static method SendMsg of the DChatServer. The message should be sent to all clients.
5.Send a second message to all clients, except Client 2.
6.Write to the console a line of text to indicate that the program is finished.
! Compile and test the program
•Build the program and run it.
The output should resemble the following:
Demo start: Delegate Chat Server.
Msg arrived (Client 1): Hi to all clients
Msg arrived (Client 2): Hi to all clients
Msg arrived (Client 3): Hi to all clients
Msg arrived (Client 1): Hi to all clients except client 2
Msg arrived (Client 3): Hi to all clients except client 2
Demo stop: Delegate Chat Server.
Module 8: Delegates and Events |
37 |
|
|
|
|
! Create the event-based chat client class
1.Create a new chat client class and name it EChatClient.
2.Implement within the EChatClient class a callback method that the server will invoke by a delegate when the server receives a chat message.
a.Name the method onMsgArrived.
b.Declare the method private void.
c.Declare the method so that it takes a single string argument.
d.Implement the method so that it prints the name of the client, together with the string argument, to the console.
3.Add a constructor that will receive the name of the client as a parameter and connect the client with the server.
a.Declare the constructor so that it takes a string argument that represents the client’s name.
b.Create the constructor’s implementation.
i.Store the client’s name in a private field.
ii.Connect the client to the server by using the += operator to add a new delegate instance to the EChatServer’s event.
The += operator’s right operand should be an instance of the delegate type EChatServer.OnMsgArrived that was instantiated with the client’s callback method, onMsgArrived.
!Extend the program’s main entry point by performing the following procedures
1.Write to the console a line of text to indicate that the event chat portion of the program is starting.
2.Instantiate three EChatClient objects, passing the client names “1”, “2”, and “3” to the constructor.
3.Send a message to the server by invoking the static method SendMsg of the EchatServer. You should send the message to all clients.
4.Send a second message to all clients except client 2.
5.Write to the console a line of text to indicate that event chat is finished.
! Compile and test the program
•Build the program and run it.
The output should resemble the following:
...
Demo start: Event Chat Server.
Msg arrived (Client 1): Hi to all clients
Msg arrived (Client 2): Hi to all clients
Msg arrived (Client 3): Hi to all clients
Msg arrived (Client 1): Hi to all clients except client 2
Msg arrived (Client 3): Hi to all clients except client 2
Demo stop: Event Chat Server.
Module 8: Delegates and Events |
39 |
|
|
|
|
! Create the guidelines-based chat server class
1.Create a new chat server class and name it GChatServer.
2.Declare within the GChatServer class a delegate that will be used to invoke the function in the client when a chat message arrives.
a.Name the delegate MsgArrivedEventHandler.
b.This delegate should be declared public and reflect the guideline’s signature of a void return value and two arguments of type object and
MsgArrivedEventArgs.
3.Declare an event for the MsgArrivedEventHandler delegate type.
a.Name the event MsgArrivedHandler.
b.Make the event public. Do not make it static.
4.Define a method that will send a specified message to the connected clients, with the possible exception of one client.
a.Name the method SendMsg.
b.Declare the method public void.
c.Declare the method so that it takes two arguments: a string argument for the message and an object argument for the excluded client.
d.Create the method’s implementation.
i.Instantiate an instance of the event class MsgArrivedEventArgs with the message string.
ii.Invoke the OnMsgArrived method, which is implemented in the next step.
5.Define the OnEvent method to raise the event.
a.Name the method OnMsgArrived. In accordance with the .NET Framework guidelines declare the method protected virtual void with two arguments of type MsgArrivedEventArgs and object.
b.Create the method’s implementation.
i.If the event’s field contains a non-null delegate list and the excluded client argument is null, invoke the multicast delegate with the appropriate arguments.
ii.If the event’s field contains a non-null delegate list and the excluded client argument is not null, iterate through the MsgArrivedHandler delegate’s invocation list and invoke only those delegates that do not match the excluded client argument.
iii.If the event’s field contains a null delegate list, do nothing.

40Module 8: Delegates and Events
!Create the guidelines-based chat client class
1.Create a new chat client class and name it GChatClient.
2.Implement within the GChatClient class a callback method that the server will invoke by a delegate when the server receives a chat message.
a.Name the method onMsgArrived.
b.Declare the method private void.
c.Declare the method so that it takes an object and
MsgArrivedEventArgs arguments.
d.Create the method’s implementation so that it prints the name of the client, the Message property of MsgArrivedEventArgs, and the server object as a string to the console.
3.Add a constructor that will receive the name of the client and a reference to the server, and will connect the client with the server.
a.Declare the constructor so that it takes a string argument that represents the client’s name and a GChatServer argument that specifies the server object.
b.Create the constructor’s implementation.
i.Store the client’s name in a private field.
ii.Store the server’s reference in a private field.
iii.Connect the client to the server by using the += operator to add a new delegate instance to the GChatServer’s event.
The += operator’s right operand should be an instance of the delegate type GChatServer.MsgArrivedEventHandler that was instantiated with the client’s callback method: onMsgArrived.
!Extend the program’s main entry point by performing the following procedures
1.Create an instance of the GChatServer object.
2.Write to the console a line of text to indicate that the guidelines-based chat server class portion of the program is starting.
3.Instantiate three GChatClient objects, passing the client names “1”, “2”, and “3” to the constructor.
4.Send a message to the server by invoking the SendMsg method of GChatServer. The message should be sent to all clients.
5.Send a second message to all clients except client 2.
6.Write to the console a line of text to indicate that the guidelines-based event chat is finished.