/
Scenario5_MulticastAndBroadcast.xaml.cs
297 lines (262 loc) · 12.2 KB
/
Scenario5_MulticastAndBroadcast.xaml.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
using SDKTemplate;
using System;
using System.Collections.Generic;
using Windows.ApplicationModel.Core;
using Windows.Networking;
using Windows.Networking.Connectivity;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation;
namespace DatagramSocketSample
{
/// <summary>
/// A page for fifth scenario.
/// </summary>
public sealed partial class Scenario5 : Page
{
// A pointer back to the main page. This is needed if you want to call methods in MainPage such
// as NotifyUser()
private MainPage rootPage = MainPage.Current;
private DatagramSocket listenerSocket = null;
public Scenario5()
{
this.InitializeComponent();
}
private void CloseListenerSocket()
{
if (listenerSocket != null)
{
// DatagramSocket.Close() is exposed through the Dispose() method in C#.
// The call below explicitly closes the socket, freeing the UDP port that it is currently bound to.
listenerSocket.Dispose();
listenerSocket = null;
}
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached. The Parameter
/// property is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
MulticastRadioButton.IsChecked = true;
}
/// <summary>
/// Invoked immediately before the Page is unloaded and is no longer the current source of a parent Frame.
/// </summary>
/// <param name="e">Event data representative of the navigation that will unload the current Page.</param>
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
CloseListenerSocket();
}
// Sets up the UI to display the multicast scenario options.
private void SetupMulticastScenarioUI()
{
RemoteAddressLabel.Text = "Multicast Group:";
StartListener.Content = "Start listener and join multicast group";
RemoteAddress.Text = "224.3.0.5";
RemoteAddress.IsEnabled = false;
SendMessageButton.IsEnabled = false;
CloseListenerButton.IsEnabled = false;
SendOutput.Text = "";
}
// Sets up the UI to display the broadcast scenario options.
private void SetupBroadcastScenarioUI()
{
RemoteAddressLabel.Text = "Broadcast Address:";
StartListener.Content = "Start listener";
RemoteAddress.Text = "255.255.255.255";
RemoteAddress.IsEnabled = false;
SendMessageButton.IsEnabled = false;
CloseListenerButton.IsEnabled = false;
SendOutput.Text = "";
}
private void MulticastRadioButton_Checked(object sender, RoutedEventArgs e)
{
CloseListenerSocket();
SetupMulticastScenarioUI();
}
private void MulticastRadioButton_Unchecked(object sender, RoutedEventArgs e)
{
CloseListenerSocket();
SetupBroadcastScenarioUI();
}
/// <summary>
/// This is the click handler for the 'StartListener' button.
/// </summary>
/// <param name="sender">Object for which the event was generated.</param>
/// <param name="e">Event's parameters.</param>
private async void StartListener_Click(object sender, RoutedEventArgs e)
{
if (String.IsNullOrEmpty(ServiceName.Text))
{
rootPage.NotifyUser("Please provide a service name.", NotifyType.ErrorMessage);
return;
}
if (listenerSocket != null)
{
rootPage.NotifyUser("A listener socket is already set up.", NotifyType.ErrorMessage);
return;
}
bool isMulticastSocket = (MulticastRadioButton.IsChecked == true);
listenerSocket = new DatagramSocket();
listenerSocket.MessageReceived += MessageReceived;
if (isMulticastSocket)
{
// DatagramSockets conduct exclusive (SO_EXCLUSIVEADDRUSE) binds by default, effectively blocking
// any other UDP socket on the system from binding to the same local port. This is done to prevent
// other applications from eavesdropping or hijacking a DatagramSocket's unicast traffic.
//
// Setting the MulticastOnly control option to 'true' enables a DatagramSocket instance to share its
// local port with any Win32 sockets that are bound using SO_REUSEADDR/SO_REUSE_MULTICASTPORT and
// with any other DatagramSocket instances that have MulticastOnly set to true. However, note that any
// attempt to use a multicast-only DatagramSocket instance to send or receive unicast data will result
// in an exception being thrown.
//
// This control option is particularly useful when implementing a well-known multicast-based protocol,
// such as mDNS and UPnP, since it enables a DatagramSocket instance to coexist with other applications
// running on the system that also implement that protocol.
listenerSocket.Control.MulticastOnly = true;
}
// Start listen operation.
try
{
await listenerSocket.BindServiceNameAsync(ServiceName.Text);
if (isMulticastSocket)
{
// Join the multicast group to start receiving datagrams being sent to that group.
listenerSocket.JoinMulticastGroup(new HostName(RemoteAddress.Text));
rootPage.NotifyUser(
"Listening on port " + listenerSocket.Information.LocalPort + " and joined to multicast group",
NotifyType.StatusMessage);
}
else
{
rootPage.NotifyUser(
"Listening on port " + listenerSocket.Information.LocalPort,
NotifyType.StatusMessage);
}
// Enable scenario steps that require us to have an active listening socket.
SendMessageButton.IsEnabled = true;
CloseListenerButton.IsEnabled = true;
}
catch (Exception exception)
{
listenerSocket.Dispose();
listenerSocket = null;
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
rootPage.NotifyUser(
"Start listening failed with error: " + exception.Message,
NotifyType.ErrorMessage);
}
}
/// <summary>
/// This is the click handler for the 'SendMessage' button.
/// </summary>
/// <param name="sender">Object for which the event was generated.</param>
/// <param name="e">Event's parameters.</param>
private async void SendMessage_Click(object sender, RoutedEventArgs e)
{
SendOutput.Text = "";
try
{
IOutputStream outputStream;
HostName remoteHostname = new HostName(RemoteAddress.Text);
// GetOutputStreamAsync can be called multiple times on a single DatagramSocket instance to obtain
// IOutputStreams pointing to various different remote endpoints. The remote hostname given to
// GetOutputStreamAsync can be a unicast, multicast or broadcast address.
outputStream = await listenerSocket.GetOutputStreamAsync(remoteHostname, ServiceName.Text);
// Send out some multicast or broadcast data. Datagrams generated by the IOutputStream will use
// <source host, source port> information obtained from the parent socket (i.e., 'listenSocket' in
// this case).
const string stringToSend = "Hello";
DataWriter writer = new DataWriter(outputStream);
writer.WriteString(stringToSend);
await writer.StoreAsync();
SendOutput.Text = "\"" + stringToSend + "\" sent successfully.";
}
catch (Exception exception)
{
// If this is an unknown status it means that the error is fatal and retry will likely fail.
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
rootPage.NotifyUser("Send failed with error: " + exception.Message, NotifyType.ErrorMessage);
}
}
/// <summary>
/// This is the click handler for the 'CloseListener' button.
/// </summary>
/// <param name="sender">Object for which the event was generated.</param>
/// <param name="e">Event's parameters.</param>
private void CloseListener_Click(object sender, RoutedEventArgs e)
{
CloseListenerSocket();
// Disable scenario steps that require us to have an active listening socket.
SendMessageButton.IsEnabled = false;
CloseListenerButton.IsEnabled = false;
SendOutput.Text = "";
rootPage.NotifyUser("Listener closed", NotifyType.StatusMessage);
}
/// <summary>
/// Message received handler
/// </summary>
/// <param name="socket">The socket object</param>
/// <param name="eventArguments">The datagram event information</param>
void MessageReceived(DatagramSocket socket, DatagramSocketMessageReceivedEventArgs eventArguments)
{
try
{
// Interpret the incoming datagram's entire contents as a string.
uint stringLength = eventArguments.GetDataReader().UnconsumedBufferLength;
string receivedMessage = eventArguments.GetDataReader().ReadString(stringLength);
NotifyUserFromAsyncThread(
"Received data from remote peer (Remote Address: " +
eventArguments.RemoteAddress.CanonicalName +
", Remote Port: " +
eventArguments.RemotePort + "): \"" +
receivedMessage + "\"",
NotifyType.StatusMessage);
}
catch (Exception exception)
{
SocketErrorStatus socketError = SocketError.GetStatus(exception.HResult);
if (SocketError.GetStatus(exception.HResult) == SocketErrorStatus.Unknown)
{
throw;
}
rootPage.NotifyUser(
"Error happened when receiving a datagram:" + exception.Message,
NotifyType.ErrorMessage);
}
}
/// <summary>
/// Notifies the user from a non-UI thread
/// </summary>
/// <param name="strMessage">The message</param>
/// <param name="type">The type of notification</param>
private void NotifyUserFromAsyncThread(string strMessage, NotifyType type)
{
var ignore = Dispatcher.RunAsync(
CoreDispatcherPriority.Normal, () => rootPage.NotifyUser(strMessage, type));
}
}
}