Created
November 11, 2010 07:46
-
-
Save noqisofon/672167 to your computer and use it in GitHub Desktop.
へにょへにょな UDP 受信側窓クラス。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Drawing; | |
using System.Net; | |
using System.Net.Sockets; | |
using System.Text; | |
using System.Windows.Forms; | |
namespace demo.udpserver.app.widgets { | |
/// <summary> | |
/// | |
/// </summary> | |
public partial class UDPServerForm : Form { | |
/// <summary> | |
/// | |
/// </summary> | |
private IPEndPoint remote_endpoint_; | |
/// <summary> | |
/// | |
/// </summary> | |
public UDPServerForm() { | |
InitializeComponent(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void UDPServerForm_Load(object sender, EventArgs ergs) { | |
this.remote_endpoint_ = new IPEndPoint( IPAddress.Any, 0 ); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void UDPServerForm_Shown(object sender, EventArgs ergs) { | |
backgroundWorker.RunWorkerAsync(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="fcergs"></param> | |
private void UDPServerForm_FormClosing(object sender, FormClosingEventArgs fcergs) { | |
if ( fcergs.CloseReason == CloseReason.UserClosing ) { | |
fcergs.Cancel = true; | |
this.Visible = false; | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void notifyIcon_DoubleClick(object sender, EventArgs ergs) { | |
this.Visible = true; | |
if ( this.WindowState == FormWindowState.Minimized ) { | |
this.WindowState = FormWindowState.Normal; | |
} | |
this.Activate(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void tsmiNotifyIconQuit_Click(object sender, EventArgs ergs) { | |
backgroundWorker.CancelAsync(); | |
Application.Exit(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="ergs"></param> | |
private void backgroundWorker_DoWork(object sender, DoWorkEventArgs dwergs) { | |
try { | |
UdpClient udp_receiver = new UdpClient( 52292 ); | |
int report = 0; | |
while ( true ) { | |
byte[] received_bytes = udp_receiver.Receive( ref this.remote_endpoint_ ); | |
backgroundWorker.ReportProgress( report, Encoding.UTF8.GetString( received_bytes ) ); | |
byte[] send_bytes = Encoding.UTF8.GetBytes( "<response><result status=\"200\">OK</result></response>" ); | |
udp_receiver.Send( send_bytes, send_bytes.Length, this.remote_endpoint_ ); | |
++report; | |
} | |
} catch ( SocketException se ) { | |
dwergs.Result = se; | |
backgroundWorker.CancelAsync(); | |
} | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="rwcergs"></param> | |
private void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs rwcergs) { | |
StringBuilder resulttext_builder = new StringBuilder( tbResult.Text ); | |
resulttext_builder.AppendLine( rwcergs.Result.ToString() ); | |
if ( rwcergs.Cancelled ) { | |
resulttext_builder.AppendLine( rwcergs.Error.ToString() ); | |
} | |
tbResult.Text = resulttext_builder.ToString(); | |
} | |
/// <summary> | |
/// | |
/// </summary> | |
/// <param name="sender"></param> | |
/// <param name="pcergs"></param> | |
private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs pcergs) { | |
StringBuilder resulttext_builder = new StringBuilder( tbResult.Text ); | |
resulttext_builder.AppendLine( pcergs.UserState.ToString() ); | |
progressBar.PerformStep(); | |
tbResult.Text = resulttext_builder.ToString(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment