一段网络应用小程序如下:
    class Program
    {
        const int portNo = 500;
        static void Main(string[] args)
        {
            System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("127.0.0.1");
            TcpListener listener = new TcpListener(localAdd, portNo);
            listener.Start();
            while (true)
            {
                ChatClient user = new ChatClient(listener.AcceptTcpClient());
            }
        }
    }
我觉得奇怪,在while无限循环里,我理解的是user不是被重复命名了么?ChatClient user,重复命名。
那位高手帮忙给解释解释。

解决方案 »

  1.   

    我把完整代码贴出来:using System;
    using System.Collections.Generic;
    using System.Text;using System.Net.Sockets;namespace server_CS
    {
        class Program
        {
            const int portNo = 500;
            static void Main(string[] args)
            {
                System.Net.IPAddress localAdd = System.Net.IPAddress.Parse("127.0.0.1");
                TcpListener listener = new TcpListener(localAdd, portNo);
                listener.Start();
                while (true)
                {
                    ChatClient user = new ChatClient(listener.AcceptTcpClient());
                }
            }
        }
    }using System;
    using System.Collections.Generic;
    using System.Text;using System.Net.Sockets;
    using System.Collections;namespace server_CS
    {
        class ChatClient
        {
            public static Hashtable AllClients = 
                new Hashtable();
            private TcpClient _client;
            private string _clientIP;
            private string _ClientNick;
            private byte[] data;
            private bool ReceiveNick = true;        public ChatClient(TcpClient client)
            {
                _client = client;
                _clientIP = client.Client.RemoteEndPoint.ToString();
                AllClients.Add(_clientIP, this);
                
                data = new byte[_client.ReceiveBufferSize];
                _client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize), ReceiveMessage, null);           
            }        public void SendMessage(string message)
            {
                try
                {
                    System.Net.Sockets.NetworkStream ns;
                    lock (_client.GetStream())
                    {
                        ns = _client.GetStream();
                    }
                    byte[] bytesToSend = System.Text.Encoding.ASCII.GetBytes(message);
                    ns.Write(bytesToSend, 0, bytesToSend.Length);
                    ns.Flush();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.ToString());
                }
            }        public void ReceiveMessage(IAsyncResult ar)
            {
                int bytesRead;
                try
                {
                    lock (_client.GetStream())
                    {
                        bytesRead = _client.GetStream().EndRead(ar);
                    }
                    if (bytesRead < 1)
                    {
                        AllClients.Remove(_clientIP);
                        Broadcast(_ClientNick + " has left the chat.");
                        return;
                    }
                    else
                    {
                        string messageReceived = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
                        if (ReceiveNick)
                        {
                            _ClientNick = messageReceived;
                            Broadcast(_ClientNick + " has joined the chat.");
                            ReceiveNick = false;
                        }
                        else
                        {
                            Broadcast(_ClientNick + ">" + messageReceived);
                        }
                    }
                    lock (_client.GetStream())
                    {
                        _client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(_client.ReceiveBufferSize),ReceiveMessage, null);
                    }
                }
                catch (Exception ex)
                {
                    AllClients.Remove(_clientIP);
                    Broadcast(_ClientNick + " has left the chat.");
                }
            }
            public void Broadcast(string message)
            {
                Console.WriteLine(message);
                foreach (DictionaryEntry c in AllClients)
                {
                    ((ChatClient)(c.Value)).SendMessage(message + Environment.NewLine);
                }
            }
        }
    }
      

  2.   

                while (true)
                {
                    ChatClient user = new ChatClient(listener.AcceptTcpClient());
                }这应该不是重复命名,因为出了作用域就失效了。
      

  3.   

    没有重复,这个零时变量的作用域就是while{}里,用完就丢。所以没什么问题。
    你理解成1个类里2个函数都包含变量xxx,但他们不重复,不冲突。其实是一样的道理
      

  4.   

    名称只不过是一个名字,代号(ID)唯一就行了,
    每一个NEW的初始化都会生成新的对象
      

  5.   

                    ChatClient user = new ChatClient(listener.AcceptTcpClient());
    每次while后,user都是一个新的.必须要重新定义并赋值.