示例#1
0
        //this is the infint lissen loop
        void receveMessages(TcpClient client)
        {
            NetworkStream stream = client.GetStream();
            packetReader  reader = new packetReader();

            while (true)
            {
                Console.WriteLine(" in the receve loop");
                Byte[] buffer = new Byte[256];
                try
                {
                    stream.Read(buffer, 0, buffer.Length);
                    Console.WriteLine("the user id is " + reader.ReadUserID(buffer));
                    comandInterpriter(reader.readCMD(buffer), buffer, client);
                    Console.WriteLine(reader.ReadMessage(buffer, reader.readHeader(buffer)));
                    if (reader.readCMD(buffer) == 3)
                    {
                        Thread.CurrentThread.Join();
                    }
                } catch (Exception e)
                {
                    Console.WriteLine("this is throwing an exceion " + e);
                }
            }
        }
示例#2
0
        void startSetion(Byte[] pac, TcpClient client)
        {
            int AssiningNum = 0;

            Console.WriteLine("start setion comand called");
            packetReader  reader  = new packetReader();
            packetCreator creator = new packetCreator();

            if (NumberToAssine == 0)
            {
                userCount  += 1;
                AssiningNum = userCount;
            }
            else
            {
                AssiningNum    = NumberToAssine;
                NumberToAssine = 0;
            }

            users.Add(AssiningNum, reader.ReadMessage(pac, reader.readHeader(pac)));
            userClient.Add(AssiningNum, client);
            foreach (KeyValuePair <int, String> kvp in users)
            {
                Console.WriteLine("key = {0}, value ={1}", kvp.Key, kvp.Value);
            }
            foreach (KeyValuePair <int, TcpClient> kvp in userClient)
            {
                //writes the client ID and IPaddress
                Console.WriteLine("key = {0}, value ={1}", kvp.Key, ((IPEndPoint)kvp.Value.Client.RemoteEndPoint).Address.ToString());
            }
            Byte[]        newUserPac = creator.createPacet(AssiningNum, 0, users[AssiningNum]);
            NetworkStream stream     = client.GetStream();

            stream.Write(newUserPac, 0, newUserPac.Length);
            stream.Flush();
            Send(newUserPac);
            foreach (KeyValuePair <int, string> kvp in users)
            {
                if (kvp.Key != userCount)
                {
                    newUserPac = creator.createPacet(kvp.Key, 0, kvp.Value);
                    stream.Write(newUserPac);
                    stream.Flush();
                }
            }
        }