-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathIPMulticastServer.cs
More file actions
executable file
·138 lines (125 loc) · 3.95 KB
/
Copy pathIPMulticastServer.cs
File metadata and controls
executable file
·138 lines (125 loc) · 3.95 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
namespace RemoteControl
{
public class IPMulticastServer:IDisposable
{
private const string commandText = "getserver";
private const string MulticastAddress = "224.0.0.1";
private const int appPort = 9999;
private int _port;
private bool canFindClient = false;
public ServerStatus ServerStatus { get;private set; }
private Thread workThread;
public string AuthenticationString { get; set; }
public int ClientPort
{
get
{
return _port;
}
}
public int ServerPort { get; set; }
private bool PortIsRight (int value)
{
if (value < 65536 && value > 1024)
{
return true;
}
else
{
return false;
}
}
public IPMulticastServer()
{
workThread = new Thread(new ThreadStart(work));
ServerPort = 8888;
_port = 7777;
workThread.IsBackground = true;
ServerStatus = ServerStatus.Stop;
}
public void Start()
{
workThread.Start();
ServerStatus = ServerStatus.Runing;
}
private void work()
{
while (!canFindClient)
{
string cmd = Recieve();
Console.WriteLine(cmd);
if (cmd!=null && cmd.ToLower().Trim() == commandText)
{
if(String.IsNullOrEmpty(AuthenticationString))
{
SendMsg("ok");
}
else
{
SendMsg("key");
string recv = Recieve();
if (recv.Trim() == AuthenticationString)
{
SendMsg("ok");
}
}
Thread.Sleep(1000);
}
else
{
}
}
}
private void SendMsg(string msg)
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Parse(MulticastAddress), _port);
EndPoint ep = (EndPoint)iep;
byte[] b = Encoding.ASCII.GetBytes(msg);
s.SendTo(b, ep);
s.Close();
}
catch(Exception e)
{
Console.WriteLine(e.Message);
}
}
private string Recieve()
{
try
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPEndPoint iep = new IPEndPoint(IPAddress.Any, ServerPort);
EndPoint ep = (EndPoint)iep;
s.Bind(iep);
s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(IPAddress.Parse(MulticastAddress)));
byte[] b = new byte[1024];
int len = s.ReceiveFrom(b, ref ep);
string test;
test = System.Text.Encoding.ASCII.GetString(b,0,len);
s.Close();
return test;
}
catch(Exception e)
{
Console.WriteLine(e.Message);
return null;
}
}
public void Dispose()
{
canFindClient = false;
workThread.Abort();
ServerStatus = ServerStatus.Stop;
}
}
}