Next step of programming

Just another WordPress.com weblog

Archive for the ‘Networking’ Category

Netstat in C#

without comments

Check out how to perform Netstat command functions of DOS in C#. It will list you all the current network connections of your System


using System;
using System.Net;
using System.Net.NetworkInformation;

namespace MyNetstat
{
class Program    
{
static void Main(string[] args)
{
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();

IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();

foreach (TcpConnectionInformation info in tcpConnections)
{
Console.WriteLine("Local : " + info.LocalEndPoint.Address.ToString()
+ ":" + info.LocalEndPoint.Port.ToString()
+ "\nRemote : " + info.RemoteEndPoint.Address.ToString()
+ ":" + info.RemoteEndPoint.Port.ToString()
+ "\nState : " + info.State.ToString() + "\n\n");
}
Console.ReadLine();
}
}
}

Written by A.Sethi

February 9, 2009 at 12:09 pm

Listing Domains, Groups, Schemas, User, Computer from DirectoryEntry

with one comment

To list the Domains and their respective Groups, Schemas, User and Computer use DirectoryEntry and enumerate through the Children of the respective Entry. Check out the following code for more understanding

Include Reference to System.DirectoryServices in the project

using System;
using System.Collections.Generic;
using System.Text;
using System.DirectoryServices;

namespace ConsoleApplication2
{
class Program    
{
static void Main(string[] args)
{
DirectoryEntry dirEntry = new DirectoryEntry("WinNT:");

foreach (DirectoryEntry de in dirEntry.Children)
{
if (de.SchemaClassName == "Domain")
{
Console.WriteLine("Domain : " + de.Name);
Console.WriteLine("=================\n");
foreach (DirectoryEntry de2 in de.Children)
{
Console.WriteLine("  Name : " + de2.Name + " \t\t Type : " + de2.SchemaClassName);
}
}
de.Dispose();
}

dirEntry.Dispose();
Console.ReadLine();
}
}
}

Written by A.Sethi

February 9, 2009 at 10:12 am

SSL Part 1

without comments

One of the way to protect the data of the user visiting your website or accessing you application through network is by using SSL. SSL Also know as Secure Socket Layer. SSL was first developed by netscape. It was enhanced from version 1.0 to 3.0. Its latest version is known as TLS (Transport Layer Security) 1.2. for further details visit

http://en.wikipedia.org/wiki/Transport_Layer_Security

From the framework 2.0 it is also provided as the part of the libraries and with that we can develop our server or client application further on that. The class used for purpose is SslStream and it available in System.Net.Security namspace. For developing a server running on SSL we need a SSL certificate which contains the public and the private key. Sample for the server and the client application with complete information on the SslStream class is available in MSDN

http://msdn.microsoft.com/en-us/library/system.net.security.sslstream.aspx

In the 2 Part of this post i will try to put a sample application(server and client) working on the SSL

Written by A.Sethi

December 11, 2008 at 4:26 am

Performing PING from C#

without comments

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

Written by A.Sethi

September 23, 2008 at 2:55 pm

Posted in Networking

Tagged with , , ,

Getting TCP/IP Statistic through C#

without comments

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

Written by A.Sethi

September 23, 2008 at 7:39 am