使用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 ] {  21 23 25 110 1433  };
        
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/archive/2007/05/21/754388.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值