-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
71 lines (63 loc) · 1.82 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using System.Net;
using System.Net.Sockets;
using System.Text;
int PORT = 1234;
int BUFFER_SIZE = 1024;
string HELP_INFO = "This is a simple chat room app.\nType \"/rename <name>\" to change your name.\n";
List<Socket> socketList = new();
Socket host = new(SocketType.Stream, ProtocolType.Tcp);
host.Bind(new IPEndPoint(IPAddress.Parse("0.0.0.0"), PORT));
host.Listen();
Log("Server started.");
for (;;)
{
Socket socket = host.Accept();
socketList.Add(socket);
_ = HandleClient(socket);
}
async Task HandleClient(Socket socket)
{
byte[] buffer = new byte[BUFFER_SIZE];
int msgSize;
string name = Guid.NewGuid().ToString();
Log($"{name} connected.");
while ((msgSize = await socket.ReceiveAsync(buffer)) > 0)
{
string message = Encoding.UTF8.GetString(buffer, 0, msgSize).Trim();
Log($"{name}: {message}");
if (message.StartsWith('/'))
{
HandleCommand(socket, message, ref name);
}
else
{
await HandleMessage(socket, $"{name}: {message}\n");
}
}
socketList.Remove(socket);
socket.Disconnect(true);
Log($"{name} disconnected.");
}
void HandleCommand(Socket socket, string msg, ref string name)
{
string[] commands = msg.Split(' ');
switch (commands[0])
{
case "/help": socket.Send(Encoding.UTF8.GetBytes(HELP_INFO)); break;
case "/rename": name = commands[1]; break;
default: break;
}
}
async Task HandleMessage(Socket socket, string msg)
{
byte[] bytes = Encoding.UTF8.GetBytes(msg);
foreach (Socket s in socketList)
{
if (s == socket) continue;
await s.SendAsync(bytes);
}
}
void Log(string msg)
{
Console.WriteLine($"{DateTime.UtcNow:yyyy-MM-dd hh:mm:ss}: {msg}");
}