Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Vinod
More...

Resources » .NET programming » Visual Studio

How to find the open and closed port list


Posted Date:     Category: Visual Studio    
Author: Member Level: Gold    Points: 25


In this article, I will explain how to find the open and closed port in the given ip address or host name.It is very simple and basic code.try to connect the socket.if it is connected port is opened otherwise port is closed.



 


PORT SCANNER

this article is used to find the open and closed ports list of the user given ip address or host name.
here the scanport function have 3 parameters.first one is host name or ipaddress and second and third is starting,ending port numbers.
first try to connect the socket with ipaddress and port.
if the socket is connected then that port will be opened and that port is added into the openlist or otherwise closedlist.


public void ScanPort(string hostip,int startport,int endport)
{
for (int i = startport; i <= endport; i++)
{
lblMessage.Text = "Scanner: Scanning Port: " + i;
Port = i;
//Gets main IP of host
System.Net.IPAddress ip = System.Net.Dns.GetHostEntry(hostip).AddressList[0];
IPEndPoint ipEnd = new IPEndPoint(ip, Port);
//Creates a new socket.
System.Net.Sockets.Socket _Sock = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Stream, System.Net.Sockets.ProtocolType.Tcp);
try //Tests the port by trying to connect.
{
_Sock.Connect(ipEnd);
}
catch //Catches the error if it can not connect.
{
}
if (_Sock.Connected == true) //Checks if socket is connected or not
{
listOpened.Items.Add(Port.ToString()); //Adds to opened list if socket connected
listOpened.EnsureVisible(listOpened.Items.Count - 1);
}
else
{
listClosed.Items.Add(Port.ToString()); //Adds to Closed list if socket is not connected
listClosed.EnsureVisible(listClosed.Items.Count - 1);
}
_Sock.Close(); //Close the socket
}
MessageBox.Show("All Ports Scanned Completed!", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}





Did you like this resource? Share it with your friends and show your love!


Responses to "How to find the open and closed port list"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Example of multithreading in VB.NET
    Previous Resource: How to start visual studio from run ?
    Return to Resources
    Post New Resource
    Category: Visual Studio


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.