update
This commit is contained in:
@@ -1,36 +1,106 @@
|
||||
using System;
|
||||
using System.Net.Sockets;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
|
||||
class Client
|
||||
{
|
||||
private static TcpClient client;
|
||||
private static NetworkStream stream;
|
||||
private static string currentGroup = "all";
|
||||
|
||||
static void Main()
|
||||
{
|
||||
TcpClient client = new TcpClient();
|
||||
Console.Write("Введите ваше имя: ");
|
||||
string name = Console.ReadLine();
|
||||
|
||||
string serverIp = "127.0.0.1";
|
||||
int port = 8888;
|
||||
|
||||
try
|
||||
{
|
||||
client.Connect("127.0.0.1", 8888);
|
||||
NetworkStream stream = client.GetStream();
|
||||
client = new TcpClient();
|
||||
client.Connect(serverIp, port);
|
||||
stream = client.GetStream();
|
||||
|
||||
Console.Write("Текст: ");
|
||||
string text = Console.ReadLine();
|
||||
// Отправляем имя
|
||||
byte[] nameData = Encoding.UTF8.GetBytes(name);
|
||||
stream.Write(nameData, 0, nameData.Length);
|
||||
|
||||
byte[] data = Encoding.UTF8.GetBytes(text);
|
||||
stream.Write(data, 0, data.Length);
|
||||
Console.WriteLine($"Добро пожаловать, {name}!");
|
||||
|
||||
data = new byte[1024];
|
||||
int bytes = stream.Read(data, 0, data.Length);
|
||||
string response = Encoding.UTF8.GetString(data, 0, bytes);
|
||||
// Поток для приема сообщений
|
||||
Thread receiveThread = new Thread(ReceiveMessages);
|
||||
receiveThread.Start();
|
||||
|
||||
Console.WriteLine($"Ответ: {response}");
|
||||
// Циклическая отправка сообщений
|
||||
while (true)
|
||||
{
|
||||
Console.Write($"[Группа: {currentGroup}] Введите сообщение: ");
|
||||
string text = Console.ReadLine();
|
||||
|
||||
stream.Close();
|
||||
client.Close();
|
||||
if (text.ToLower() == "exit")
|
||||
break;
|
||||
else if (text.StartsWith("/join "))
|
||||
{
|
||||
currentGroup = text.Substring(6);
|
||||
string command = $"/group {currentGroup}";
|
||||
byte[] data = Encoding.UTF8.GetBytes(command);
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
else if (text.StartsWith("/groups"))
|
||||
{
|
||||
// Запрос списка групп и клиентов
|
||||
byte[] data = Encoding.UTF8.GetBytes("/list");
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
else
|
||||
{
|
||||
string message = $"/msg {currentGroup} {text}";
|
||||
byte[] data = Encoding.UTF8.GetBytes(message);
|
||||
stream.Write(data, 0, data.Length);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine($"Ошибка: {e.Message}");
|
||||
}
|
||||
finally
|
||||
{
|
||||
stream?.Close();
|
||||
client?.Close();
|
||||
}
|
||||
}
|
||||
|
||||
private static void ReceiveMessages()
|
||||
{
|
||||
byte[] buffer = new byte[4096];
|
||||
try
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int bytes = stream.Read(buffer, 0, buffer.Length);
|
||||
if (bytes == 0) break;
|
||||
|
||||
string response = Encoding.UTF8.GetString(buffer, 0, bytes);
|
||||
|
||||
if (response.StartsWith("/clients "))
|
||||
{
|
||||
Console.WriteLine($"\n=== Список клиентов и групп ===");
|
||||
Console.WriteLine(response.Substring(9));
|
||||
Console.WriteLine("================================");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\n[Сообщение]: {response}");
|
||||
}
|
||||
Console.Write($"[Группа: {currentGroup}] Введите сообщение: ");
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine("\nСоединение с сервером потеряно");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user