日期:2014-05-20  浏览次数:21093 次

关于GetHashCode用法
需要做一个能转发在线用户列表的聊天服务器 以下各句代码的具体作用是什么呢?2001表示登陆成功。还望各位给我指点下,能否在重点代码上加个注释?? 
C# code
foreach (ClientService item in clietn)
                                {
                                    if (item.mysock.GetHashCode() == model.TcpClientHashCode)
                                    {
                                        item.SendMessage(loginState);                                        
                                        if (loginState == "2001")
                                        {
                                            foreach (OnlineModel onLineItem in onlineList)
                                            {
                                                if (onLineItem.TcpClientHashCode != model.TcpClientHashCode)
                                                {
                                                    string userId = onLineItem.UserId.ToString();
                                                    while (true)
                                                    {
                                                        if (userId.Length == 4)
                                                        {
                                                            break;
                                                        }
                                                        userId = userId.Insert(0, "0");
                                                    }
                                                    string userName = onLineItem.UserName;
                                                    int len = userName.Length;
                                                    while (true)
                                                    {
                                                        if (userName.Length == len)
                                                        {
                                                            break;
                                                        }
                                                        //userName = userName.Insert(len, " ");
                                                        userName = userName.PadRight(len);
                                                    }
                                                    string gender = onLineItem.Gender.ToString();
                                                    string message = userId + userName + gender + "0";
                                                    string length = message.Length.ToString();
                                                    while (true)
                                                    {
                                                        if (length.Length == 4)
                                                        {
                                                            break;
                                                        }
                                                        length = length.Insert(0, "0");
                                                    }
                                                    item.SendMessage(length + message);
                                                }
                                            }
                                        }
                                        break;
                                    }
                                }
                                #endregion

                                #region 通知其他用户上线信息
                               
                                if (loginState == "2001")//通知其他用户
                                {
                                    foreach (OnlineModel item in onlineList)
                                    {
                                        foreach (ClientService clientItem in clietn)
                                        {
                                            if (item.TcpClientHashCode == clientItem.mysock.GetHashCode() && item.TcpClientHashCode != loginUse.TcpClientHashCode)
                                            {
                                                string userId = loginUse.UserId.ToString();
                                                while (true)
                                                {
                                                    if (userId.Length == 4)
                                                    {
                                                        break;
                                                    }
                                                    userId = userId.Insert(0, "0");
                                                }
                                                string userName = loginUse.UserName;
                                                int len = userName.Length;
                                                while (true)
                                                {
                                                    if (userName.Length == len)
                                                    {
                                                        break;
                                                    }
                                                    //userName = userName.Insert(len, " ");
                                                    userName = userName.PadRight(len);
                                                }
                                                string gender = loginUse.Gender.ToString();
                                                string message = userId + userName + gender + "0";
                                                string length = message.Length.ToString();
                                                while (true)
                                                {
                                                    if (length.Length == 4)
                                                    {
                                                        break;
                                                    }
                                                    length = length.Insert(0, "0");
                                                }
                                                clientItem.SendMessage(length + message);
                                            }
                                        }
                                    }
                                }
                                #endregion