IPAddress[] ips=Dns.GetHostAddress("www.baidu.com"); //ips为主机ip地址
foreach(IPAddress ip in ips) ip为ip地址
{
string i=ip.ToString(); Dns.GetHostName();//本机名
}
--------------------------------------------
IPAddress[] ips =Dns.GetHostAddress("192.168.1.97");
foreach (IPAddress ip in ips)
{
}
-------------------------------------------
IPEndPoint ipendpoint =new IPEndPoint("192.168.1.97“,80);
----------------------------------------------
Socket socket=null;
IPHostEntry iphostentry=null;
iphosstentry=Dns.GetHostEntry(server);
foreach(IPAddress address in iphostentry.AddressList)
{
IPEndPoint ipendpoint=new IPEndPoint(address ,port);
Socket newSocket=new Socket(ipendpoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
newSocket.Connect(ipendpoint);
if(newSocket.Connect)
{
socket=newSocket;
break;
}
else{
continue;
}
return socket;
}
------------------------------------------------------------------
TcpListener tpclistener=new TcpListener("192.168.1.98",80);
tcplistener.Start();
foreach(IPAddress ip in ips) ip为ip地址
{
string i=ip.ToString(); Dns.GetHostName();//本机名
}
--------------------------------------------
IPAddress[] ips =Dns.GetHostAddress("192.168.1.97");
foreach (IPAddress ip in ips)
{
}
-------------------------------------------
IPEndPoint ipendpoint =new IPEndPoint("192.168.1.97“,80);
----------------------------------------------
Socket socket=null;
IPHostEntry iphostentry=null;
iphosstentry=Dns.GetHostEntry(server);
foreach(IPAddress address in iphostentry.AddressList)
{
IPEndPoint ipendpoint=new IPEndPoint(address ,port);
Socket newSocket=new Socket(ipendpoint.AddressFamily,SocketType.Stream,ProtocolType.Tcp);
newSocket.Connect(ipendpoint);
if(newSocket.Connect)
{
socket=newSocket;
break;
}
else{
continue;
}
return socket;
}
------------------------------------------------------------------
TcpListener tpclistener=new TcpListener("192.168.1.98",80);
tcplistener.Start();
本文通过实例演示了如何使用C#进行DNS解析获取主机IP地址,并介绍了如何利用Socket进行网络连接建立的过程。从获取域名对应的IP地址到创建并连接Socket,详细地展示了网络编程中的关键技术点。

被折叠的 条评论
为什么被折叠?



