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