C#委托服务器客户端示例

时间:2016-02-23 08:33:43

标签: c# delegates

我一直在尝试用C#代替我的代表,但我似乎没有明白如何使用它们。

我有一个程序应该与代表一起执行以下操作:

Main.cs:

  • 创建1台服务器;
  • 创建3个客户;
  • 将客户注册到服务器;
  • 从服务器向所有注册客户端发送消息;
  • 取消注册1个客户端并从服务器向注册客户端发送消息;
  • 从服务器向特定客户端发送消息。

Server.cs:

  • public delegate MsgSend(string message);
  • private MsgSend msgSend;
  • public void Register(MsgSend m);
  • public void Unregister(MsgSend m);
  • public void SendMessage(string message);

Clien.cs:

  • private string id;
  • public Client(string id); - 显式构造函数;
  • public void ClientRegister(Server server);
  • public void ClientUnregister(Server server);
  • public void PrintMessage(string message);

我应该如何在这种情况下使用委托......?

任何帮助解释这个程序中发生的事情的方式和地点对我来说都很有用。

提前致谢。

以下是代码:

服务器:

public delegate string MsgSend(string message);

class Server
{
    private MsgSend msgSend;

    public void Register(MsgSend m)
    { // Is this the right way to use the delegate as multicast delegate
      // to register the client and say it is registered or 
      // I should do extremely different thing here?
        msgSend += new MsgSend(m);
        m("xx");
    }

    public void Unregister(MsgSend m)
    { // Is this the right way to use the delegate as multicast delegate
      // to register the client and say it is registered or 
      // I should do extremely different thing here?
        msgSend += new MsgSend(m);
        m("yy");
    }

    public void SendMessage(string message)
    {
        Console.WriteLine(message + this.msgSend);
    }
}

客户端:

class Client
{
    public string id;

    public Client(string id)
    {
        this.id = id;
    }

    public void ClientRegister(Server server)
    {
        server.Register(...); // What should I pass as parameter here and why...
    }

    public void ClientUnregister(Server server)
    {
        server.Unregister(...); // What should I pass as parameter here and why...
    }

    public void PrintMessage(string message)
    {
        Console.WriteLine(id + " recieved: " + message);
    }
}

主:

class Program
{
    static void Main(string[] args)
    { // Is this enough in the main program so the program to work?
        Server svr = new Server();

        Client cl1 = new Client("123");
        Client cl2 = new Client("456");
        Client cl3 = new Client("789");

        cl1.ClientRegister(svr);
        cl2.ClientRegister(svr);
        cl3.ClientRegister(svr);

        svr.SendMessage("message from server");

    }
}

1 个答案:

答案 0 :(得分:2)

以下是完整的工作代码:

主要

using System;

static partial class Program
{
    static void Main()
    {
        Server svr = new Server();

        Client cl1 = new Client("123");
        Client cl2 = new Client("456");
        Client cl3 = new Client("789");

        // Register
        cl1.ClientRegister(svr);
        cl2.ClientRegister(svr);
        cl3.ClientRegister(svr);

        svr.SendMessage("message from server");

        // Unregister    
        cl1.ClientUnregister(svr);
        cl2.ClientUnregister(svr);
        cl3.ClientUnregister(svr);

        // Try send string when you unregistered all clients
        svr.SendMessage("message from server again");
        Console.ReadLine();
    }
}

服务器

class Server
{
    public delegate void MsgSend(string message);

    private MsgSend msgSend;

    public void Register(MsgSend m)
    {
        msgSend += m;
        m("Registered");
    }

    public void Unregister(MsgSend m)
    {
        msgSend -= m; // <--- Change += to -=
        m("Unregistered");
    }

    public void SendMessage(string message)
    {
        //You have to check if msgSend is null:
        if (msgSend != null) this.msgSend(message);
        else Console.WriteLine("DONT HAVE ANY CLIENT!");
    }
}

<强>客户端

class Client
{
    public string id;

    public Client(string id)
    {
        this.id = id;
    }

    public void ClientRegister(Server server)
    {
        server.Register(PrintMessage);
    }

    public void ClientUnregister(Server server)
    {
        server.Unregister(PrintMessage); 
    }

    public void PrintMessage(string message)
    {
        Console.WriteLine("Client " + id + " recieved: " + message);
    }
}

运行Main()时,您会收到:

Client 123 recieved: Registered
Client 456 recieved: Registered
Client 789 recieved: Registered
Client 123 recieved: message from server
Client 456 recieved: message from server
Client 789 recieved: message from server
Client 123 recieved: Unregistered
Client 456 recieved: Unregistered
Client 789 recieved: Unregistered
DONT HAVE ANY CLIENT!