Posts Tagged ‘Networking’
Performing PING from C#
Most of the users will be doing ping to a system to check whether it is running or responding or not. First thing what they will do is start a command promt and type the ping command. But doing it in C# is also very easy. We can implement in our programs and use to check whether system are responding. It can be used in various network application
public partial class frmPingSystem : Form { public frmPingSystem(){InitializeComponent();}private void pingButton_Click(object sender, EventArgs e){int timeout = 12000;Ping ping = new Ping();byte[] buffer = Encoding.ASCII.GetBytes("ooooooooooooooooooooooo");PingOptions options = new PingOptions(64,true);AutoResetEvent waiter = new AutoResetEvent(false);ping.PingCompleted += new PingCompletedEventHandler(ping_PingCompleted);ping.SendAsync(addressTextBox.Text, timeout, buffer, options, null);}void ping_PingCompleted(object sender, PingCompletedEventArgs e){string output = "Status : " + e.Reply.Status.ToString();output += "\r\nAddress : " + e.Reply.Address.ToString();output += "\r\nRound Trip Time : " + e.Reply.RoundtripTime.ToString();output += "\r\nBuffer Length : " + e.Reply.Buffer.Length.ToString();output += "\r\nTime to live : " + e.Reply.Options.Ttl.ToString();output += "\r\nDont Framment : " + e.Reply.Options.DontFragment.ToString();resultTextBox.Text = output;} }
If you want to check the result in the same line where you are sending ping then you must use Send function in place SendAsync
Getting TCP/IP Statistic through C#
Getting the TCP/IP Statistic is very easy through C#. Since all the required methods and properties are there in framework. To get the TCP/IP Statistic we have to use the following nampespace
using System.Net.NetworkInformation;
Now to get the statistic we have to IPGlobalProperties class and extract the information out of it to our TCP/IP statistic object
IPGlobalProperties properties = IPGlobalProperties.GetIPGlobalProperties();
TcpStatistics tcpstat = null;
now with following line you will get all the details for IPv4
tcpstat = properties.GetTcpIPv4Statistics();
and to get the details of IPv6 you use following line
tcpstat = properties.GetTcpIPv6Statistics();
now we can get details from this object. Details like data sent, recieved, total connections and various other