Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                Student[] students =
                {
                    new Student("Bob",     "1"),
                    new Student("Richard", "2"),
                    new Student("Nick",    "3"),
                    new Student("Rob",     "10"),
                    new Student("Yan",     "11")
                };

                StudentsList list = new StudentsList(students);

                //StudentsList listOfStudents = new StudentsList();

                //foreach (var item in students)
                //{
                //    StudentsList.
                //        SetStudents
                //        (item);

                //}
                //Student.Students.AddRange(students);

                server = new TcpListener(IPAddress.Parse("127.0.0.1"), 5001);
                server.Start();

                Console.WriteLine("Waiting for connections...");

                while (true)
                {
                    TcpClient    client       = server.AcceptTcpClient();
                    ClientObject clientObject = new ClientObject(client);

                    Thread thread = new Thread(new ParameterizedThreadStart(clientObject.Process));
                    thread.Start(list);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Console.ReadKey();
                throw;
            }
            finally
            {
                if (server != null)
                {
                    server.Stop();
                }
            }
        }
Beispiel #2
0
        public void Process(object students)
        {
            studentsList = (StudentsList)students;
            NetworkStream stream = null;

            try
            {
                stream = Client.GetStream();
                while (true)
                {
                    byte[]        data       = new byte[256];
                    StringBuilder builder    = new StringBuilder();
                    int           dataLength = 0;
                    do
                    {
                        dataLength = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, dataLength));
                    }while (stream.DataAvailable);

                    string response = builder.ToString();

                    if (!ResponsePerform(response, stream))
                    {
                        Send(DateTime.Now.ToShortTimeString() + ": " + response, stream);
                        //Console.WriteLine(DateTime.Now.ToShortTimeString() + ": " + response);
                        //data = Encoding.Unicode.GetBytes(response);
                        //stream.Write(data, 0, data.Length);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                //throw;
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (Client != null)
                {
                    Client.Close();
                }
            }
        }