Speedy

Speedy

  • NA
  • 1
  • 0

UDP communication

Dec 18 2006 9:38 AM

I'm busy making an webbased udp communications application for communication with a hardware solution, for now everything works fine the messages i send from my webform arriving at the terminal and the terminals is responding. I can see the udp packages because i've installed ethereal.

Now i want the reply message from the udp message i send be placed in a label txtPing, below is my source code but how can i realize this.



public
class UDPListener

{

private const int listenPort = 2002;

private static void StartListener()

{

UdpClient listener = new UdpClient(listenPort);

IPEndPoint groupEP = new IPEndPoint(IPAddress.Any, listenPort);

try

{

byte[] bytes = listener.Receive(ref groupEP);

Console.WriteLine("Received broadcast from {0} :\n {1}\n",

groupEP.ToString(),

Encoding.ASCII.GetString(bytes, 0, bytes.Length));

}

catch (Exception e)

{

Console.WriteLine(e.ToString());

}

}

}

 

 

 

public partial class _Default : System.Web.UI.Page

{

protected void Page_Load(object sender, EventArgs e)

{

}

protected void Button1_Click(object sender, EventArgs e)

{

string ip = Ipadres1.Text;

string Poort = Poort1.Text;

int SendPoort = Convert.ToInt32(Poort);

UdpClient udpClient = new UdpClient();

// Bericht verzenden.

Byte[] sendBytes = Encoding.ASCII.GetBytes(Command1.Text + (char)13);

try

{

udpClient.Send(sendBytes, sendBytes.Length, ip, SendPoort);

}

catch (Exception error)

{

Console.WriteLine(error.ToString());

}

}

 

protected void Button2_Click(object sender, EventArgs e)

{

string ip = Ipadres1.Text;

lblStatus.Text = null;

txtPing.Text = null;

try

{

Ping ping = new Ping();

PingReply pingreply = ping.Send(Ipadres1.Text);

txtPing.Text += "<br>&nbsp;&nbsp; Ip adres : " + pingreply.Address + "\r<br>";

txtPing.Text += "&nbsp;&nbsp; Tijd : " + pingreply.RoundtripTime + " ms \r<br>";

txtPing.Text += "&nbsp;&nbsp; TTL (Time To Live): " + pingreply.Options.Ttl + " ms \r<br>";

txtPing.Text += "&nbsp;&nbsp; Buffer Size: " + pingreply.Buffer.Length.ToString() + " bytes \r";

}

catch (Exception err)

{

lblStatus.Text = err.Message;

}

 

}

}