SoFunction
Updated on 2025-03-07

Code to implement network segment scanning in C#

Summary
I believe everyone is very familiar with scanners such as Xiaorong Time, so do you have the urge to write one by yourself? Recently, Microsoft has launched the implementation of the .NET strategic plan. C# is the main language. Are you interested in using C# to scan LAN IP addresses and try the happiness you write on yourself? Then please follow me.
Text
1. Let me first introduce the classes used:
DNS class: Under the namespace in .net, the main function is to retrieve information about a specific host from the Internet Domain Name System (DNS).
IPHostEntry class: associates a domain name system (DNS) host with a set of aliases and a set of matching IP addresses, and uses it with the DNS class.
IPAddress class: IP address on the network.
The namespaces used are:
The namespace provides a simple programming interface for a variety of protocols currently used on the network.
The namespace contains types that allow synchronous and asynchronous reading and writing on data streams and files.
Namespaces are mainly used for multi-line program programming.
The program implements the following functions:
2. Obtain the local host IP address
/// <summary> 
///
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void button1_Click(object sender,  e) 

IPHostEntry myHost = new IPHostEntry(); 
try 

this. = ""; 
//() Get the host name of the local computer
//() Get DNS information for the specified DNS host name
//Get DNS information of the local host
myHost = (()); 
//Show the local host name
 = (); 
//Show the IP address table of the local host
for(int i=0; i<;i++) 

("Local Host IP Address->" + [i].ToString()+ "\r");


catch(Exception error) 

(); 


3. Remote query
private void button2_Click(object sender,  e) 

this. = ""; 
IPHostEntry myDnsToIP = new IPHostEntry(); 
// Method: Format the DNS host name or dot-separated four-part notation
// IP address resolves to IPHostEntry instance
myDnsToIP =(()); 
//Show the list of IP addresses of this domain name
for(int i=0;i<;i++) 

( + "'s IP address is " + [i].ToString() + "\r");


4. Realize network segment scanning
Implement scanning of network segments to determine the number of hosts being used in the network. Multi-threading technology is used here and a thread is added to prevent the program from scanning too long and affecting the program's response. However, since garbage collection technology is used in .net, the control of threads is not very complicated.
private void button3_Click(object sender,  e) 

this. = ""; 
//Thread class: Create and control threads
//Thread thScan = new Thread(new ThreadStart(ScanTarget)); 
Thread thScan = new Thread(new ThreadStart(ScanTarget)); 
// Method: Start the thread
(); 

private void ScanTarget() 

//Construct the 31-8BIT bits of the IP address, which is the front part of the fixed IP address
// numericUpDown1 is a defined control
string strIPAddress =  + "." +  + "." +  + "."; 
//Start scan address
int nStrat = (); 
//Terminate the scan address
int nEnd =(); 
//Scan operation
for(int i = nStrat; i <= nEnd; i++) 

string strScanIPAdd = strIPAddress +(); 
//Convert to IP address
IPAddress myScanIP = (strScanIPAdd); 
try 

//You can add your own to enhance the functions
//   Method: According to IP
//Address to obtain DNS host information.
IPHostEntry myScanHost = (myScanIP); 
//Get the host name
string strHostName =(); 
(strScanIPAdd + "->" + strHostName + "\r"); 

catch(Exception error) 

(); 



So far, a simple implementation of the main function of the scanner using C# has been completed. Try it and you can see if the host on your network is feeling of accomplishment:)