custom port setting

remotes/origin/master
Pyunghwa Kim 3 years ago
parent 37a840cf3f
commit 4abdf11c01
  1. BIN
      WoL/Waker/Designcontest-Ecommerce-Business-Idea.ico
  2. 163
      WoL/Waker/MainForm.Designer.cs
  3. 39
      WoL/Waker/MainForm.cs
  4. 14
      WoL/Waker/Settings.Designer.cs
  5. 3
      WoL/Waker/Settings.settings
  6. 8
      WoL/Waker/Waker.csproj
  7. 29
      WoL/WoL/WoL.cs

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 KiB

@ -29,75 +29,96 @@ namespace Waker
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm)); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.AddressTextBox = new System.Windows.Forms.TextBox(); this.AddressTextBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label();
this.MacTextBox = new System.Windows.Forms.TextBox(); this.MacTextBox = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label();
this.WakeButton = new System.Windows.Forms.Button(); this.WakeButton = new System.Windows.Forms.Button();
this.SuspendLayout(); this.label3 = new System.Windows.Forms.Label();
// this.PortTextBox = new System.Windows.Forms.TextBox();
// AddressTextBox this.SuspendLayout();
// //
this.AddressTextBox.Location = new System.Drawing.Point(67, 14); // AddressTextBox
this.AddressTextBox.Name = "AddressTextBox"; //
this.AddressTextBox.Size = new System.Drawing.Size(198, 23); this.AddressTextBox.Location = new System.Drawing.Point(67, 14);
this.AddressTextBox.TabIndex = 0; this.AddressTextBox.Name = "AddressTextBox";
// this.AddressTextBox.Size = new System.Drawing.Size(198, 23);
// label1 this.AddressTextBox.TabIndex = 0;
// //
this.label1.AutoSize = true; // label1
this.label1.Location = new System.Drawing.Point(12, 18); //
this.label1.Name = "label1"; this.label1.AutoSize = true;
this.label1.Size = new System.Drawing.Size(49, 15); this.label1.Location = new System.Drawing.Point(12, 18);
this.label1.TabIndex = 1; this.label1.Name = "label1";
this.label1.Text = "Address"; this.label1.Size = new System.Drawing.Size(49, 15);
// this.label1.TabIndex = 1;
// MacTextBox this.label1.Text = "Address";
// //
this.MacTextBox.Location = new System.Drawing.Point(67, 43); // MacTextBox
this.MacTextBox.Name = "MacTextBox"; //
this.MacTextBox.Size = new System.Drawing.Size(198, 23); this.MacTextBox.Location = new System.Drawing.Point(67, 43);
this.MacTextBox.TabIndex = 1; this.MacTextBox.Name = "MacTextBox";
// this.MacTextBox.Size = new System.Drawing.Size(198, 23);
// label2 this.MacTextBox.TabIndex = 1;
// //
this.label2.AutoSize = true; // label2
this.label2.Location = new System.Drawing.Point(21, 47); //
this.label2.Name = "label2"; this.label2.AutoSize = true;
this.label2.Size = new System.Drawing.Size(30, 15); this.label2.Location = new System.Drawing.Point(21, 47);
this.label2.TabIndex = 1; this.label2.Name = "label2";
this.label2.Text = "mac"; this.label2.Size = new System.Drawing.Size(30, 15);
// this.label2.TabIndex = 1;
// WakeButton this.label2.Text = "mac";
// //
this.WakeButton.Location = new System.Drawing.Point(271, 12); // WakeButton
this.WakeButton.Name = "WakeButton"; //
this.WakeButton.Size = new System.Drawing.Size(102, 54); this.WakeButton.Location = new System.Drawing.Point(271, 12);
this.WakeButton.TabIndex = 2; this.WakeButton.Name = "WakeButton";
this.WakeButton.Text = "WoL"; this.WakeButton.Size = new System.Drawing.Size(102, 83);
this.WakeButton.UseVisualStyleBackColor = true; this.WakeButton.TabIndex = 3;
this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click); this.WakeButton.Text = "WoL";
// this.WakeButton.UseVisualStyleBackColor = true;
// MainForm this.WakeButton.Click += new System.EventHandler(this.WakeButton_Click);
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); // label3
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; //
this.BackColor = System.Drawing.Color.White; this.label3.AutoSize = true;
this.ClientSize = new System.Drawing.Size(383, 79); this.label3.Location = new System.Drawing.Point(19, 76);
this.Controls.Add(this.WakeButton); this.label3.Name = "label3";
this.Controls.Add(this.label2); this.label3.Size = new System.Drawing.Size(34, 15);
this.Controls.Add(this.label1); this.label3.TabIndex = 3;
this.Controls.Add(this.MacTextBox); this.label3.Text = "ports";
this.Controls.Add(this.AddressTextBox); //
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle; // PortTextBox
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); //
this.MaximumSize = new System.Drawing.Size(399, 118); this.PortTextBox.Location = new System.Drawing.Point(67, 72);
this.MinimumSize = new System.Drawing.Size(399, 118); this.PortTextBox.Name = "PortTextBox";
this.Name = "MainForm"; this.PortTextBox.Size = new System.Drawing.Size(198, 23);
this.Text = "Waker"; this.PortTextBox.TabIndex = 2;
this.ResumeLayout(false); this.PortTextBox.Text = "7, 9";
this.PerformLayout(); //
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(383, 105);
this.Controls.Add(this.label3);
this.Controls.Add(this.PortTextBox);
this.Controls.Add(this.WakeButton);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.MacTextBox);
this.Controls.Add(this.AddressTextBox);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximumSize = new System.Drawing.Size(399, 144);
this.MinimumSize = new System.Drawing.Size(399, 144);
this.Name = "MainForm";
this.Text = "Waker";
this.ResumeLayout(false);
this.PerformLayout();
} }
@ -108,6 +129,8 @@ namespace Waker
private System.Windows.Forms.TextBox MacTextBox; private System.Windows.Forms.TextBox MacTextBox;
private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button WakeButton; private System.Windows.Forms.Button WakeButton;
} private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox PortTextBox;
}
} }

