Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.Text;
- using System.Threading;
- using System.IO;
- using System.Collections.Generic;
- namespace server
- {
- class Program
- {
- static TcpListener listener = null;
- static Dictionary<string, string> credentials;
- static Dictionary<string, TcpClient> clients = new Dictionary<string, TcpClient>();
- static Dictionary<string, TcpClient> authclients = new Dictionary<string, TcpClient>();
- static void Main(string[] args)
- {
- string filePath = "clients.txt";
- credentials = ReadCredentialsFromFile(filePath);
- listener = new TcpListener(IPAddress.Any, 8000);
- listener.Start();
- Console.WriteLine("Сервер запущен. Ожидание подключений...");
- while (true)
- {
- TcpClient client = listener.AcceptTcpClient();
- Thread clientThread = new Thread(() => HandleClient(client));
- clientThread.Start();
- }
- }
- static Dictionary<string, string> ReadCredentialsFromFile(string filePath)
- {
- Dictionary<string, string> credentials = new Dictionary<string, string>();
- try
- {
- string[] lines = File.ReadAllLines(filePath);
- foreach (string line in lines)
- {
- string[] tokens = line.Split(' ');
- if (tokens.Length == 2)
- {
- string username = tokens[0];
- string password = tokens[1];
- credentials[username] = password;
- }
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine($"Ошибка при чтении файла: {ex.Message}");
- }
- return credentials;
- }
- static void HandleClient(TcpClient client)
- {
- NetworkStream stream = client.GetStream();
- byte[] buffer = new byte[1024];
- int bytesRead;
- bool isAuthenticated = false;
- while (true)
- {
- bytesRead = 0;
- try
- {
- bytesRead = stream.Read(buffer, 0, buffer.Length);
- }
- catch
- {
- break;
- }
- if (bytesRead == 0)
- {
- break;
- }
- string message = Encoding.UTF8.GetString(buffer, 0, bytesRead);
- if (!isAuthenticated && message.StartsWith("AUTH"))
- {
- string[] authData = message.Split(' ');
- if (authData.Length == 3)
- {
- string username = authData[1];
- string password = authData[2];
- if (credentials.ContainsKey(username) && credentials[username] == password)
- {
- if (authclients.ContainsKey(username))
- {
- SendMessage(client, "Ошибка: клиент с даннымм логином уже подключен");
- Console.WriteLine($"{username} уже подключен");
- client.Close();
- }
- else
- {
- authclients[username] = client;
- clients[username] = client;
- SendMessage(client, "AUTH_SUCCES");
- Console.WriteLine($"{username} успешно аутентифицирован");
- isAuthenticated = true;
- }
- }
- else
- {
- SendMessage(client, $"Ошибка аутентификации {username}");
- Console.WriteLine($"{username} ошибка аутентификации");
- client.Close();
- }
- }
- }
- Console.WriteLine($"Получено сообщение от клиента {message}");
- foreach (var kvp in authclients)
- {
- if (kvp.Value != client && kvp.Value.Connected)
- {
- if (!message.StartsWith("AUTH"))
- {
- SendMessage(kvp.Value, message);
- }
- }
- }
- }
- Console.WriteLine("Клиент отключился");
- client.Close();
- }
- static void SendMessage(TcpClient client, string message)
- {
- NetworkStream stream = client.GetStream();
- byte[] buffer = Encoding.UTF8.GetBytes(message);
- stream.Write(buffer, 0, buffer.Length);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement