|
public class UdpKeyListener
{
private const int listenPort = 11053;
private int m_exitCode = 0;
private int m_messageCount = 0;
private System.IntPtr m_handle = System.IntPtr.Zero;
//Asynchronous Non-Blocking Background Worker
private System.ComponentModel.BackgroundWorker m_BackgroundWorker = new System.ComponentModel.BackgroundWorker();
public event OnMessageReceivedEventHandler OnMessageReceived;
public delegate void OnMessageReceivedEventHandler(object Sender, int msgCount, int iRet, string msg);
private void m_BackgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
int iRet = 1;
System.Net.Sockets.UdpClient listener = new System.Net.Sockets.UdpClient(listenPort);
System.Net.IPEndPoint groupEP = new System.Net.IPEndPoint(System.Net.IPAddress.Any, listenPort);
try {
while (!(((iRet == 0) || (iRet == -1)))) {
byte[] bytes = listener.Receive(ref groupEP);
//GMTMXX
if (bytes.Length > 6 && bytes[0] == 71 && bytes[1] == 77 && bytes[2] == 84 && bytes[3] == 77 && bytes[4] == 88 && bytes[5] == 88) {
m_BackgroundWorker.ReportProgress(iRet,System.Convert.ToString(System.Text.Encoding.ASCII.GetString(bytes, 6, bytes.Length - 6)));
}
}
} catch (System.Exception ex) {
System.Diagnostics.Debug.Write(ex.ToString());
iRet = -1;
} finally {
listener.Close();
}
}
private void m_BackgroundWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if ((((e.UserState != null)) && (typeof(string).Equals(e.UserState.GetType()))))
{
m_messageCount += 1;
int iRet = e.ProgressPercentage;
string msg =System.Convert.ToString(e.UserState);
if ((((OnMessageReceived != null)) && (OnMessageReceived.GetInvocationList().Length > 0))) {
if (OnMessageReceived != null) {
OnMessageReceived(this, m_messageCount, iRet, msg);
}
}
}
}
public UdpKeyListener()
{
m_BackgroundWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(m_BackgroundWorker_DoWork);
m_BackgroundWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(m_BackgroundWorker_ProgressChanged);
m_BackgroundWorker.WorkerReportsProgress = true;
m_BackgroundWorker.WorkerSupportsCancellation = true;
m_BackgroundWorker.RunWorkerAsync();
}
public int exitCode {
get { return m_exitCode; }
}
public int messageCount {
get { return m_messageCount; }
}
public System.IntPtr handle
{
get { return m_handle; }
}
public static void Send(string msg)
{
System.Net.Sockets.UdpClient xClient = new System.Net.Sockets.UdpClient();
byte[] bytes = System.Text.Encoding.ASCII.GetBytes("GMTMXX" + msg);
xClient.Send(bytes, bytes.Length, new System.Net.IPEndPoint(System.Net.IPAddress.Parse("255.255.255.255"), listenPort));
xClient.Close();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.