代码之家  ›  专栏  ›  技术社区  ›  Jamie Keeling

内存管理和异常处理

  •  1
  • Jamie Keeling  · 技术社区  · 14 年前

    使多个用户一次与服务器通信,每个新的客户端连接在单独的线程上建立。

    在这个类中,我创建两个流作为客户端的入站和出站流。我先创建字段,然后在一个单独的方法中初始化对象,这仅仅是因为该对象在其他几个地方使用。

    这意味着我有一个相当烦人的方法,只用于关闭底层连接,而不是更多。

    我希望我的解释是正确的,我会发一个片段给你看。

    谢谢!

    //Fields
            TcpClient tcpClient;
    
            //The thread that will send information to the client
            private Thread thrSender;
            private StreamReader srReceiver;
            private StreamWriter swSender;
            private string currentUser;
            private string strResponse;
    
            //The constructor of the class takes in a TCP connection
            public Connection(TcpClient tcpCon)
            {
                tcpClient = tcpCon;
    
                //The thread that accepts the client and waits messages
                thrSender = new Thread(AcceptClient);
    
                //The thread calls the AcceptClient method
                thrSender.Start();
            }
    
            private void CloseConnection()
            {
                //Close the currently open objects
                tcpClient.Close();
                srReceiver.Close();
                swSender.Close();
            }
    
            //Occurs when a new client is accepted
            private void AcceptClient()
            {
                srReceiver = new StreamReader(tcpClient.GetStream());
                swSender = new StreamWriter(tcpClient.GetStream());
    
                //Read account information from the client
                currentUser = srReceiver.ReadLine();
    
                //Examine response from client
                if (currentUser != "")
                {
                    //Store the user name in the hash table
                    if (ChatServer.htUsers.Contains(currentUser) == true)
                    {
                        //0 means not connected - Writes error to Client and Server log
                        swSender.WriteLine("0|This username already exists.");
                        swSender.Flush();
                        CloseConnection();
                        return;
                    }
                    //More if/else if/else statements
                    //...  
    
            }
    
        }
    
    1 回复  |  直到 14 年前
        1
  •  1
  •   Steve    14 年前

    您可以在AcceptClient方法中相当容易地处理这两个流,方法是将它们设置为局部变量,因为它们在其他地方没有被引用,如下所示:

    private void AcceptClient()
    {
        using (StreamReader srReceiver = new StreamReader(tcpClient.GetStream()))
        {
            using (StreamWriter swSender = new StreamWriter(tcpClient.GetStream()))
            {
                // ...
            }
        }
    }
    

    private void AcceptClient()
    {
        try
        {
            using (StreamReader srReceiver = new StreamReader(tcpClient.GetStream()))
            {
                using (StreamWriter swSender = new StreamWriter(tcpClient.GetStream()))
                {
                    // ...
                }
            }
        }
        finally
        {
            tcpClient.Dispose();
        }
    }
    

    无论try子句是否抛出异常,都将调用finally子句。