Scanner d'ip

Contenu du snippet

Voici ma toute première source en Csharp, enfin !!! Bon ceci est un simple scanner multithread avec une interface graphique. A mon avis g fait un travail barbar malgrès que j'ai essayé de reprendre la structure que j'ai vu dans d'autre programme. Je m'attend bien sur à de nombreuses critiques...

Source / Exemple :


using System;
using System.Drawing;
using System.Windows.Forms;
using System.Net.Sockets;
using System.Net;
using System.IO;
using System.Threading;

namespace winscan
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox4;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.TextBox textBox2;
		public System.Windows.Forms.ListBox listBox1;
		public static int n = 10;
        static string ip;
        static int port;
        static object obmrd;
        
    
        public Form1()
        {
            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();          
            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        
        [STAThread]
        public static void Main(string[] args)
        {
            Application.Run(new Form1());
        }
        
        #region Windows Forms Designer generated code
        /// <summary>
        /// This method is required for Windows Forms designer support.
        /// do not change the method contents inside the source code editor. The Forms designer might
        /// not be able to load this method if it was changed manually.
        /// </summary>
        private void InitializeComponent() {
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox4 = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.label5 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(112, 24);
			this.textBox2.Name = "textBox2";
			this.textBox2.Size = new System.Drawing.Size(88, 20);
			this.textBox2.TabIndex = 2;
			this.textBox2.Text = "127.0.0.20";
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(208, 24);
			this.textBox3.Name = "textBox3";
			this.textBox3.Size = new System.Drawing.Size(40, 20);
			this.textBox3.TabIndex = 6;
			this.textBox3.Text = "4444";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 24);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(88, 20);
			this.textBox1.TabIndex = 1;
			this.textBox1.Text = "127.0.0.1";
			// 
			// textBox4
			// 
			this.textBox4.Location = new System.Drawing.Point(256, 24);
			this.textBox4.Name = "textBox4";
			this.textBox4.Size = new System.Drawing.Size(24, 20);
			this.textBox4.TabIndex = 8;
			this.textBox4.Text = "10";
			// 
			// label1
			// 
			this.label1.ForeColor = System.Drawing.Color.Black;
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(100, 16);
			this.label1.TabIndex = 4;
			this.label1.Text = "Ip start :";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(256, 8);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 9;
			this.label4.Text = "Thread :";
			// 
			// label5
			// 
			this.label5.Location = new System.Drawing.Point(18, 240);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(272, 23);
			this.label5.TabIndex = 0;
			this.label5.Text = "Waiting ...";
			// 
			// button1
			// 
			this.button1.FlatStyle = System.Windows.Forms.FlatStyle.System;
			this.button1.ForeColor = System.Drawing.SystemColors.Control;
			this.button1.Location = new System.Drawing.Point(288, 24);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(56, 24);
			this.button1.TabIndex = 3;
			this.button1.Text = "Scan";
			this.button1.Click += new System.EventHandler(this.ButtonClick);
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(16, 64);
			this.listBox1.Name = "listBox1";
			this.listBox1.ScrollAlwaysVisible = true;
			this.listBox1.Size = new System.Drawing.Size(328, 173);
			this.listBox1.TabIndex = 0;
			this.listBox1.Tag = "";
			// 
			// label2
			// 
			this.label2.ForeColor = System.Drawing.Color.Black;
			this.label2.Location = new System.Drawing.Point(112, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(96, 16);
			this.label2.TabIndex = 5;
			this.label2.Text = "Ip End :";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(208, 8);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(32, 16);
			this.label3.TabIndex = 7;
			this.label3.Text = "Port :";
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.SlateGray;
			this.ClientSize = new System.Drawing.Size(360, 262);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.textBox4);
			this.Controls.Add(this.label5);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.textBox3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox2);
			this.Controls.Add(this.textBox1);
			this.Controls.Add(this.listBox1);
			this.Name = "MainForm";
			this.Text = "IP scanner by aBd0ulaX";
			this.Load += new System.EventHandler(this.MainFormLoad);
			this.ResumeLayout(false);
		}
        #endregion
        void ButtonClick(object sender, System.EventArgs e)
        {
            try
            {
            	Thread run = new Thread(new ThreadStart(scanner));
            	if ( this.button1.Text == "Scan" ) 
            	{
            		n = int.Parse(this.textBox4.Text);
            		port = int.Parse(this.textBox3.Text);
        	        //scanner(this.textBox1.Text,this.textBox2.Text);
            	    run.Start();
	                //scanner();
	                
            		this.button1.Text = "Stop";
            		this.textBox1.Enabled = false;
            		this.textBox2.Enabled = false;
            		this.textBox3.Enabled = false;
            		this.textBox4.Enabled = false;
            		this.listBox1.Items.Add( "*** Scanning " + this.textBox1.Text + " to " + this.textBox2.Text + " port " + port + " threads " + this.textBox4.Text);
                	this.listBox1.Refresh();
            	}
            	else 
            	{
            		//run.Abort();
            		this.button1.Text = "Scan";
            	}
            }
            catch
            {
            	MessageBox.Show("Parametre error !!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
        void MainFormLoad(object sender, System.EventArgs e)
        {
        	this.listBox1.Items.Add("Scanning PGM ... Have Fun :-)");
           	this.listBox1.Items.Add("---------------------------------------------------------->");
			obmrd = sender;
        }   
        //public static void scanner(string ipString,string ipend)
        public void scanner()
        {
        	try 
        	{
		        string ipString = this.textBox1.Text;
        		string ipend = this.textBox2.Text;
	    	    char[] delimite = {'.'};
    	    	string[] bAdd = ipString.Split(delimite,4);
        	    string[] bAdd2 = ipend.Split(delimite,4);           
            	long w ,x ,y ,z;
		        for (w=long.Parse(bAdd[0]);w<=256;w++) for (x=long.Parse(bAdd[1]);x<=256;x++) for (y=long.Parse(bAdd[2]);y<=256;y++) for (z=long.Parse(bAdd[3]);z<=256;z++)
       		   {
            		if ( this.button1.Text == "Scan" ) 
            		{
						CloseConnect();
	            		return;
    	        	}
        	        n--;
            	    while (n <= 0 ) Thread.Sleep(300);
                	ip = w.ToString() + '.' + x.ToString() + '.' + y.ToString() + '.' + z.ToString();
	                //Console.WriteLine("{1}\t***Connection to {0}\n",ip,n);
    	            label5.Text = ip;
        	        label5.Refresh();
        	        
        	        Connection ipToScan = new Connection(ip,port,this);        
            	    Thread t1 = new Thread(new ThreadStart(ipToScan.Connect));
                	t1.Start();
                	if ( ( w == long.Parse(bAdd2[0]) ) && ( x == long.Parse(bAdd2[1]) ) && ( y == long.Parse(bAdd2[2]) ) && ( z == long.Parse(bAdd2[3]) ) ) { this.button1.Text = "Scan"; }
	            }
        	}
            catch
            {
            	MessageBox.Show("Parametre error !!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            	CloseConnect();
            	return;
            }
        }
        public void CloseConnect()
        {
        	this.textBox1.Enabled = true;
           	this.textBox2.Enabled = true;
            this.textBox3.Enabled = true;
            this.textBox4.Enabled = true;
            this.label5.Text = "Waiting ...";
            this.listBox1.Items.Add("*** Scan Closed");
            this.listBox1.Items.Add("---------------------------------------------------------->");
        }
    }
    public class Connection
    {
    	string ip;
    	Form1 frm;
    	int port;
    	
		public Connection(string ip1, int port1, Form1 frm1)
        {
			ip = ip1;
			frm = frm1;
			port = port1;
		}
		        public void Connect()
        {
            try {
                Socket s;
                IPEndPoint ep;
                s = new Socket( AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                ep = new IPEndPoint(IPAddress.Parse(ip),port);
                s.Connect(ep);
                frm.listBox1.Items.Add(ip + " port " + port);
                frm.listBox1.Refresh();
                //Console.WriteLine("***Connection to {0} succed\n",ip);
                Form1.n++;
                s.Close();
            }
            catch {
                Form1.n++;
            }
        }
    }
		
}

Conclusion :


Ce que j'aurai aimé faire c'est une seconde list avec les ip en cours de scan.
Je pensé pour de remplacer
//Console.WriteLine("{1}\t***Connection to {0}\n",ip,n);
par
listBox2.Items.Add(ip);
Mais je me suis rendu compte que le résultat ne s'affiché k la fin du scan et non au fur et à mesure. Faut que je trouve pkoi et si qlq1 pouvais m'aidé ce serai plus simple !!

Have fun

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.