static void ServerActionOnClientInput(String input, ProtocalHandler pHandler) { String[] Arguments = input.Split(" "); Console.WriteLine("Client Message: " + input); switch (Arguments[0]) { case "AUTH": pHandler.Authenticate(Arguments[1], Arguments[2]); break; case "REG": pHandler.Register(Arguments[1], Arguments[2]); break; case "FILES": pHandler.SendFilesList( ); break; case "DLOAD": pHandler.Download(Arguments[1]); break; } }
static void FileServerLoop(String InitialMessage, TcpClient tcpClient) { NetworkStream NWStream = tcpClient.GetStream( ); Reader SocketReader = new Reader( ); ProtocalHandler pHandler = new ProtocalHandler(tcpClient); while (Excecuting) { ServerActionOnFSInput(SocketReader.ReadFromSocket(NWStream, tcpClient.ReceiveBufferSize), pHandler); } }
static void ServerActionOnFSInput(String input, ProtocalHandler pHandler) { String[] Arguments = input.Split(" "); Console.WriteLine("File Server Message: " + input); switch (Arguments[0]) { case "REG": FSStreams.Add(pHandler.RegisterFileServer(Arguments[1], Arguments[2])); break; } }
static void Main(string[] args) { Console.WriteLine("File Server Started"); IPHostEntry hostentries = Dns.GetHostEntry(Dns.GetHostName( )); ipAddress = hostentries.AddressList[hostentries.AddressList.Length - 1]; while (true) { PrintMenue( ); GetInput( ); try { if (IPAddress.TryParse(Arguments[0], out IPAddress DAIPAddress)) { if (int.TryParse(Arguments[1], out int DAPort)) { ConnectToDa(DAIPAddress, DAPort); ProtocalHandler pHandler = new ProtocalHandler(tcpClient, tcpClient.GetStream()); Thread.Sleep(250); if (pHandler.RegisterWithDA( )) { Reader SocketReader = new Reader( ); Console.WriteLine("Connected To: " + DAIPAddress + " " + DAPort); new Thread(new ThreadStart(() => { StartClientHandler( ); })); NetworkStream NWStream = tcpClient.GetStream( ); while (true) { var message = SocketReader.ReadFromSocket(NWStream, tcpClient.ReceiveBufferSize); pHandler.HandleDaRequest(message); } } } } } catch (Exception Ex) { Console.WriteLine(Ex); } Thread.Sleep(1000); } }
static void ServerActionOnClientInput(String input, ProtocalHandler pHandler) { String[] Arguments = input.Split(" "); Console.WriteLine("Client Message: " + input); switch (Arguments[0]) { case "FILE": Byte[] EncryptionKey = pHandler.AuthenticateWithDA(Arguments[1]); pHandler.SendFile(EncryptionKey); break; } }
static void ClientLoop(String InitialMessage, TcpClient tcpClient) { try { NetworkStream NWStream = tcpClient.GetStream( ); Reader SocketReader = new Reader( ); ProtocalHandler pHandler = new ProtocalHandler(tcpClient, NWStream); ServerActionOnClientInput(InitialMessage, pHandler); while (Excecuting) { ServerActionOnClientInput(SocketReader.ReadFromSocket(NWStream, tcpClient.ReceiveBufferSize), pHandler); } } catch (Exception Ex) { Console.WriteLine(Ex); tcpClient.Close( ); } }
static void Main(string[] args) { while (true) { if (!connected) { PrintMenue( ); GetInput( ); try { if (Arguments[0] == "exit") { break; } if (IPAddress.TryParse(Arguments[0], out sIPAddress)) { if (int.TryParse(Arguments[1], out sPort)) { try { Console.WriteLine("Connecting..."); tcpClient = new TcpClient( ); IPEndPoint ipEndPoint = new IPEndPoint(sIPAddress, sPort); tcpClient.Connect(ipEndPoint); Console.WriteLine("To connect please write: <username> <password>"); GetInput( ); pHandler = new ProtocalHandler(tcpClient); if (pHandler.Authenticate(Arguments[0], Arguments[1])) { Console.WriteLine("Connected"); connected = true; } else { Console.WriteLine("Failed to verify user, closing connection."); Console.WriteLine("Attempt To Register with server? Yes/No"); GetInput( ); if (Arguments[0] == "Yes") { Console.WriteLine("To register please write: <username> <password>"); GetInput( ); if (pHandler.Register(Arguments[0], Arguments[1])) { Console.WriteLine("Connected"); connected = true; } } } } catch (Exception e) { Console.WriteLine(e); } Thread.Sleep(1000); } } } catch (Exception) { } } else { while (true) { Console.WriteLine("Connected To Server"); PrintServerMenue( ); if (FilesList.Count > 0) { PrintFilesList( ); } GetInput( ); if (Arguments[0] == "exit") { break; } if (Arguments[0] == "L") { pHandler.DisplayFileList(FilesList); } if (Arguments[0] == "l") { pHandler.DisplayLocalFilesList(FilesList); } if (Arguments[0] == "D") { pHandler.DownloadFile(Arguments[1]); } if (Arguments[0] == "H") { displayFilesList = false; } LastInput = Input; } break; } } }