Basit bir portscan programı

portscan

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;

namespace BaseLib
{
     public class PortScan{

        public static bool Connect(string IPAddress, int Port , int WaitSeconds){

          TcpClient TcpScan = new TcpClient();
try{
bool ConnectSuccess = false;
TcpScan.BeginConnect(IPAddress, Port,
null, null);
for (int i = 0; i <= WaitSeconds; i++) {
System.Threading.
Thread.Sleep(10);
if (TcpScan.Connected){
ConnectSuccess =
true;
break;
}
}

                return ConnectSuccess;
}
catch{

               return false;
}
             finally{
TcpScan.Close();
}
}
}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaseLib;
using System.IO;

namespace WinPortScan{

     public partial class FrmMain : Form{          public FrmMain(){

InitializeComponent();
}

     private void btnScanPort_Click(object sender, EventArgs e){

     btnSave.Enabled = false;
int startPort = Convert.ToInt32(nudMin.Value);
int endPort = Convert.ToInt32(nudMax.Value);
if (startPort > endPort)
{
MessageBox.Show(“End port must greater then the start port”, “Error – PortScan”);
return;
}
Cursor.Current = Cursors.WaitCursor;
for (int CurrPort = startPort; CurrPort <= endPort; CurrPort++)
{
DateTime stopTime;
TimeSpan ts;
DateTime startTime = DateTime.Now;
bool result=PortScan.Connect(this.txtIPNum.Text, CurrPort,int.Parse(nudSeconds.Value.ToString()) );
if (result == true)
{
stopTime =
DateTime.Now;
ts= stopTime – startTime;
txtLog.AppendText(txtIPNum.Text+
“:” + CurrPort + ” open “+DateTime.Now.ToString(“HH:mm:ss:ff”)+“-“+ts.ToString()+“rn”); }
else
{
stopTime =
DateTime.Now;
ts = stopTime – startTime;
txtLog.AppendText(txtIPNum.Text +
“:” + CurrPort + ” closed “ + DateTime.Now.ToString(“HH:mm:ss:ff”) + “-“ + ts.ToString() + “rn”);
}
}
Cursor.Current = Cursors.Arrow;
btnSave.Enabled =
true;
}
     private void btnSave_Click(object sender, EventArgs e){
sfdIpPortScanResults.Filter =
“txt files (*.txt)|*.txt|All files (*.*)|*.*”;
sfdIpPortScanResults.FilterIndex = 1;
sfdIpPortScanResults.RestoreDirectory =
true;
if (sfdIpPortScanResults.ShowDialog()==DialogResult.OK)
{
string fileName = sfdIpPortScanResults.FileName;
SaveFile(fileName);
}
}

     protected void SaveFile(string fileName)
{
try {
Stream stream = File.OpenWrite(fileName);
using(StreamWriter writer=new StreamWriter(stream))
{
writer.Write(txtLog.Text);
}
}

     catch (IOException ex){
MessageBox.Show(ex.Message, “PortScanner”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}

   private void btnClear_Click(object sender, EventArgs e){
txtLog.Text =
“”;
btnSave.Enabled =
false;
}
}
}

Source: rss

Hello LinQ

İlk denemelerden örnekler. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HelloLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] sayilar = { 1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008};

            var ikibinuctenbuyuk = from s in sayilar where s > 2003 select s;
            Console.WriteLine(“2003’ten büyük”);
            foreach (var a in ikibinuctenbuyuk)
                {
                    Console.WriteLine(“-“+a);
                }
        }
    }
}
,


Source: rss

Generik sınıflar

Generik sınıf,içerisinde her türlü değişken tipi (int,string…) tutabilen,hangi nesne tutabileceğine sınıftan bir nesne yaratırken karar verebileceğiniz ve belirlene tip için aynı işleri yapabilmenizi sağlayan sınıftır.

List<Tipi> mylist = new List<Tipi>();

şeklinde tanımlanır.Bir kaç örnek vermek gerekirse;

List<string> mylist = new List<string>();
List<int> mylist = new List<int>();


Source: rss