Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Net;
  5. using System.Net.Sockets;
  6. using System.Text;
  7.  
  8. namespace Server
  9. {
  10.     class Server
  11.     {
  12.         private static byte[] buffer = new byte[1024];
  13.         private static int port;
  14.         private static List<Socket> clientSockets = new List<Socket>();
  15.         private static Socket serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  16.  
  17.         static void Main(string[] args)
  18.         {
  19.             DirectoryCreation.CreateDirectories();
  20.             Console.Title = "Server";
  21.             Console.WriteLine("Welcome to MCBank! Please type a valid Port.");
  22.  
  23.             string portString = Console.ReadLine();
  24.  
  25.             while (!int.TryParse(portString, out port))
  26.             {
  27.                 Console.WriteLine("Port invalid, please type in a valid Port.");
  28.                 portString = Console.ReadLine();
  29.             }
  30.  
  31.             SetupServer();
  32.         }
  33.         private static void SetupServer()
  34.         {
  35.             Console.WriteLine("Setting up server...");
  36.             serverSocket.Bind(new IPEndPoint(IPAddress.Any, port));
  37.             serverSocket.Listen(10);
  38.             Console.WriteLine("Server is running.");
  39.             serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
  40.             Console.Read();
  41.         }
  42.  
  43.         private static void AcceptCallback(IAsyncResult AR)
  44.         {
  45.             Socket socket = serverSocket.EndAccept(AR);
  46.             Console.WriteLine("Client connected.");
  47.             clientSockets.Add(socket);
  48.             socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
  49.             serverSocket.BeginAccept(new AsyncCallback(AcceptCallback), null);
  50.         }
  51.  
  52.         private static void ReceiveCallback(IAsyncResult AR)
  53.         {
  54.             Socket socket = (Socket)AR.AsyncState;
  55.             try
  56.             {
  57.                 int received = socket.EndReceive(AR);
  58.                 byte[] dataBuf = new byte[received];
  59.  
  60.                 Array.Copy(buffer, dataBuf, received);
  61.  
  62.                 string command = Encoding.ASCII.GetString(dataBuf);
  63.                 byte[] data = Encoding.ASCII.GetBytes(Commands(command));
  64.                 socket.BeginSend(data, 0, data.Length, SocketFlags.None, new AsyncCallback(SendCallback), socket);
  65.                 socket.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, new AsyncCallback(ReceiveCallback), socket);
  66.             }
  67.             catch
  68.             {
  69.                 Console.WriteLine("Client disconnected.");
  70.                 socket.Close();
  71.                 socket.Dispose();
  72.             }
  73.         }
  74.  
  75.         private static void SendCallback(IAsyncResult AR)
  76.         {
  77.             Socket socket = (Socket)AR.AsyncState;
  78.             socket.EndSend(AR);
  79.         }
  80.  
  81.         private static string Commands(string str)
  82.         {
  83.             string[] split = str.Split(new Char[] { ' ' });
  84.             int i = int.Parse(split[0]);
  85.  
  86.             switch (i)
  87.             {
  88.                 case 1:
  89.                     if (User.UserExists(split[1]))
  90.                     {
  91.                         User user = new User(split[1]);
  92.  
  93.                         if (user.password == split[2])
  94.                         {
  95.                             Console.WriteLine($"{user.username} has logged in.");
  96.                             return "Auth successful";
  97.                         }
  98.  
  99.                         return "Auth unsuccessful";
  100.                     }
  101.  
  102.                     return "Auth unsuccessful";
  103.                 case 2:
  104.                     if (!User.UserExists(split[1]))
  105.                     {
  106.                         DataBasing.NewUser(split[1], split[2]);
  107.                         Console.WriteLine($"{split[1]} has logged in.");
  108.                         return "Signup successful";
  109.                     }
  110.                     return "Signup failed";
  111.                 case 3:
  112.                     return new User(split[1]).balance.ToString("0.00");
  113.                 case 4:
  114.                     FileInfo[] information = DataBasing.GetFiles();
  115.  
  116.                     string usernames = "";
  117.  
  118.                     for (int j = 0; j < information.Length; j++)
  119.                     {
  120.                         if (j < information.Length - 1)
  121.                         {
  122.                             usernames += information[j].Name.Replace(".txt", ", ");
  123.                         }
  124.                         else
  125.                         {
  126.                             usernames += information[j].Name.Replace(".txt", "");
  127.                         }
  128.                     }
  129.  
  130.                     return usernames;
  131.                 case 5:
  132.                     if (User.UserExists(split[1]))
  133.                     {
  134.                         return new User(split[1]).permission.ToString();
  135.                     }
  136.  
  137.                     return "-1";
  138.                 case 6:
  139.                     if (User.UserExists(split[1]))
  140.                     {
  141.                         User user = new User(split[1]);
  142.  
  143.                         float num;
  144.  
  145.                         if (float.TryParse(split[2], out num))
  146.                         {
  147.                             if (num < 0)
  148.                             {
  149.                                 return "The fund parameter cannot be a negative number.";
  150.                             }
  151.                             user.balance += num;
  152.                             User.SaveUser(user);
  153.                             Transaction.AppendTransaction(user.username, "Bank", "Deposit", num.ToString("0.00"));
  154.                             return "Funding successful.";
  155.                         }
  156.                         return "The fund parameter is not a float.";
  157.                     }
  158.                     return "User does not exist.";
  159.                 case 7:
  160.                     if (User.UserExists(split[2]))
  161.                     {
  162.                         User user = new User(split[1]);
  163.                         User user2 = new User(split[2]);
  164.  
  165.                         float num;
  166.  
  167.                         if (float.TryParse(split[3], out num))
  168.                         {
  169.                             if (num < 0)
  170.                             {
  171.                                 return "The fund parameter cannot be a negative number.";
  172.                             }
  173.  
  174.                             if (user.balance < num)
  175.                             {
  176.                                 return "Your balance is too low for this transaction.";
  177.                             }
  178.  
  179.                             if (user.username.ToLower() == user2.username.ToLower())
  180.                             {
  181.                                 return "You cannot fund yourself.";
  182.                             }
  183.  
  184.                             user2.balance += num;
  185.                             user.balance -= num;
  186.                             User.SaveUser(user);
  187.                             User.SaveUser(user2);
  188.                             Transaction.AppendTransaction(user.username, user2.username, "Withdrawal", num.ToString("0.00"));
  189.                             Transaction.AppendTransaction(user2.username, user.username, "Deposit", num.ToString("0.00"));
  190.                             return "Funding successful.";
  191.                         }
  192.                         return "The fund parameter is not a float.";
  193.                     }
  194.                     return "User does not exist.";
  195.                 case 8:
  196.                     if (User.UserExists(split[1]))
  197.                     {
  198.                         User user = new User(split[1]);
  199.  
  200.                         float num;
  201.  
  202.                         if (float.TryParse(split[2], out num))
  203.                         {
  204.                             if (num < 0)
  205.                             {
  206.                                 return "The funds parameter cannot be a negative number.";
  207.                             }
  208.                             user.balance -= num;
  209.                             User.SaveUser(user);
  210.                             Transaction.AppendTransaction(user.username, "Bank", "Withdrawal", num.ToString("0.00"));
  211.                             return "Withdraw successful.";
  212.                         }
  213.                         return "The funds parameter is not a float.";
  214.                     }
  215.                     return "User does not exist.";
  216.                 case 9:
  217.                     if (User.UserExists(split[1]))
  218.                     {
  219.                         int permission;
  220.  
  221.                         if (!int.TryParse(split[2], out permission))
  222.                         {
  223.                             return "Permission parameter is not a number.";
  224.                         }
  225.  
  226.                         if (permission < 0 || permission > 1)
  227.                         {
  228.                             return "Permission parameter is out of range.";
  229.                         }
  230.  
  231.                         User user = new User(split[1]);
  232.  
  233.                         if (user.permission == permission && permission == 1)
  234.                         {
  235.                             return $"{split[1]} already has admin permissions.";
  236.                         }
  237.  
  238.                         if (user.permission == permission && permission == 0)
  239.                         {
  240.                             return $"{split[1]} already has basic permissions.";
  241.                         }
  242.  
  243.                         if (user.username.ToLower() == DataBasing.CheckOwner().ToLower())
  244.                         {
  245.                             return "Admin permissions cannot be revoked from owner.";
  246.                         }
  247.  
  248.                         user.permission = permission;
  249.  
  250.                         User.SaveUser(user);
  251.  
  252.                         if (permission == 1)
  253.                         {
  254.                             return $"{split[1]} has been granted admin permissions.";
  255.                         }
  256.  
  257.                         return $"{split[1]} has been granted basic permissions.";
  258.                     }
  259.  
  260.                     return "User does not exist.";
  261.                 case 10:
  262.                     return Transaction.GetTransactions();
  263.                 case 11:
  264.                     if (User.UserExists(split[1]))
  265.                     {
  266.                         return Transaction.GetTransactions(split[1]);
  267.                     }
  268.  
  269.                     return "User does not exist.";
  270.                 case 12:
  271.                     return Conversions.ReturnConversions();
  272.             }
  273.             return "";
  274.         }
  275.     }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement