示例#1
0
        //is used to receive msgs sent by the server
        public void Receiver()
        {
            this.tcpListener = new TcpListener(IPAddress.Any, receivePort);

            //this loop will keep on listening
            while (true)
            {
                // Always use a Sleep call in a while(true) loop
                // to avoid locking up your CPU. --Shanika
                //Thread.Sleep(10);
                tcpListener.Start();
                TcpClient client = this.tcpListener.AcceptTcpClient();
                Stream streamReceiver = client.GetStream();
                Byte[] bytes = new Byte[256];

                int i;
                String data = null;

                while ((i = streamReceiver.Read(bytes, 0, bytes.Length)) != 0)
                {

                    //receive msg sent by server
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                }
                //write to console- for testing purposes
                Console.WriteLine(data);
                msg = data;
                //creates a MsgObject object and stores the respectivr values
                MsgObject msgObject = new MsgObject(msg, DateTime.Now);
                msgParser.addMsg(msgObject);

                streamReceiver.Close();
                tcpListener.Stop();
                client.Close();

            }
        }
示例#2
0
文件: MsgParser.cs 项目: N-2-L/Tanks
 /*********setter for msgQueue Queue *********/
 public void addMsg(MsgObject msgObject)
 {
     this.msgQueue.Enqueue(msgObject);
 }