@@ -79,7 +79,7 @@ IPAddress ParseIp(string str)
7979 case "loopback" :
8080 case "localhost" : return IPAddress . Loopback ;
8181 case "broadcast" : return IPAddress . Broadcast ;
82- default : return IPAddress . Any ;
82+ default : return IPAddress . IPv6Any ;
8383 }
8484 }
8585 return ip ;
@@ -226,30 +226,48 @@ enum NowType
226226 }
227227 NowType nowType = NowType . Udp ;
228228
229- Socket socket ;
229+ object socket ;
230230 CancellationTokenSource loop ;
231231 List < Task > loops = new List < Task > ( ) ;
232232 private void Open_Button_Click ( object sender , RoutedEventArgs e )
233233 {
234234 if ( socket != null )
235235 {
236- socket . Close ( ) ;
236+ if ( socket is UdpClient uc )
237+ {
238+ uc . Close ( ) ;
239+ uc . Dispose ( ) ;
240+ }
241+ else if ( socket is Socket sk )
242+ {
243+ sk . Close ( ) ;
244+ sk . Dispose ( ) ;
245+ }
237246 socket = null ;
238247 SetEnables ( ) ;
239- loop . Cancel ( ) ;
240- var nloop = loop ;
241- loop = null ;
242- Task . Run ( async ( ) =>
248+ try
243249 {
244- try
245- {
246- await Task . WhenAll ( loops ) ;
247- }
248- finally
250+ loop . Cancel ( ) ;
251+ var nloop = loop ;
252+ var nloops = loops ;
253+ loop = null ;
254+ loops = new List < Task > ( ) ;
255+ Task . Run ( async ( ) =>
249256 {
250- nloop . Dispose ( ) ;
251- }
252- } ) ;
257+ try
258+ {
259+ await Task . WhenAll ( nloops ) ;
260+ }
261+ finally
262+ {
263+ nloop . Dispose ( ) ;
264+ }
265+ } ) ;
266+ }
267+ catch ( Exception ex )
268+ {
269+ LogError ( ex ) ;
270+ }
253271 return ;
254272 }
255273 var ip = ParseIp ( UDP_Local_ip . Text ) ;
@@ -268,15 +286,17 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
268286
269287 try
270288 {
271- socket = new Socket ( AddressFamily . InterNetwork , SocketType . Dgram , ProtocolType . Udp ) ;
272- socket . Bind ( new IPEndPoint ( ip , port ) ) ;
289+ var uc = new UdpClient ( new IPEndPoint ( ip , port ) ) ;
290+ socket = uc ;
291+ //socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
292+ //socket.Bind(new IPEndPoint(ip, port));
273293
274294 LogSystem ( $ "Open on {{ { ip } :{ port } }}") ;
275295
276296 SetEnables ( ) ;
277297 nowType = NowType . Udp ;
278298
279- socket . EnableBroadcast = UDP_type_broadcast . IsChecked ?? false ;
299+ uc . EnableBroadcast = UDP_type_broadcast . IsChecked ?? false ;
280300
281301 if ( UDP_type_broadcast . IsChecked ?? false )
282302 {
@@ -286,31 +306,31 @@ private void Open_Button_Click(object sender, RoutedEventArgs e)
286306 if ( UDP_type_multicast . IsChecked ?? false )
287307 {
288308 var mip = ParseIp ( UDP_multicast_ip . Text ) ;
289- var opt = new MulticastOption ( mip , ip ) ;
290- socket . SetSocketOption ( SocketOptionLevel . IP , SocketOptionName . AddMembership , opt ) ;
309+ //var opt = new MulticastOption(mip, ip);
310+ uc . JoinMulticastGroup ( mip ) ;
311+ //socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, opt);
291312
292313 LogSystem ( $ "Join Multicast Group {{ { mip } }}") ;
293314 }
294315
295316 loop = new CancellationTokenSource ( ) ;
296317 loops . Add ( Task . Run ( ( ) =>
297318 {
298- while ( socket != null )
319+ while ( socket != null || socket != uc )
299320 {
300- var buffer = new byte [ 1024 ] ;
301-
302321 try
303322 {
304- EndPoint ep = new IPEndPoint ( IPAddress . Any , 0 ) ;
305- socket . ReceiveFrom ( buffer , SocketFlags . None , ref ep ) ;
323+ var ep = new IPEndPoint ( IPAddress . Any , 0 ) ;
324+
325+ var buffer = uc . Receive ( ref ep ) ;
306326
307327 var str = Encoding . Default . GetString ( buffer ) ;
308328
309329 Log ( ep , str , true ) ;
310330 }
311331 catch ( Exception ex )
312332 {
313- if ( socket == null )
333+ if ( socket == null || socket != uc )
314334 {
315335 LogSystem ( "Closed" ) ;
316336 }
@@ -350,7 +370,15 @@ private void Send_Button_Click(object sender, RoutedEventArgs e)
350370 try
351371 {
352372 var @byte = Encoding . Default . GetBytes ( new TextRange ( Send_Msg . Document . ContentStart , Send_Msg . Document . ContentEnd ) . Text ) ;
353- socket . SendTo ( @byte , new IPEndPoint ( ip , port ) ) ;
373+ if ( socket is UdpClient uc )
374+ {
375+ uc . Send ( @byte , @byte . Length , new IPEndPoint ( ip , port ) ) ;
376+ }
377+ else
378+ {
379+ throw new SocketException ( ) ;
380+ }
381+ //socket.SendTo(@byte, new IPEndPoint(ip, port));
354382 Log ( new IPEndPoint ( ip , port ) , new TextRange ( Send_Msg . Document . ContentStart , Send_Msg . Document . ContentEnd ) . Text ) ;
355383 }
356384 catch ( Exception ex )
@@ -363,12 +391,16 @@ private void Send_Button_Click(object sender, RoutedEventArgs e)
363391
364392 private void UDP_type_broadcast_Checked ( object sender , RoutedEventArgs e )
365393 {
366- UDP_Target_ip . Items . Add ( UDP_Target_ip_broadcast ) ;
394+ UDP_Target_ip . Items . Insert ( 0 , UDP_Target_ip_broadcast ) ;
395+ UDP_Target_ip . Items . Remove ( UDP_Target_ip_localhost ) ;
396+ UDP_Target_ip . SelectedIndex = 0 ;
367397 }
368398
369399 private void UDP_type_broadcast_UnChecked ( object sender , RoutedEventArgs e )
370400 {
371401 UDP_Target_ip . Items . Remove ( UDP_Target_ip_broadcast ) ;
402+ UDP_Target_ip . Items . Insert ( 0 , UDP_Target_ip_localhost ) ;
403+ UDP_Target_ip . SelectedIndex = 0 ;
372404 }
373405
374406 private void UDP_Target_ip_Loaded ( object sender , RoutedEventArgs e )
@@ -405,5 +437,17 @@ private void Clear_Send_Button_Click(object sender, RoutedEventArgs e)
405437 {
406438 Send_Msg . Document . Blocks . Clear ( ) ;
407439 }
440+
441+ private void UDP_Local_ip_Loaded ( object sender , RoutedEventArgs e )
442+ {
443+ UDP_Local_ip . Items . Clear ( ) ;
444+ var host = Dns . GetHostEntry ( Dns . GetHostName ( ) ) ;
445+ foreach ( var ip in host . AddressList . Select ( ip => ip . ToString ( ) ) . Distinct ( ) )
446+ {
447+ UDP_Local_ip . Items . Add ( ip ) ;
448+ }
449+ UDP_Local_ip . Items . Insert ( 0 , UDP_Local_ip_localhost ) ;
450+ UDP_Local_ip . SelectedIndex = 0 ;
451+ }
408452 }
409453}
0 commit comments