@ -16,6 +16,7 @@ namespace Waker
{ {
private string ip; private string ip;
private string mac; private string mac;
private string ports;
public MainForm() public MainForm()
{ {
@ -33,25 +34,33 @@ namespace Waker
if (!string.IsNullOrEmpty(Settings.Default.mac)) if (!string.IsNullOrEmpty(Settings.Default.mac))
mac = Settings.Default.mac; mac = Settings.Default.mac;
if (!string.IsNullOrEmpty(Settings.Default.ports))
ports = Settings.Default.ports;
FillInformation(); FillInformation();
AddressTextBox.KeyUp += TextBox_KeyUp; AddressTextBox.KeyUp += TextBox_KeyUp;
MacTextBox.KeyUp += TextBox_KeyUp; MacTextBox.KeyUp += TextBox_KeyUp;
PortTextBox.KeyUp += TextBox_KeyUp;
} }
private bool GetInformation() private bool GetInformation()
{ {
ip = AddressTextBox.Text.Replace(" ", ""); ip = AddressTextBox.Text.Replace(" ", "");
mac = MacTextBox.Text.Replace(" ", ""); mac = MacTextBox.Text.Replace(" ", "");
ports = PortTextBox.Text.Replace(" ", "");
if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(mac)) if (string.IsNullOrEmpty(ip) || string.IsNullOrEmpty(mac))
return false; return false;
AddressTextBox.Text = ip; AddressTextBox.Text = ip;
MacTextBox.Text = mac; MacTextBox.Text = mac;
PortTextBox.Text = ports;
Settings.Default.ip = ip; Settings.Default.ip = ip;
Settings.Default.mac = mac; Settings.Default.mac = mac;
Settings.Default.ports = ports;
Settings.Default.Save(); Settings.Default.Save();
return true; return true;
@ -64,6 +73,9 @@ namespace Waker
if (!string.IsNullOrEmpty(mac)) if (!string.IsNullOrEmpty(mac))
MacTextBox.Text = mac; MacTextBox.Text = mac;
if (!string.IsNullOrEmpty(ports))
PortTextBox.Text = ports;
} }
private void WakeButton_Click(object sender, EventArgs e) private void WakeButton_Click(object sender, EventArgs e)
@ -76,7 +88,32 @@ namespace Waker
try try
{ {
WoL.Wake(ip, mac); string portsString = ports.Replace(" ", "");
if (string.IsNullOrEmpty(portsString))
{
WoL.Wake(ip, mac);
return;
}
string[] portStringArray = portsString.Split(',');
if (portStringArray == null || portStringArray.Length < 1 || string.IsNullOrEmpty(portStringArray[0]))
{
WoL.Wake(ip, mac);
return;
}
int[] portArray = portStringArray.Select(int.Parse).ToArray();
if (portArray == null || portArray.Length< 1)
{
WoL.Wake(ip, mac);
return;
}
WoL.Wake(ip, mac, portArray);
} catch (Exception ex) } catch (Exception ex)
{ {
MessageBox.Show(ex.Message); MessageBox.Show(ex.Message);

@ -12,7 +12,7 @@ namespace Waker {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.10.0.0")] [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.2.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
@ -46,5 +46,17 @@ namespace Waker {
this["mac"] = value; this["mac"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("7, 9")]
public string ports {
get {
return ((string)(this["ports"]));
}
set {
this["ports"] = value;
}
}
} }
} }

@ -8,5 +8,8 @@
<Setting Name="mac" Type="System.String" Scope="User"> <Setting Name="mac" Type="System.String" Scope="User">
<Value Profile="(Default)" /> <Value Profile="(Default)" />
</Setting> </Setting>
<Setting Name="ports" Type="System.String" Scope="User">
<Value Profile="(Default)">7, 9</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

@ -4,8 +4,16 @@
<OutputType>WinExe</OutputType> <OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>netcoreapp3.1</TargetFramework>
<UseWindowsForms>true</UseWindowsForms> <UseWindowsForms>true</UseWindowsForms>
<GeneratePackageOnBuild>False</GeneratePackageOnBuild>
<AssemblyVersion>1.0.0.0</AssemblyVersion>
<StartupObject>Waker.Program</StartupObject>
<ApplicationIcon>Designcontest-Ecommerce-Business-Idea.ico</ApplicationIcon>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Content Include="Designcontest-Ecommerce-Business-Idea.ico" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\WoL\WakeOnLan.csproj" /> <ProjectReference Include="..\WoL\WakeOnLan.csproj" />
</ItemGroup> </ItemGroup>

@ -9,18 +9,37 @@ namespace WakeOnLan
{ {
public class WoL public class WoL
{ {
private const int WOL_PACKET_LEN = 102; private static readonly int WOL_PACKET_LEN = 102;
private static readonly int[] DEFAULT_PORTS = new int[] { 7, 9 };
public static void Wake(string ip, string macAddress) public static void Wake(string ip, string macAddress, int[] ports = null)
{ {
byte[] wolBuffer = GetWolPacket(macAddress); byte[] wolBuffer = GetWolPacket(macAddress);
UdpClient udp = new UdpClient(); UdpClient udp = new UdpClient();
udp.EnableBroadcast = true; udp.EnableBroadcast = true;
IPAddress ipAddress = IPAddress.Parse(ip); IPAddress ipAddress = GetIpAddress(ip);
udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 7); if (ports == null || ports.Length < 1)
udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), 9); ports = DEFAULT_PORTS;
foreach (int port in ports)
{
udp.Send(wolBuffer, wolBuffer.Length, ipAddress.ToString(), port);
}
}
private static IPAddress GetIpAddress(string ip)
{
Regex regex = new Regex(@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$");
if (regex.IsMatch(ip))
return IPAddress.Parse(ip);
IPHostEntry hostEntry = Dns.GetHostEntry(ip);
if (hostEntry.AddressList.Length < 1)
throw new Exception("invalid ip address");
return hostEntry.AddressList[0];
} }
private static byte[] GetWolPacket(string macAddress) private static byte[] GetWolPacket(string macAddress)

Loading…
Cancel
Save