如何从C#中的Mac地址获取IP地址 [英] how to get ip address from mac address in c#

查看:330
本文介绍了如何从C#中的Mac地址获取IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在c#中使用TCP创建了一个应用程序.其中将有一台服务器,而另一台将是客户端.每次客户端需要连接到服务器时,它们都将需要服务器IP地址和正在监听的端口.有什么办法可以使ip静态化吗?
为了解决这个问题,我一直认为mac地址是常数,因此,如果我们有任何方法可以从ma地址获取ip地址,那么我的问题就可以解决.

I have created an app using TCP in c#. In which there will be one server and other will be clients. Every time when clients need to connect to server,they will require server ip address and port on which it is listening. Is there any way to make that ip static ?
To solve this problem , i was thinking that mac address will be constant , so if we is there any way to get ip address from the ma address, then my problem will solve.

推荐答案

您可以同时获取mac地址和IP地址-查看以下内容:
You can get the mac addresses and IP addresses together - have a look at this: Retrieving IP and MAC addresses for a LAN[^]

But that is a very clumsy solution.

Can I suggest that either they access via the PC name, or get the IP from the PC name? The latter is really simple:
IPHostEntry host = Dns.GetHostByName("ANAS");
IPAddress ipaddr = host.AddressList[0];

那样,它不依赖于硬件-可以破坏,不依赖于特定计算机-可以替换.您无需更改软件,只需设置适当的PC ID.

That way, it''s not tied to hardware - which can break, it''s not tied to a specific machine - which can be replaced. You don''t need to change software, just set the appropriate PC Id.


这篇关于如何从C#中的Mac地址获取IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