Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading.Tasks;
- using MySimpleFtp.Core.Data;
- using MySimpleFtp.Core.Models;
- namespace MySimpleFtp.Server
- {
- public class Server
- {
- public static Socket listener;
- public static void StartListening()
- {
- IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
- IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
- IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 11000);
- IList<User> AuthorizedUsers = UserDatabase.Instance.Users.ToList();
- ClientHandler client;
- listener = new Socket(ipAddress.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
- try {
- listener.Bind(localEndPoint);
- listener.Listen(10);
- Console.WriteLine("Server started...");
- while (true) {
- Console.WriteLine("Waiting for a connection...");
- Socket handler = listener.Accept();
- handler.Ttl = 0;
- byte[] bytes = new byte[65536];
- int bytesReceived = handler.Receive(bytes);
- string data = Encoding.ASCII.GetString(bytes, 0, bytesReceived);
- string username = data.Substring(0, data.IndexOf(":"));
- string password = data.Substring(username.Length + 1);
- foreach (User user in AuthorizedUsers) {
- if (user.Username == username && user.Password == password) {
- client = new ClientHandler(user);
- }
- }
- handler.Shutdown(SocketShutdown.Both);
- handler.Close();
- }
- } catch (Exception e) {
- Console.WriteLine(e.ToString());
- }
- }
- public static int Main(String[] args)
- {
- StartListening();
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement