📄 clientform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace HeartbeatClient
{
/// <summary>
/// The ClientForm contains the Functions for sending the Heartbeat
/// via UDP local.
/// </summary>
public class ClientForm : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private System.Windows.Forms.CheckBox chkSendHeardbeat;
private System.Timers.Timer heartbeatTimer = new System.Timers.Timer();
private IPEndPoint ipep;
/// <summary>
/// Constructor. Initializes the Form and the Timer
/// </summary>
public ClientForm()
{
InitializeComponent();
// Initialize the Timer for 1s Interval and start it.
heartbeatTimer.Interval = 1000;
heartbeatTimer.AutoReset = true;
heartbeatTimer.Elapsed += new System.Timers.ElapsedEventHandler(heartbeatTimer_Elapsed);
heartbeatTimer.Start();
// Initialize IPEndPoint for loopback network adapter on Port 10000.
ipep = new IPEndPoint(IPAddress.Loopback, 10000);
}
/// <summary>
/// Dispose used Ressources.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Vom Windows Form-Designer generierter Code
/// <summary>
/// Erforderliche Methode f黵 die Designerunterst黷zung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor ge鋘dert werden.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(ClientForm));
this.chkSendHeardbeat = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// chkSendHeardbeat
//
this.chkSendHeardbeat.Checked = true;
this.chkSendHeardbeat.CheckState = System.Windows.Forms.CheckState.Checked;
this.chkSendHeardbeat.Location = new System.Drawing.Point(8, 16);
this.chkSendHeardbeat.Name = "chkSendHeardbeat";
this.chkSendHeardbeat.Size = new System.Drawing.Size(272, 24);
this.chkSendHeardbeat.TabIndex = 0;
this.chkSendHeardbeat.Text = "Send Heartbeat";
//
// ClientForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(292, 48);
this.Controls.Add(this.chkSendHeardbeat);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "ClientForm";
this.Text = "ClientForm";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// Main function of this Application. Creates the Form.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new ClientForm());
}
/// <summary>
/// Handler for timer event. Sends the Heartbeat if the Checkbox is checked.
/// </summary>
/// <param name="sender">Sender that occured the event; not used.</param>
/// <param name="e">Arguments for the Elapsed Event; not used.</param>
private void heartbeatTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if(chkSendHeardbeat.Checked)
{
SendUdpPacket();
}
}
/// <summary>
/// Sends the UDP Heartbeat Packet.
/// </summary>
private void SendUdpPacket()
{
byte[] data = new byte[1024];
// Create UDP Socket
Socket udpClientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
// Send Application Title (Window Title) as the Data
data = Encoding.ASCII.GetBytes(this.Text);
// Send it ...
udpClientSocket.SendTo(data,0,data.Length,SocketFlags.None,ipep);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -