Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1190 → Rev 1191

/rebootd/trunk/client.net/MainForm.cs
0,0 → 1,87
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
 
namespace reboot_client
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
 
private void button1_Click(object sender, EventArgs e)
{
string host = hostBox.Text;
if (host.Trim() == string.Empty)
{
MessageBox.Show(this, "Please enter a host name or IP address",
"reboot-client", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
 
IPAddress addr;
try
{
addr = IPAddress.Parse(host);
}
catch(FormatException)
{
try
{
IPHostEntry hostEntry = Dns.GetHostEntry(host);
addr = hostEntry.AddressList[0];
}
catch (Exception ex)
{
MessageBox.Show(this, "Cannot get IP address for host: " + ex.Message,
"reboot-client", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
 
int port;
try
{
port = int.Parse(portBox.Text);
if (port < 1 || port > 65535) throw new Exception();
}
catch (Exception)
{
MessageBox.Show(this, "Port must be an integer between 1 and 65535",
"reboot-client", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
 
string password = passwordBox.Text;
if (password == string.Empty)
{
MessageBox.Show(this, "Please enter a password",
"reboot-client", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
 
if (MessageBox.Show(this,
string.Format("Do you really want to REBOOT {0}?", addr),
"reboot-client", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
!= DialogResult.Yes)
{
return;
}
 
Socket soc = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
 
soc.SendTo(Encoding.ASCII.GetBytes(password), new IPEndPoint(addr, port));
 
MessageBox.Show(this, "Reboot command sent",
"reboot-client", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}