private static async Task HandleClient(TcpClient tcpClientSocket) { var isClientConnected = true; IClientDataAccess clientDataAccess = new ClientDataAccess(); IClientService clientService = new ClientService(clientDataAccess); DateTime hour = DateTime.Now; string connectionHour = hour.Hour + ":" + hour.Minute; int token = clientService.AddClient(connectionHour); try { using (var networkStream = tcpClientSocket.GetStream()) { SendMenu(networkStream); IFrameHandler frameHandler = new FrameHandler(networkStream); while (isClientConnected) { var frame = await frameHandler.ReadDataAsync(); var printiame = Encoding.ASCII.GetString(frame); Console.WriteLine("Now handeling: " + printiame); isClientConnected = HandleRequest(frame, networkStream); } clientService.RemoveClient(token); } } catch (SocketException) { Console.WriteLine("The client connection was interrupted"); } }
static async Task Main(string[] args) { try { var tcpClient = await ClientConnectAsync().ConfigureAwait(false); var keepConnection = true; using (var networkStream = tcpClient.GetStream()) { FrameHandler frameHandler = new FrameHandler(networkStream); var serverResponse = await frameHandler.ReadDataAsync().ConfigureAwait(false); var menu = Encoding.ASCII.GetString(serverResponse); Console.WriteLine(menu); while (keepConnection) { var frameToBeSent = ""; while (frameToBeSent == "") { frameToBeSent = Console.ReadLine(); } if (frameToBeSent.Equals("exit")) { keepConnection = false; var exitFrame = Encoding.UTF8.GetBytes("REQFF0000"); var tarea = frameHandler.SendMessageAsync(exitFrame); serverResponse = await frameHandler.ReadDataAsync().ConfigureAwait(false); var asciiResponse = (Encoding.ASCII.GetString(serverResponse)); Console.WriteLine(asciiResponse); } else { var encodedFrame = Encoding.UTF8.GetBytes(frameToBeSent); var tarea = frameHandler.SendMessageAsync(encodedFrame); serverResponse = await frameHandler.ReadDataAsync().ConfigureAwait(false); var asciiResponse = (Encoding.ASCII.GetString(serverResponse)); string[] separator = { "@" }; string[] splittedResponse = asciiResponse.Split(separator, StringSplitOptions.RemoveEmptyEntries); Console.WriteLine(splittedResponse[0]); if (splittedResponse[0].Equals("Sending file...")) { IFileFunctions fileFunctions = new FileFunctions(); IFileHandler fileHandler = new FileHandler(networkStream); IFileService fileService = new FileService(fileFunctions, fileHandler); try { fileService.SendFile(splittedResponse[1]); serverResponse = await frameHandler.ReadDataAsync().ConfigureAwait(false); asciiResponse = (Encoding.ASCII.GetString(serverResponse)); Console.WriteLine(asciiResponse); } catch (BussinesLogicException e) { Console.WriteLine(e.Message); } } if (asciiResponse.Equals("Goodbye")) { keepConnection = false; } } } } tcpClient.Close(); } catch (Exception e) { Console.WriteLine("Ha ocurrido un error de conexion verifique su app config o que el servidor este levantado"); Console.ReadLine(); } }