Advertisement
Guest User

Untitled

a guest
Dec 27th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.15 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Net.Sockets;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using MySimpleFtp.Core.Data;
  10. using MySimpleFtp.Core.Models;
  11.  
  12. namespace MySimpleFtp.Server
  13. {
  14. public class Server
  15. {
  16. public static Socket listener;
  17.  
  18. public static void StartListening()
  19. {
  20. IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
  21. IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
  22. IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
  23.  
  24. IList<User> AuthorizedUsers = UserDatabase.Instance.Users.ToList();
  25.  
  26. ClientHandler client;
  27.  
  28. listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  29.  
  30. try {
  31. listener.Bind(localEndPoint);
  32. listener.Listen(10);
  33.  
  34. Console.WriteLine("Server started...");
  35.  
  36. while (true) {
  37. Console.WriteLine("Waiting for a connection...");
  38.  
  39. Socket handler = listener.Accept();
  40. handler.Ttl = 0;
  41.  
  42. byte[] bytes = new byte[65536];
  43. int bytesReceived = handler.Receive(bytes);
  44. string data = Encoding.ASCII.GetString(bytes, 0, bytesReceived);
  45. string username = data.Substring(0, data.IndexOf(":"));
  46. string password = data.Substring(username.Length + 1);
  47. foreach (User user in AuthorizedUsers) {
  48. if (user.Username == username && user.Password == password) {
  49. client = new ClientHandler(user);
  50. }
  51. }
  52. handler.Shutdown(SocketShutdown.Both);
  53. handler.Close();
  54. }
  55.  
  56. } catch (Exception e) {
  57. Console.WriteLine(e.ToString());
  58. }
  59. }
  60.  
  61. public static int Main(String[] args)
  62. {
  63. StartListening();
  64. return 0;
  65. }
  66. }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement