private void GetAllComputer()
{
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry dom in root.Children)
{
string name = dom.Name;
this.comboBox1.Items.Add(name);
foreach (DirectoryEntry com in dom.Children)
{
if (com.SchemaClassName.Equals("Computer"))
{
string coms = com.Name;
comboBox2.Items.Add(coms);
IPHostEntry iphe = Dns.GetHostEntry(com.Name);
foreach (IPAddress ip in iphe.AddressList)
{
if(ip.AddressFamily==AddressFamily.InterNetwork)
comboBox3.Items.Add(ip.ToString());
}
}
}
}
}这个方法是获取局域网的所有用户的机器名。
所有的机器名可以获取到。。
但是对应的机器名的IP获得不到
SocketException
报错为NativeErrorCode 提示:不知道的主机
还有个为 “请求的名称有效,但找不到请求的类型的数据”
我是想做局域网的聊天。。以下代码是我的发送消息
public void send(string message,string ip)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ipAddre = IPAddress.Parse(ip); Byte[] by = Encoding.UTF8.GetBytes(message); IPEndPoint ep = new IPEndPoint(ipAddre, 11000); s.SendTo(by, ep); }我只是知道这一种发送机制
但是这个是需要IP和端口来发送信息
但是我现在只能获得计算机名.
只是有个设想能不能用计算机名+端口来发送
如果可以 请教各位
如果不行的话
请帮我查看我的代码那里错误了 怎么才能实现这个功能。。谢谢各位高手。
{
DirectoryEntry root = new DirectoryEntry("WinNT:");
foreach (DirectoryEntry dom in root.Children)
{
string name = dom.Name;
this.comboBox1.Items.Add(name);
foreach (DirectoryEntry com in dom.Children)
{
if (com.SchemaClassName.Equals("Computer"))
{
string coms = com.Name;
comboBox2.Items.Add(coms);
IPHostEntry iphe = Dns.GetHostEntry(com.Name);
foreach (IPAddress ip in iphe.AddressList)
{
if(ip.AddressFamily==AddressFamily.InterNetwork)
comboBox3.Items.Add(ip.ToString());
}
}
}
}
}这个方法是获取局域网的所有用户的机器名。
所有的机器名可以获取到。。
但是对应的机器名的IP获得不到
SocketException
报错为NativeErrorCode 提示:不知道的主机
还有个为 “请求的名称有效,但找不到请求的类型的数据”
我是想做局域网的聊天。。以下代码是我的发送消息
public void send(string message,string ip)
{
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
IPAddress ipAddre = IPAddress.Parse(ip); Byte[] by = Encoding.UTF8.GetBytes(message); IPEndPoint ep = new IPEndPoint(ipAddre, 11000); s.SendTo(by, ep); }我只是知道这一种发送机制
但是这个是需要IP和端口来发送信息
但是我现在只能获得计算机名.
只是有个设想能不能用计算机名+端口来发送
如果可以 请教各位
如果不行的话
请帮我查看我的代码那里错误了 怎么才能实现这个功能。。谢谢各位高手。
IPHostEntry iphe;
try
{
iphe = Dns.GetHostEntry(com.Name);
}
catch
{
continue;
}然后检测没有IP的计算机名。然后从列表中过滤掉。。
我估计他们的IP都经过加密或者什么了。DNS无法解析。然后就会报错。