多线程

今天被多线程整晕了。
起初设想好的给server端加上多线程,实现多个用户的连接,可是没搞清楚到底怎么用。

后来终于找到个示例,稍加修改才改成的。

首先监听服务端的端口,有连接后,进行登录判定,结束后开个线程去监视客户端输入的信息,加以判断即可。


class CenterServer

    {

        private static IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 350);
        //private Socket serverSocket = new Socket(localEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        private TcpListener listener;
        private Socket clientsocket;
        private Thread clientservice;

        static void Main()
        {
            CenterServer demo = new CenterServer();
            demo.StartListening();
        }

        private void StartListening()
        {
            listener = new TcpListener(localEndPoint);
            listener.Start();
            while (true)
            {
                try
                {
                    Socket s = listener.AcceptSocket();
                    clientsocket = s;

                    byte[] reciveInfo = new byte[1024];
                    clientsocket.Receive(reciveInfo);
                    char[] charInfo = new char[1024];
                    charInfo = System.Text.Encoding.Default.GetChars(reciveInfo);
                    string msgResult = string.Empty;

                    msgResult = Encoding.Default.GetString(reciveInfo, 2, reciveInfo.Length - 4);
                    Console.WriteLine(msgResult);

                    System.Threading.Thread.Sleep(10);

                    //发送1
                    byte[] feedback1 = {0x00,0x14,0x01,0x00,0x03,0x00,0x00,0x00,0x39,0xD1,
                                            0x0C,0x00,0x00,0x8C,0x8C,0x29,0x00,0x00,0xA9,0xA9};
                    clientsocket.SendTo(feedback1, localEndPoint);

                    //接收
                    System.Threading.Thread.Sleep(10);
                    clientsocket.Receive(reciveInfo);
                    System.Threading.Thread.Sleep(10);

                    //发送2
                    byte[] feedback2 = new byte[6];
                    feedback2[0] = 0x01; // 0x01,0xFD,0x00,0x06};
                    feedback2[1] = 0xFD;
                    feedback2[2] = 0x00;
                    feedback2[3] = 0x06;
                    feedback2[4] = 0x00;
                    feedback2[5] = 0x00;

                    clientsocket.SendTo(feedback2, localEndPoint);

                    //接收
                    //System.Threading.Thread.Sleep(10);
                    //clientsocket.Receive(reciveInfo);
                    //msgResult = Encoding.Default.GetString(reciveInfo, 2, reciveInfo.Length - 4);
                    //Console.WriteLine(msgResult);

                    System.Threading.Thread.Sleep(10);

                    //发送3
                    feedback2[5] = 0x0C;            //发送和2的一样,就第5个字节上面是0x0C
                    clientsocket.SendTo(feedback2, localEndPoint);

                    //接收
                    //System.Threading.Thread.Sleep(10);
                    //clientsocket.Receive(reciveInfo);
                    //msgResult = Encoding.Default.GetString(reciveInfo, 2, reciveInfo.Length - 4);
                    //Console.WriteLine(msgResult);

                    System.Threading.Thread.Sleep(10);

                    //发送4
                    feedback2[5] = 0x29;            //还是和2一样,第5个人字节上面是0x29
                    clientsocket.SendTo(feedback2, localEndPoint);

                    //似乎有返回包
                    System.Threading.Thread.Sleep(10);
                    clientsocket.Receive(reciveInfo);
                    msgResult = Encoding.Default.GetString(reciveInfo, 2, reciveInfo.Length - 4);
                    Console.WriteLine(msgResult);

                    System.Threading.Thread.Sleep(1000);

                    byte[] feedback3 = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
                    clientsocket.SendTo(feedback3, localEndPoint);

                    System.Threading.Thread.Sleep(1000);
                    byte[] haha = {  };
                    clientsocket.SendTo(haha, localEndPoint);

                    //clientsocket = s;
                    clientservice = new Thread(new ThreadStart(ServerClient));
                    clientservice.Start();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }

        private void ServerClient()
        {
            Socket client = clientsocket;
            bool keepalive = true;
            while (keepalive)
            {
                TcpClient client = new TcpClient();
                string msgResult = string.Empty;
                byte[] back = new byte[1024];
                client.Receive(back);
               
                SocketUtil abc = new SocketUtil();

                msgResult = Encoding.Default.GetString(back, 0, back.Length).Substring(19, 12);
                //根据相应的数据,返回信息  if (msgResult == "abcdefg")
                {
                    Thread.Sleep(1000);
                    byte[] login = {};
                    client.SendTo(login, localEndPoint);
                }
                else if (msgResult.Contains("hello"))
                {
                    Thread.Sleep(1000);
                    byte[] hello = {};
                    client.SendTo(hello, localEndPoint);
                }
                else if (msgResult.Contains("avh"))
                {
                    Thread.Sleep(1000);
                    byte[] avh = {};
                    client.SendTo(avh, localEndPoint);
                }
                else if (msgResult.Trim() == "")
                    keepalive = false;
            }
            if (!keepalive)
            {
                client.Close();
            }
        }
    }这边是不是写得不太好。。。再看看。

posted on 2009-10-23 22:25  delphi2007  阅读(376)  评论(0编辑  收藏  举报