如何将一个字典的值添加到另一个字典

时间:2019-01-10 19:52:09

标签: c# visual-studio dictionary

我需要你的帮助。我正在编写聊天服务器应用程序,但服务器端出现问题。为了注册客户,我创建了词典static readonly Dictionary <int, TcpClient> list_clients = new Dictionary <int, TcpClient> ();以使用广播消息,而对于私人消息,我创建了一个与第一个词典相等的新词典。我只是不知道如何添加到第二本字典。我尝试了一种简单的方法privateClients.Add (1, list_clients [1]);,但是它不起作用,您能帮我吗?我把服务器代码留给你 PS:我不太有经验

namespace Server {
public partial class Form1 : Form
{
    TcpListener server;
    static readonly object _lock = new object();
    static readonly Dictionary<int, TcpClient> list_clients = new Dictionary<int, TcpClient>();
    Dictionary<int, TcpClient> privateClients;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        int count = 1;
        server = new TcpListener(IPAddress.Any, 33000);
        server.Start();
        button1.Enabled = false;
        listBox1.Items.Add("Server inizializzato, ora in ascolto...");
        Thread t = new Thread(() => Oh(count));
        t.Start();
    }

    private void Oh(int count)
    {
        while (true)
        {
            DateTime thisDay = DateTime.Now;
            TcpClient client = server.AcceptTcpClient();
            lock (_lock) list_clients.Add(count, client);
            privateClients = new Dictionary<int, TcpClient>();
            lock (_lock) privateClients.Add(1, list_clients[1]);
            listBox1.Invoke(new MethodInvoker(delegate
            {
                listBox1.Items.Add("Client " + count + " connesso in data " + thisDay.ToString());
            }));


            Thread t = new Thread(handle_clients);
            t.Start(count);
            count++;
        }
    }

    private void handle_clients(object o)
    {
        int id = (int)o;
        TcpClient client;


        lock (_lock) client = list_clients[id];
        NetworkStream stream = client.GetStream();
        try
        {
            string data = "Il tuo id e' " + id + "\n Inserisci il tuo nome: ";
            byte[] b = Encoding.ASCII.GetBytes(data);
            stream.Write(b, 0, b.Length);
            byte[] bytes = new byte[1024];
            int a = stream.Read(bytes, 0, bytes.Length);
            string nome = Encoding.ASCII.GetString(bytes, 0, a);
            textBox1.Invoke(new MethodInvoker(delegate
            {
                if (textBox1.Text.Contains(nome))
                {
                    bool azz = true;
                    while (azz == true)
                    {
                        data = "Mi dispiace il nome e' gia' stato scelto, scegline uno nuovo";
                        b = Encoding.ASCII.GetBytes(data);
                        stream.Write(b, 0, b.Length);
                        a = stream.Read(bytes, 0, bytes.Length);
                        nome = Encoding.ASCII.GetString(bytes, 0, a);
                        if (textBox1.Text.Contains(nome) == false)
                        {
                            azz = false;
                        }
                    }
                }
                textBox1.Text += Environment.NewLine + id + "             " + nome;
            }));
            data = "Benvenuto nella chat! Ora puoi chattare";
            b = Encoding.ASCII.GetBytes(data);
            stream.Write(b, 0, b.Length);
            while (true)
            {
                int byte_count = stream.Read(bytes, 0, bytes.Length);
                data = nome + ": " + Encoding.ASCII.GetString(bytes, 0, byte_count);
                string da = Encoding.ASCII.GetString(bytes, 0, byte_count);
                if (da == "CIAOo")
                {
                    privateClients.Add(2, list_clients[2]);
                    bool ciao = true;
                    while (ciao == true)
                    {
                        privateChat(data);
                    }
                } else
                {
                    broadcast(data);
                }

            }
        }
        catch
        {
            DateTime thisDay = DateTime.Now;
            listBox1.Invoke(new MethodInvoker(delegate
            {
                listBox1.Items.Add("Il Client " + id + " disconnesso in data " + thisDay.ToString());
            }));
            lock (_lock) list_clients.Remove(id);
            client.Client.Shutdown(SocketShutdown.Both);
            client.Close();
        }
    }

    private void broadcast(string data)
    {
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        lock (_lock)
        {
            foreach (TcpClient c in list_clients.Values)
            {
                NetworkStream stream = c.GetStream();
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    }

    private void privateChat(string data)
    {
        byte[] buffer = Encoding.ASCII.GetBytes(data);

        lock (_lock)
        {
            foreach (TcpClient c in privateClients.Values)
            {
                NetworkStream stream = c.GetStream();
                stream.Write(buffer, 0, buffer.Length);
            }
        }
    }
}

这是它不起作用的地方

while (true)
        {
            int byte_count = stream.Read(bytes, 0, bytes.Length);
            data = nome + ": " + Encoding.ASCII.GetString(bytes, 0, byte_count);
            string da = Encoding.ASCII.GetString(bytes, 0, byte_count);
            if (da == "CIAOo")
            {
                privateClients.Add(2, list_clients[2]);
                bool ciao = true;
                while (ciao == true)
                {
                    privateChat(data);
                }
            } else
            {
                broadcast(data);
            }

        }

在此部分中应检查是否客户端发送消息“ CIAOo”应启动privateChat函数但未启动,这会跳过它并继续进行广播,因此我认为将客户端添加到第二本字典

0 个答案:

没有答案
相关问题