private static void IterazioneComunica(StreamReader sr, StreamWriter sw, TipoStato stato) { while (true) { // lettura richiesta dal client string str = sr.ReadLine(); Console.WriteLine(str); // Elaborazione e invio risposta try { //Divisione messaggio string strMsg = SplitString(str, ref stato); //Creazione messaggio e ingresso e definizione stato Messaggio msg = null; TipoStato statoSuccessivo = CreateMsgIng(strMsg, stato, out msg); //Risposta dal server RispostaServer(msg, stato, statoSuccessivo, sw); if (statoSuccessivo == TipoStato.Q) { Console.WriteLine("Connessione chiusa"); break; } } catch (Exception) { Console.WriteLine("Messaggio sconosciuto"); sw.WriteLine("{0} -ERR", stato.ToString()); } } }
static async Task ComunicaClientAsync(object param) { TcpClient client = (TcpClient)param; NetworkStream nsRead = client.GetStream(); NetworkStream nsWrite = client.GetStream(); StreamReader sr = new StreamReader(nsRead); StreamWriter sw = new StreamWriter(nsWrite); sw.AutoFlush = true; TipoStato stato = TipoStato.C; while (stato != TipoStato.Q) { Console.Write("Digitare il messaggio: "); string risp = Console.ReadLine(); risp = stato.ToString() + ' ' + risp; sw.WriteLine(risp); string[] msgRisp = sr.ReadLine().Split(' '); stato = (TipoStato)Enum.Parse(typeof(TipoStato), msgRisp[0], true); Console.WriteLine(""); Console.WriteLine("La risposta è {0} {1}", msgRisp[0], msgRisp[1]); Console.WriteLine("-------------------------------"); Console.WriteLine(""); } }
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()); } }