class serverClass
    {
        private Thread t1;
        TcpListener tcpListen;
        private int port = 8900;
        private string sIP = "127.0.0.1";
        bool nstate;
        public serverClass()
        {
            IPEndPoint iep = new IPEndPoint(IPAddress.Parse(sIP), port);
            tcpListen = new TcpListener(iep);
        }
        public void beginListen()
        {
            //开始监听
            nstate = true;
            Thread t = new Thread(new ThreadStart(listen));
            t.Start();
        }        private void listen()
        {            tcpListen.Start(10);
            while (nstate)
            {
                if (!tcpListen.Server.Poll(500, SelectMode.SelectRead))
                {
                    continue;
                }
                else
                {
                    TcpClient TC1 = tcpListen.AcceptTcpClient();
                    Thread t1 = new Thread(delegate() { receive(TC1); });
                    receive(TC1);
                    t1.Start();
                }
            }
            tcpListen.Stop();
        }        private void receive(TcpClient TC)
        {
            NetworkStream ns;
            ns = TC.GetStream();
            try
            {
                byte[] byteMessage = new byte[8];
                int rect = ns.Read(byteMessage, 0, byteMessage.Length);
                byte[] chat = new byte[rect];
                Buffer.BlockCopy(byteMessage, 0, chat, 0, rect);
                string msg = System.Text.Encoding.Default.GetString(chat);                //s生成一个随机数并将其发送到客户端
                Random r = new Random();
                int r1 = r.Next(100);
                byte[] rByte = new byte[8];
                rByte = System.Text.Encoding.Default.GetBytes(r1.ToString());
                ns.Write(rByte, 0, rByte.Length);
                //将收到的数据在数据库中查询
                seekdata(msg, ns, r1);
            }
            catch (Exception ex)
            {            }
        }
        public void seekdata(string msg, NetworkStream ns, int random)    //在服务器端的数据库中查找接收到的用户和密码
        {
            string accessString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =11.mdb";
            OleDbConnection conn = new OleDbConnection(accessString);
            conn.Open();
            string strSQL = "select userkey from usbkey where userID = '" + msg + "'";
            OleDbCommand cmd = new OleDbCommand(strSQL, conn);
            byte[] userKey = System.Text.Encoding.Default.GetBytes((string)cmd.ExecuteScalar());
            if (userKey == null)
            {
                byte[] randomKey = new byte[64];
                ns.Read(randomKey, 0, 64);
                ns.Write(System.Text.Encoding.Default.GetBytes("No"), 0, "no".Length);
            }
            else
            {
                //对随机数进行加密
                HMACMD5 hmac1 = new HMACMD5();
                byte[] chat1 = new byte[64];
                hmac1.Key = userKey;
                chat1 = hmac1.ComputeHash(System.Text.Encoding.Default.GetBytes(random.ToString()));                //接收客户端发送来加密的随机数
                byte[] randomKey = new byte[64];
                int i = ns.Read(randomKey, 0, randomKey.Length);
                byte[] chat2 = new byte[i];
                Buffer.BlockCopy(randomKey, 0, chat2, 0, i);
                if (PasswordEquals(chat1, chat2))
                {
                    ns.Write(System.Text.Encoding.Default.GetBytes("yes"), 0, "yes".Length);
                }
                else
                {
                    ns.Write(System.Text.Encoding.Default.GetBytes("No"), 0, "no".Length);
                }
            }
        }        //对两个byte[]进行比较
        private bool PasswordEquals(byte[] b1, byte[] b2)
        {
            if (b1.Length != b2.Length) return false;
            if (b1 == null || b2 == null) return false;
            for (int i = 0; i < b1.Length; i++)
                if (b1[i] != b2[i])
                    return false;
            return true;
        }        //设置循环变量为假,几让服务器停止监听
        public void serverStop()
        {
            nstate = false;
        }
    }
现在我知道像我这么来一个连接创建一个线程是不行的
 我该怎么改
1