|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net.Sockets; |
| 5 | +using System.Net; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Controls; |
| 10 | +using System.Windows.Data; |
| 11 | +using System.Windows.Documents; |
| 12 | +using System.Windows.Input; |
| 13 | +using System.Windows.Media; |
| 14 | +using System.Windows.Media.Imaging; |
| 15 | +using System.Windows.Navigation; |
| 16 | +using System.Windows.Shapes; |
| 17 | +using static llcom.Pages.SocketClientPage; |
| 18 | +using System.Net.NetworkInformation; |
| 19 | + |
| 20 | +namespace llcom.Pages |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// UdpLocalPage.xaml 的交互逻辑 |
| 24 | + /// </summary> |
| 25 | + [PropertyChanged.AddINotifyPropertyChangedInterface] |
| 26 | + public partial class UdpLocalPage : Page |
| 27 | + { |
| 28 | + public UdpLocalPage() |
| 29 | + { |
| 30 | + InitializeComponent(); |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | + public bool IsConnected { get; set; } = false; |
| 35 | + |
| 36 | + private static bool loaded = false; |
| 37 | + private void Page_Loaded(object sender, RoutedEventArgs e) |
| 38 | + { |
| 39 | + if (loaded) |
| 40 | + return; |
| 41 | + loaded = true; |
| 42 | + RefreshIp(); |
| 43 | + //绑定 |
| 44 | + MainGrid.DataContext = this; |
| 45 | + IpPortTextBox.DataContext = Tools.Global.setting; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// 刷新本机ip列表 |
| 50 | + /// </summary> |
| 51 | + private void RefreshIp() |
| 52 | + { |
| 53 | + IpListComboBox.Items.Clear(); |
| 54 | + IpListComboBox.Items.Add("0.0.0.0"); |
| 55 | + IpListComboBox.Items.Add("::"); |
| 56 | + try |
| 57 | + { |
| 58 | + string name = Dns.GetHostName(); |
| 59 | + IPAddress[] ipadrlist = Dns.GetHostAddresses(name); |
| 60 | + foreach (IPAddress ipa in ipadrlist) |
| 61 | + { |
| 62 | + if (ipa.AddressFamily == AddressFamily.InterNetwork) |
| 63 | + IpListComboBox.Items.Add(ipa.ToString()); |
| 64 | + } |
| 65 | + } |
| 66 | + catch { } |
| 67 | + IpListComboBox.SelectedIndex = 0; |
| 68 | + } |
| 69 | + private void ShowData(string title, byte[] data = null, bool send = false) |
| 70 | + { |
| 71 | + this.Dispatcher.Invoke(new Action(delegate |
| 72 | + { |
| 73 | + Tools.Logger.ShowDataRaw(new Tools.DataShowRaw |
| 74 | + { |
| 75 | + title = $"🗑 local udp server: {title}", |
| 76 | + data = data ?? new byte[0], |
| 77 | + color = send ? Brushes.DarkRed : Brushes.DarkGreen, |
| 78 | + }); |
| 79 | + })); |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + private UdpClient Server = null; |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// 开始监听服务器 |
| 87 | + /// </summary> |
| 88 | + /// <param name="ip"></param> |
| 89 | + /// <param name="port"></param> |
| 90 | + /// <returns></returns> |
| 91 | + private bool StartServer(string ip, int port) |
| 92 | + { |
| 93 | + if (Server != null) |
| 94 | + return false; |
| 95 | + IPAddress localAddr = IPAddress.Parse(ip); |
| 96 | + IPEndPoint IpEndPoint = new IPEndPoint(localAddr, port); |
| 97 | + Server = new UdpClient(IpEndPoint); |
| 98 | + |
| 99 | + var isV6 = ip.Contains(":"); |
| 100 | + ShowData($"🗑 {(isV6 ? "[" : "")}{ip}{(isV6 ? "]" : "")}:{port}"); |
| 101 | + |
| 102 | + AsyncCallback newConnectionCb = null; |
| 103 | + newConnectionCb = new AsyncCallback((ar) => |
| 104 | + { |
| 105 | + try |
| 106 | + { |
| 107 | + UdpClient u = ((UdpState)(ar.AsyncState)).u; |
| 108 | + IPEndPoint e = ((UdpState)(ar.AsyncState)).e; |
| 109 | + |
| 110 | + byte[] receiveBytes = u.EndReceive(ar, ref e); |
| 111 | + ShowData($"{e.Address}:{e.Port}", receiveBytes); |
| 112 | + Server.BeginReceive(newConnectionCb, ar.AsyncState); |
| 113 | + } |
| 114 | + catch { } |
| 115 | + }); |
| 116 | + UdpState s = new UdpState(); |
| 117 | + s.e = IpEndPoint; |
| 118 | + s.u = Server; |
| 119 | + Server.BeginReceive(newConnectionCb, s); |
| 120 | + |
| 121 | + return true; |
| 122 | + } |
| 123 | + |
| 124 | + /// <summary> |
| 125 | + /// 关闭服务器,断开所有连接 |
| 126 | + /// </summary> |
| 127 | + private void StopServer() |
| 128 | + { |
| 129 | + Server?.Close(); |
| 130 | + Server?.Dispose(); |
| 131 | + Server = null; |
| 132 | + } |
| 133 | + |
| 134 | + private void RefreshIpButton_Click(object sender, RoutedEventArgs e) |
| 135 | + { |
| 136 | + RefreshIp(); |
| 137 | + } |
| 138 | + |
| 139 | + private void ListenButton_Click(object sender, RoutedEventArgs e) |
| 140 | + { |
| 141 | + int port; |
| 142 | + if (int.TryParse(IpPortTextBox.Text, out port)) |
| 143 | + { |
| 144 | + try |
| 145 | + { |
| 146 | + IsConnected = StartServer(IpListComboBox.Text, port); |
| 147 | + } |
| 148 | + catch (Exception err) |
| 149 | + { |
| 150 | + MessageBox.Show(err.Message); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + private void StopListenButton_Click(object sender, RoutedEventArgs e) |
| 156 | + { |
| 157 | + try |
| 158 | + { |
| 159 | + StopServer(); |
| 160 | + IsConnected = false; |
| 161 | + ShowData($"🚫 server closed"); |
| 162 | + } |
| 163 | + catch { } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + public struct UdpState |
| 168 | + { |
| 169 | + public UdpClient u; |
| 170 | + public IPEndPoint e; |
| 171 | + } |
| 172 | +} |
0 commit comments