forked from chronoxor/NetCoreServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (61 loc) · 2.15 KB
/
Copy pathProgram.cs
File metadata and controls
74 lines (61 loc) · 2.15 KB
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
72
73
74
using System;
using System.Net;
using System.Net.Sockets;
using NetCoreServer;
namespace UdpMulticastServer
{
class MulticastServer : UdpServer
{
public MulticastServer(IPAddress address, int port) : base(address, port) {}
protected override void OnError(SocketError error)
{
Console.WriteLine($"Multicast UDP server caught an error with code {error}");
}
}
class Program
{
static void Main(string[] args)
{
// UDP multicast address
string multicastAddress = "239.255.0.1";
if (args.Length > 0)
multicastAddress = args[0];
// UDP multicast port
int multicastPort = 3334;
if (args.Length > 1)
multicastPort = int.Parse(args[1]);
Console.WriteLine($"UDP multicast address: {multicastAddress}");
Console.WriteLine($"UDP multicast port: {multicastPort}");
Console.WriteLine();
// Create a new UDP multicast server
var server = new MulticastServer(IPAddress.Any, 0);
// Start the multicast server
Console.Write("Server starting...");
server.Start(multicastAddress, multicastPort);
Console.WriteLine("Done!");
Console.WriteLine("Press Enter to stop the server or '!' to restart the server...");
// Perform text input
for (;;)
{
string line = Console.ReadLine();
if (string.IsNullOrEmpty(line))
break;
// Restart the server
if (line == "!")
{
Console.Write("Server restarting...");
server.Restart();
Console.WriteLine("Done!");
continue;
}
// Multicast admin message to all sessions
line = "(admin) " + line;
server.Multicast(line);
}
// Stop the server
Console.Write("Server stopping...");
server.Stop();
Console.WriteLine("Done!");
}
}
}