Guest User

Untitled

a guest
Oct 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using System.Net.Sockets;
  8. using System.Net;
  9.  
  10. namespace AlexandriaServer
  11. {
  12. class Server
  13. {
  14.  
  15. //IPAddress localIP = IPAddress.Broadcast;
  16. //static readonly IPAddress Any;
  17. TcpListener tcpListener = new TcpListener(8080);
  18. //File file;
  19. char[] seperators = { ':' };
  20.  
  21. string fileLocation = "\\files";
  22.  
  23. public void runServer()
  24. {
  25. Console.WriteLine("Starting server...");
  26. tcpListener.Start();
  27. Console.WriteLine("Server Started!");
  28. //Byte[] bytes = new Byte[16];
  29. //Byte[] data = new Byte[16];
  30.  
  31. bool fileComplete = false;
  32.  
  33. while (true)
  34. {
  35. TcpClient tcpClient = tcpListener.AcceptTcpClient();
  36. Console.WriteLine("Client Connected!");
  37. NetworkStream stream = tcpClient.GetStream();
  38. byte[] incomingData = new byte[tcpClient.ReceiveBufferSize];
  39. int bytesRead = stream.Read(incomingData, 0, System.Convert.ToInt32(tcpClient.ReceiveBufferSize));
  40.  
  41. string command = Encoding.ASCII.GetString(incomingData, 0, bytesRead);
  42.  
  43. Console.WriteLine(command);
  44.  
  45. string[] commands = command.Split(seperators);
  46.  
  47. foreach (string x in commands)
  48. {
  49. Console.WriteLine(x);
  50. }
  51.  
  52. if (commands[0].Contains("GET"))
  53. {
  54. Console.WriteLine("Getting File! \n" + commands[1]+"test");
  55. //file = new File(commands[1], fileLocation);
  56. FileStream fs = new FileStream(commands[1].Replace("\n", ""), FileMode.Open);
  57.  
  58. int currentPos = 0;
  59.  
  60. while (currentPos < fs.Length)
  61. {
  62. byte[] bytes = new byte[16];
  63. int data = fs.Read(bytes, currentPos, 16);
  64.  
  65. Console.WriteLine(bytes.ToString());
  66. stream.Write(bytes, 0, 16);
  67. currentPos += 16;
  68. if (currentPos >= fs.Length)
  69. fileComplete = true;
  70.  
  71. }
  72.  
  73. if (fileComplete)
  74. {
  75. Console.WriteLine("File Complete!");
  76.  
  77. stream.Close();
  78. }
  79. }
  80. }
  81. }
  82.  
  83. public void sendFile(string fileName)
  84. {
  85. }
  86. }
  87. }
Add Comment
Please, Sign In to add comment