使用System.Net命名空间对网络进行操作

利用IPHostEntry类的GetHostByName方法,可以根据机器名来找到该机器的IP地址。
using System.Net;

    
protected void mybutton_Click(object sender, EventArgs e)
    {
        IPHostEntry hostInfo 
= Dns.GetHostByName(txtDomain.Text);
        showmsg.Text 
= hostInfo.HostName;
    }

    
<form id="form1" runat="server">
    
<div>
        
<asp:Label ID="showmsg" runat="server" Width="229px"></asp:Label><br />
        请输入机器名:
<asp:TextBox ID="txtDomain" runat="server"></asp:TextBox>
        
<asp:Button ID="mybutton" runat="server" OnClick="mybutton_Click" Text="确定" /></div>
    
</form>

利用IPHostEntry类的GetHostByAddress方法,可以根据IP地址来找到该机器名。
using System.Net;

    
    protected void mybutton_Click(object sender, EventArgs e)
    {
        IPHostEntry hostInfo = Dns.GetHostByAddress(txtDomain.Text);
        showmsg.Text = hostInfo.HostName;
    }

    
<form id="form1" runat="server">
    
<div>
        
<asp:Label ID="showmsg" runat="server" Width="229px"></asp:Label><br />
        请输入机器名:
<asp:TextBox ID="txtDomain" runat="server"></asp:TextBox>
        
<asp:Button ID="mybutton" runat="server" OnClick="mybutton_Click" Text="确定" /></div>
    
</form>

检测服务器开放的端口信息
using System.Net.Sockets;
using System.IO;

protected void Page_Load(object sender, EventArgs e)
    {
        
int[] arrPort = new int[5] { 2123251101433 };
        
string strServer = "localhost";
        
string strService, strResponse, strShow;
        
int intport;
        
for (int i = 0; i < arrPort.Length; i++)
        {
            intport 
= arrPort[i];
            
switch (intport)
            {
                
case 21:
                    strService 
= "FTP服务";
                    
break;
                
case 23:
                    strService 
= "Telnet服务";
                    
break;
                
case 25:
                    strService 
= "SMTP服务";
                    
break;
                
case 110:
                    strService 
= "POP3服务";
                    
break;
                
default:
                    strService 
= "无法知道";
                    
break;
            }
            
if ((strResponse = TcpConnect(strServer, intport)) != "没有开放")
            {
                strShow 
= "<b>" + intport.ToString() + "</b>端口: " +
                    strResponse 
+ "(<font color=red>" + strService + "</font>)";
            }
            
else
            {
                strShow 
= "<b>" + intport.ToString() + "</b>端口没有开放: " 
                    
+ "(<font color=red>" + strService + "</font>)";
            }
            pnlServer.Controls.Add(
new LiteralControl(strShow + "<p>"));
        }
    }

    
public string TcpConnect(string strServer, int intPort)
    {
        
string strResult;
        TcpClient tcpc 
= new TcpClient();
        
try
        {
            tcpc.Connect(strServer, intPort);
            Stream s 
= tcpc.GetStream();
            StreamReader sr 
= new StreamReader(s, System.Text.Encoding.Default);
            strResult 
= sr.ReadLine();
        }
        
catch (Exception ee)
        {
            strResult 
= "没有开放";
        }
        
finally
        {
            tcpc.Close();
        }
        
return strResult;            
    }

    
<form id="form1" runat="server">
    
<div>
        使用Socket探测主机信息
<br />
        
<asp:Panel ID="pnlServer" runat="server" Height="254px" Width="400px">
        
</asp:Panel>
    
</div>
    
</form>
原文地址:https://www.cnblogs.com/qixin622/p/754388.html