private static void RispostaServer(Messaggio msg, TipoStato stato, TipoStato statoSuccessivo, StreamWriter sw) { if (msg != null && statoSuccessivo != stato) { Console.WriteLine(msg.GetType().ToString()); sw.WriteLine("{0} +OK", statoSuccessivo.ToString()); } else { Console.WriteLine("Messaggio sconosciuto"); sw.WriteLine("{0} -ERR", stato.ToString()); } }
static void Comunica(object param) { TcpClient client = (TcpClient)param; Console.WriteLine("Sto avviando la comunicazione"); NetworkStream nsRead = client.GetStream(); NetworkStream nsWrite = client.GetStream(); StreamReader sr = new StreamReader(nsRead); StreamWriter sw = new StreamWriter(nsWrite); sw.AutoFlush = true; try { while (true) { // lettura richiesta dal client string str = sr.ReadLine(); Console.WriteLine(str); // Elaborazione e invio risposta Messaggio msg = MessaggioFactory.Create(str); if (msg != null) { Console.WriteLine(msg.GetType().ToString()); sw.WriteLine("+OK"); } else { Console.WriteLine("Messaggio sconosciuto"); sw.WriteLine("-ERR"); } } } catch (Exception) { Console.WriteLine("Messaggio sconosciuto"); sw.WriteLine("-ERR"); } }