Skip to content

Commit 05f656a

Browse files
committed
add: udp服务端
1 parent cf96773 commit 05f656a

5 files changed

Lines changed: 250 additions & 0 deletions

File tree

llcom/Model/Settings.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,5 +571,8 @@ public void SetQuickListNameNow(string name)
571571

572572
private int _tcpServerPort = 2333;
573573
public int tcpServerPort { get { return _tcpServerPort; } set { _tcpServerPort = value; Save(); } }
574+
575+
private int _udpServerPort = 2333;
576+
public int udpServerPort { get { return _udpServerPort; } set { _udpServerPort = value; Save(); } }
574577
}
575578
}

llcom/Pages/UdpLocalPage.xaml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<Page
2+
x:Class="llcom.Pages.UdpLocalPage"
3+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:adonisExtensions="clr-namespace:AdonisUI.Extensions;assembly=AdonisUI"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:local="clr-namespace:llcom.Pages"
8+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9+
Title="TcpLocalPage"
10+
adonisExtensions:RippleExtension.FadeInDuration="0"
11+
adonisExtensions:RippleExtension.FadeOutDuration="0"
12+
d:DesignHeight="480"
13+
d:DesignWidth="400"
14+
Loaded="Page_Loaded"
15+
mc:Ignorable="d">
16+
<Page.Resources>
17+
<local:boolVisibe x:Key="boolVisibeConverter" />
18+
<local:boolNotVisibe x:Key="boolNotVisibeConverter" />
19+
<local:boolNot x:Key="boolNotConverter" />
20+
</Page.Resources>
21+
<Grid Name="MainGrid">
22+
<Grid.RowDefinitions>
23+
<RowDefinition Height="35" />
24+
<RowDefinition Height="35" />
25+
</Grid.RowDefinitions>
26+
<Grid Margin="0,5,0,0">
27+
<Grid.ColumnDefinitions>
28+
<ColumnDefinition Width="2*" />
29+
<ColumnDefinition Width="auto" />
30+
<ColumnDefinition Width="70" />
31+
</Grid.ColumnDefinitions>
32+
<ComboBox
33+
Name="IpListComboBox"
34+
IsEditable="True"
35+
IsEnabled="{Binding IsConnected, Converter={StaticResource boolNotConverter}}" />
36+
<Button
37+
x:Name="RefreshIpButton"
38+
Grid.Column="1"
39+
Margin="5,0,0,0"
40+
Click="RefreshIpButton_Click"
41+
Content="{DynamicResource RefreshIpList}"
42+
IsEnabled="{Binding IsConnected, Converter={StaticResource boolNotConverter}}" />
43+
<TextBox
44+
x:Name="IpPortTextBox"
45+
Grid.Column="2"
46+
Margin="5,0,0,0"
47+
IsEnabled="{Binding ElementName=RefreshIpButton, Path=IsEnabled}"
48+
Text="{Binding tcpServerPort}" />
49+
</Grid>
50+
<Button
51+
x:Name="StopListenButton"
52+
Grid.Row="1"
53+
Margin="0,5,0,0"
54+
Click="StopListenButton_Click"
55+
Content="{DynamicResource StopListen}"
56+
Visibility="{Binding IsConnected, Converter={StaticResource boolVisibeConverter}}" />
57+
<Button
58+
x:Name="ListenButton"
59+
Grid.Row="1"
60+
Margin="0,5,0,0"
61+
Click="ListenButton_Click"
62+
Content="{DynamicResource StartListen}"
63+
Visibility="{Binding IsConnected, Converter={StaticResource boolNotVisibeConverter}}" />
64+
</Grid>
65+
</Page>

llcom/Pages/UdpLocalPage.xaml.cs

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

llcom/View/MainWindow.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
171171
//本地tcp服务器
172172
tcpLocalTestFrame.Navigate(new Uri("Pages/TcpLocalPage.xaml", UriKind.Relative));
173173

174+
//本地udp服务器
175+
udpLocalTestFrame.Navigate(new Uri("Pages/UdpLocalPage.xaml", UriKind.Relative));
176+
174177
//mqtt测试页面
175178
MqttTestFrame.Navigate(new Uri("Pages/MqttTestPage.xaml", UriKind.Relative));
176179

llcom/llcom.csproj

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,9 @@
259259
<Compile Include="Pages\tcpTest.xaml.cs">
260260
<DependentUpon>tcpTest.xaml</DependentUpon>
261261
</Compile>
262+
<Compile Include="Pages\UdpLocalPage.xaml.cs">
263+
<DependentUpon>UdpLocalPage.xaml</DependentUpon>
264+
</Compile>
262265
<Compile Include="Tools\InputDialog.cs" />
263266
<Compile Include="Tools\Win32.cs" />
264267
<Compile Include="View\InputDialogWindow.xaml.cs">
@@ -321,6 +324,10 @@
321324
<SubType>Designer</SubType>
322325
<Generator>MSBuild:Compile</Generator>
323326
</Page>
327+
<Page Include="Pages\UdpLocalPage.xaml">
328+
<SubType>Designer</SubType>
329+
<Generator>MSBuild:Compile</Generator>
330+
</Page>
324331
<Page Include="View\InputDialogWindow.xaml">
325332
<SubType>Designer</SubType>
326333
<Generator>MSBuild:Compile</Generator>

0 commit comments

Comments
 (0)