public static void Service() { try { Socket soc = listener.AcceptSocket(); Console.WriteLine("Connected: {0}", soc.RemoteEndPoint); NetworkStream s = new NetworkStream(soc); StreamReader sr = new StreamReader(s); StreamWriter sw = new StreamWriter(s); sw.AutoFlush = true; while (true) { string command = sr.ReadLine(); if (command == "stop") { break; } if (command == "waiting" || command == null) { continue; } if (command == "publish") { var json = sr.ReadLine(); ArticleModel article = JsonConvert.DeserializeObject <ArticleModel>(json); articleRepository.PublishArticle(article.Author, article.Date, article.Title, article.Abstract, article.Body); sw.WriteLine("published"); } if (command == "login") { string token = sr.ReadLine(); UserModel user = JsonConvert.DeserializeObject <UserModel>(token); if (userLoginRepository.Authorize(user.Username, user.Psssword)) { sw.WriteLine(JsonConvert.SerializeObject(userLoginRepository.GetAuthor(user.Username))); } sw.WriteLine("false"); } if (command == "update") { var json = JsonConvert.SerializeObject(articleRepository.GetAllArticles().ToList()); sw.WriteLine(json); } if (command == "last") { DateTime received = JsonConvert.DeserializeObject <DateTime>(sr.ReadLine()); if (received.CompareTo(articleRepository.GetLatestArticleTime()) < 0) { sw.WriteLine("update"); } else { sw.WriteLine("wait"); } } } Console.WriteLine("Disconnected: {0}", soc.RemoteEndPoint); soc.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } }