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

本文关键字:客户端 服务器 | 更新日期: 2023-09-27 18:35:44

我一直在尝试了解 C# 中的委托,但我似乎不明白如何使用它们。

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

主.cs:

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

服务器.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");
    }
}

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

这是完整的工作代码:

主要

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!