首页 新闻 会员 周边 捐助

c#做简单的qq服务器时如何将hashtable中的在线用户取出来?

0
悬赏园豆:5 [已关闭问题]

具体点就是我不很不理解hashtable的具体内容,所以也就不会用了下面是我自己写的东东,写到这里就不会了,

 public partial class Form1 : Form
    {
        Hashtable onLineUsers;
        TcpListener tcpListener;
        TcpClient tcpClient;
        Database db;
        public Form1()
        {
            InitializeComponent();
            tcpListener = new TcpListener(2000);
            onLineUsers = new Hashtable();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Thread listenThread = new Thread(new ThreadStart(startListen));
            listenThread.Start();
        }
        private void startListen()
        {
            tcpListener.Start();
            while (true)
            {
                tcpClient = tcpListener.AcceptTcpClient();
                Thread serviceThread = new Thread(new ParameterizedThreadStart(service));
                serviceThread.Start(tcpClient);
                //service(tcpClient);
            }
        }
        private void service(object client)
        {
            //针对client,对它进行服务(为client提供所有
            //它应该具有的能力)
            TcpClient tcpc=client as TcpClient;
            NetworkStream ns = tcpc.GetStream();
            while (true)
            {
                byte[] mss = new byte[1024];
                string ms="";
                ns.Read(mss, 0, tcpc.Available);
                ms += Encoding.UTF8.GetString(mss);
                string[] messages = ms.Split(new char[] { '^' });
              
                switch (messages[0])//chat^zjl^huzhuxi^asdfasd
                {
                    case "chat":
                        NetworkStream nsClient=(NetworkStream)onLineUsers[messages[2]];
                        byte[] sendms=Encoding.UTF8.GetBytes(messages[1]+"说:"+messages[3]);
                        nsClient.Write(sendms, 0, sendms.Length);
                        break;//在这里执行转发
                    case "login":
                        onLineUsers.Add(messages[1], ns);
                        break;//在这里执行登陆
                    case "register":
                       
                            bool exist;
                            DataTable dt;
                            dt = new DataTable();
                             db=new Database();
                             dt=db.sel("select * from user where username='" +messages[1].ToString()+ "'");
                             if (dt.Rows.Count > 0)

                                 exist = true;
                             else
                             {
                                 db.not_sel("insert into user values('"+messages[1].ToString()+"','"+messages[2].ToString()+"','"+messages[3].ToString() +"','"+messages[4].ToString()+"')");
                                 exist = false;
                             }
                             byte[] sendms1 = Encoding.UTF8.GetBytes(exist.ToString());
                             NetworkStream nets= tcpClient.GetStream();
                             nets.Write(sendms1,0,sendms1.Length);
                             break;
                    case"sxym":
                        IDictionaryEnumerator myEnumerator = onLineUsers.GetEnumerator();
                        while (myEnumerator.MoveNext())
                        {
                          就是在这里我想把hashtable表里面存放的在线用户的姓名都取出来然后发送给客户端通过刷性在线用户按钮来显示在线的用户
                        }

高媛媛的主页 高媛媛 | 初学一级 | 园豆:195
提问于:2009-04-22 18:41
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册