This repository was archived by the owner on Mar 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifier.cs
More file actions
69 lines (58 loc) · 2.02 KB
/
Notifier.cs
File metadata and controls
69 lines (58 loc) · 2.02 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
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Collections.Generic;
namespace DoorKeeper
{
public class Notifier {
//Notify n clients about the Bell
private List<Socket> clients = new List<Socket>();
private Socket listener;
internal Notifier() {
IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 11000);
// Create a TCP/IP socket.
listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp );
// Bind the socket to the local endpoint and listen for incoming connections.
try {
listener.Bind(localEndPoint);
listener.Listen(100);
listener.BeginAccept(
new AsyncCallback(AcceptCallback),
listener );
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
void AcceptCallback(IAsyncResult ar) {
Socket server = (Socket)ar.AsyncState;
Socket client = server.EndAccept(ar);
server.BeginAccept(AcceptCallback, server);
// client socket logic...
clients.Add(client);
}
internal void notifyall(){
foreach(Socket clientSocket in clients)
{
try {
clientSocket.Send(Encoding.ASCII.GetBytes("\r\n"));
} catch (SocketException se) {
Console.WriteLine("SocketException : {0}",se.ToString());
} catch (Exception e) {
Console.WriteLine("Unexpected exception : {0}", e.ToString());
}
}
}
~Notifier ()
{
foreach(Socket clientSocket in clients)
{
clientSocket.Close ();
}
listener.Close ();
}
}
}