Introduction
This is a very basic IP Scanner written in C#. I searched a long time and couldn't find something done in C# similar to what I wrote. So I decided to put it on the Code Project.
Ok.. now how to implement it ?
For those of you who have written a TCP/IP program it's more than easy... Basically after getting the From .. To IPAddresses, all you do is, call Dns.GetHostByAddress
.
Here is the most important part of the App:
try
{
string address = ipTo.Text.Substring(0,lastT+1);
System.Diagnostics.Debug.WriteLine(ipTo.Text.Substring(0,lastT+1)+i);
IPHostEntry he = Dns.GetHostByAddress( address+i);
Add( he.HostName );
result += 1;
}
catch( SocketException )
{
}
catch( Exception )
{
}
Ok , that's it ... yes .. for the implementation, get the code Or copy/paste this >...
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
using System.Threading;
namespace GetStations
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox ipFrom;
private System.Windows.Forms.TextBox ipTo;
private System.Windows.Forms.TextBox hostName;
private System.Windows.Forms.Button btnUse;
private System.Windows.Forms.ListBox add;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnStart;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
LoadMe();
}
void LoadMe()
{
hostName.Text = Dns.GetHostName();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.ipFrom = new System.Windows.Forms.TextBox();
this.ipTo = new System.Windows.Forms.TextBox();
this.hostName = new System.Windows.Forms.TextBox();
this.btnUse = new System.Windows.Forms.Button();
this.add = new System.Windows.Forms.ListBox();
this.label3 = new System.Windows.Forms.Label();
this.btnStart = new System.Windows.Forms.Button();
this.SuspendLayout();
this.label1.Location = new System.Drawing.Point(32, 32);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(88, 24);
this.label1.TabIndex = 0;
this.label1.Text = "Ip Range:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label2.Location = new System.Drawing.Point(32, 64);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(88, 24);
this.label2.TabIndex = 1;
this.label2.Text = "Host :";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.ipFrom.Location = new System.Drawing.Point(128, 32);
this.ipFrom.Name = "ipFrom";
this.ipFrom.Size = new System.Drawing.Size(160, 20);
this.ipFrom.TabIndex = 2;
this.ipFrom.Text = "127.0.0.1";
this.ipTo.Location = new System.Drawing.Point(328, 32);
this.ipTo.Name = "ipTo";
this.ipTo.Size = new System.Drawing.Size(160, 20);
this.ipTo.TabIndex = 3;
this.ipTo.Text = "127.0.0.1";
this.hostName.Location = new System.Drawing.Point(128, 64);
this.hostName.Name = "hostName";
this.hostName.Size = new System.Drawing.Size(128, 20);
this.hostName.TabIndex = 4;
this.hostName.Text = "";
this.btnUse.Location = new System.Drawing.Point(280, 64);
this.btnUse.Name = "btnUse";
this.btnUse.Size = new System.Drawing.Size(40, 24);
this.btnUse.TabIndex = 5;
this.btnUse.Text = "Use";
this.btnUse.Click += new System.EventHandler(this.UseClick);
this.add.Location = new System.Drawing.Point(64, 120);
this.add.Name = "add";
this.add.Size = new System.Drawing.Size(552, 264);
this.add.TabIndex = 6;
this.label3.Location = new System.Drawing.Point(296, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(24, 24);
this.label3.TabIndex = 7;
this.label3.Text = "to";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.btnStart.Location = new System.Drawing.Point(432, 64);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(112, 40);
this.btnStart.TabIndex = 8;
this.btnStart.Text = "Start !";
this.btnStart.Click += new System.EventHandler(this.StartClick);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(680, 422);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.label3);
this.Controls.Add(this.add);
this.Controls.Add(this.btnUse);
this.Controls.Add(this.hostName);
this.Controls.Add(this.ipTo);
this.Controls.Add(this.ipFrom);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Find network";
this.ResumeLayout(false);
}
#endregion
void Add( string m )
{
add.Items.Add( m );
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void UseClick(object sender, System.EventArgs e)
{
IPHostEntry ip = Dns.GetHostByName( Dns.GetHostName() );
ipFrom.Text = ip.AddressList[0].ToString();
ipTo.Text = ip.AddressList[0].ToString();
}
Thread sercher ;
private void StartClick(object sender, System.EventArgs e)
{
sercher = new Thread( new ThreadStart( Find ));
add.Items.Clear();
try
{
IPAddress from = IPAddress.Parse( ipFrom.Text );
IPAddress to = IPAddress.Parse( ipTo.Text );
}
catch( FormatException fe )
{
MessageBox.Show( fe.Message );
return;
}
sercher.Name = "Network searching thread";
sercher.Start();
add.Items.Add( "-->> >>> Please wait while processing is done <<< <<--" );
Thread.Sleep(1);
}
void Find( )
{
Add( "-- >>> >> >>> Found station <<< << <<< -- " );
int lastF = ipFrom.Text.LastIndexOf(".");
int lastT = ipTo.Text.LastIndexOf(".");
string frm = ipFrom.Text.Substring(lastF+1);
string tto = ipTo.Text.Substring(lastT+1);
int result = 0;
System.Diagnostics.Debug.WriteLine( frm + " " + tto);
for( int i = int.Parse( frm); i <= int.Parse(tto );i++)
{
try
{
string address = ipTo.Text.Substring(0,lastT+1);
System.Diagnostics.Debug.WriteLine(ipTo.Text.Substring(0,lastT+1)+i);
IPHostEntry he = Dns.GetHostByAddress( address+i);
Add( he.HostName );
result += 1;
}
catch( SocketException )
{
}
catch( Exception )
{
}
}
}
}
}