-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroadcast.cs
More file actions
51 lines (46 loc) · 1.69 KB
/
Broadcast.cs
File metadata and controls
51 lines (46 loc) · 1.69 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
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Web.Script.Serialization;
namespace Aaf.Sinc.Transport
{
/// <summary>
/// 广播
/// </summary>
internal class Broadcast
{
public static bool Online = true;
public void Send()
{
var udpClient = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
var ep = new IPEndPoint(IPAddress.Parse("192.168.1.255"), Protocol.BROADCAST_PORT);
//var ep = new IPEndPoint(IPAddress.Broadcast, Protocol.BROADCAST_PORT);
var serializer = new JavaScriptSerializer();
var node = string.Format("{0}{1}", Protocol.NODE_STATUS_CMD, serializer.Serialize(new Node
{
Alias = Dns.GetHostName(),
ComputerName = Dns.GetHostName(),
IP = Protocol.LocalIP.ToString(),
WorkGroup = Protocol.DEFAULT_WORKGROUP,
Online = true
}));
var buff = Encoding.Default.GetBytes(node);
while (Online)
{
udpClient.SendTo(buff, ep);
Thread.Sleep(Protocol.BROADCAST_HEARTBEAT_INTERVAL);
}
node = string.Format("{0}{1}", Protocol.NODE_STATUS_CMD, serializer.Serialize(new Node
{
Alias = Dns.GetHostName(),
ComputerName = Dns.GetHostName(),
IP = Protocol.LocalIP.ToString(),
WorkGroup = Protocol.DEFAULT_WORKGROUP,
Online = false
}));
buff = Encoding.Default.GetBytes(node);
udpClient.SendTo(buff, ep);
}
}
}