// прослушивание входящих подключений
        protected internal void Listen()
        {
            string    Host      = System.Net.Dns.GetHostName();
            string    IP        = System.Net.Dns.GetHostByName(Host).AddressList[0].ToString();
            IPAddress localaddr = IPAddress.Parse("0.0.0.0");

            try
            {
                tcpListener = new TcpListener(localaddr, 9999);
                tcpListener.Start();
                Console.WriteLine("Server is online. IP address: " + IP);
                while (true)
                {
                    TcpClient tcpClient    = tcpListener.AcceptTcpClient();
                    ClientObj clientObject = new ClientObj(tcpClient, this);
                    Thread    clientThread = new Thread(new ThreadStart(clientObject.Process));
                    clientThread.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                CloseAll();
            }
        }
        protected internal void RemoveConnection(string id)
        {
            // получаем по id закрытое подключение
            ClientObj client = clients.FirstOrDefault(c => c.Id == id);

            // и удаляем его из списка подключений
            if (client != null)
            {
                clients.Remove(client);
            }
        }
Exemple #3
0
        // прослушивание входящих подключений
        protected internal void Listen()
        {
            try
            {
                tcpListener = new TcpListener(IPAddress.Any, 8888);
                tcpListener.Start();
                Console.WriteLine("Сервер запущен. Ожидание подключений...");

                while (true)
                {
                    TcpClient tcpClient = tcpListener.AcceptTcpClient();

                    ClientObj clientObject = new ClientObj(tcpClient, this);
                    Thread    clientThread = new Thread(new ThreadStart(clientObject.Process));
                    clientThread.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Disconnect();
            }
        }
 List <ClientObj> clients = new List <ClientObj>(); // все подключения
 protected internal void AddConnection(ClientObj clientObject)
 {
     clients.Add(clientObject);
 }
Exemple #5
0
        static void Listening(ClientObj clientObj)
        {
            string Name2 = "";

            while (clientObj.Connected)
            {
                var req = JsonConvert.DeserializeObject <Request>(clientObj.Read());
                if (req == null)
                {
                    clientObj.Close();
                    break;
                }
                Console.WriteLine();
                Console.WriteLine($"[ Request { req.Type }]::[{ Name2 }]::[{ req.Command } ]");

                if (req.Command == "login")
                {
                    var logindata = req.Data.Split(',');
                    var client    = new Client(logindata[0], logindata[1], clientObj.Id);
                    if (logindata[2] == "new")
                    {
                        if (Db.Exist(client))
                        {
                            clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.ExistAccount, String.Empty)));
                        }
                        else
                        {
                            Db.Clients.Add(client);
                            Directory.CreateDirectory(client.Dir);
                            Name2 = client.Name;
                            Console.WriteLine($"[ Register New Client Name:'{client.Name}' ]");
                            clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.OK, String.Empty)));
                        }
                    }
                    else if (logindata[2] == "old")
                    {
                        if (Db.Exist(client))
                        {
                            Console.WriteLine($" Login Old Client Name:'{ client.Name }' ]");
                            Name2 = client.Name;
                            clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.OK, String.Empty)));
                        }
                        else
                        {
                            clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.NotAccount, string.Empty)));
                        }
                    }
                    else
                    {
                        clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.BadRequest, String.Empty)));
                    }
                }
                else if (req.Command == "getfilepaths")
                {
                    var files  = new Dictionary <string, int>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    GetFiles($"{ client.Dir }", files);

                    clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.OK, JsonConvert.SerializeObject(files))));
                }
                else if (req.Command == "getfiles")
                {
                    var files  = new Dictionary <string, byte[]>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    var files2 = new Dictionary <string, int>();
                    GetFiles($"{ client.Dir }", files2);
                    foreach (var i in files2)
                    {
                        files.Add(i.Key, File.ReadAllBytes($@"Static\\{ client.Name }\\{ i.Key }"));
                    }
                    clientObj.Write(JsonConvert.SerializeObject(new Response(StatusCodes.OK, JsonConvert.SerializeObject(files))));
                }
                else if (req.Command == "createfile")
                {
                    var files  = new Dictionary <string, byte[]>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    files = JsonConvert.DeserializeObject <Dictionary <string, byte[]> >(req.Data);
                    foreach (var file in files)
                    {
                        File.WriteAllBytes($"{client.Dir}\\{file.Key}", file.Value);
                    }
                }
                else if (req.Command == "deletefile")
                {
                    var files  = new List <string>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    files = JsonConvert.DeserializeObject <List <string> >(req.Data);
                    foreach (var file in files)
                    {
                        if (File.Exists($"{client.Dir}\\{file}"))
                        {
                            File.Delete($"{client.Dir}\\{file} ");
                        }
                    }
                }
                else if (req.Command == "renamefiles")
                {
                    var files  = new Dictionary <string, string>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    files = JsonConvert.DeserializeObject <Dictionary <string, string> >(req.Data);
                    foreach (var file in files)
                    {
                        if (File.Exists($"{client.Dir}\\{file.Key}"))
                        {
                            File.Move($"{client.Dir}\\{file.Key}", $"{client.Dir}\\{file.Value}");
                        }
                    }
                }
                else if (req.Command == "changefiles")
                {
                    var files  = new Dictionary <string, byte[]>();
                    var client = Db.Clients.Find(item => item.Name == Name2);
                    files = JsonConvert.DeserializeObject <Dictionary <string, byte[]> >(req.Data);
                    foreach (var file in files)
                    {
                        File.WriteAllBytes($"{client.Dir}\\{file.Key}", file.Value);
                    }
                }
            }
            Console.WriteLine($"[ Disconnect Client ID: { clientObj.Id } ]");
        }