外网如何获取内网的IP?
如果用户在局域网内使用内部IP进行外网服务器访问,如何在服务器端获得用户的局域网IP呢?最好使用C#来实现,谢谢能获取客户端计算机名也行,可以吗?...
如果用户在局域网内使用内部IP进行外网服务器访问,如何在服务器端获得用户的局域网IP呢?
最好使用C#来实现,谢谢
能获取客户端计算机名也行,可以吗? 展开
最好使用C#来实现,谢谢
能获取客户端计算机名也行,可以吗? 展开
6个回答
网易云信
2023-12-06 广告
2023-12-06 广告
网易云信IM支持发语音消息和播放的,但是需要评估下设备机型、设备系统、是否需要私有化部署等因素,详细可以点击进入官网查看,您也可以先咨询下商务,和商务提供具体信息。...
点击进入详情页
本回答由网易云信提供
展开全部
最近看到一些对IP的操作,感觉不错,而且以后可能会用到,所以收集过来,以备以后使用.
1.获取MAC地址操作:
using System;
using System.Runtime.InteropServices;
namespace UtilityControl
{
/// <summary>
/// 关于IP地址的若干操作
/// </summary>
public class IP
{
public IP()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
/// <summary>
/// 根据ip得到网卡mac地址
/// </summary>
/// <param name="ip">给出的ip地址</param>
/// <returns>对应ip的网卡mac地址</returns>
public static Int64 GetMACByIP(string ip)
{
Int32 ldest= inet_addr(ip); //目的地的ip
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
}
}
}
-----------------------------------------------------------------
用C#编写获取远程IP,MAC的方法
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的ip
try
{
Byte[] macinfo=new Byte[6];
Int32 length=6;
IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);
Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);
}
catch(Exception err)
{
Console.WriteLine(err);
}
-----------------------------------------------
用WMI
使用时首先添加System.Management.dll,然后引用
using System.Management;
using System.Threading;
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address"+mo["MacAddress"].ToString()+"<br/>");
mo.Dispose();
}
}
2.手动设置IP或设置动态IP
如果是winform的话,需要添加对System.Management.dll的引用
然后使用下面的类就可以实现你的前2个要求:
namespace modify
{
public class ChangeIP
{
public static readonly System.Version myVersion = new System.Version(1, 1);
private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
private readonly ManagementObjectCollection moc;
/// <summary>
/// example:
/// <code>
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP();
/// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"};
/// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"};
/// o.ChangeTo(ipList,subnetList);
/// </code>
/// </summary>
public ChangeIP()
{
moc = mc.GetInstances();
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
}
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
}
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
/// <param name="dnsServer">DNSServer List</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
iObj = mo.GetMethodParameters("SetDNSServerSearchOrder");
iObj["DNSServerSearchOrder"] = dnsServer;
oObj = mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);
}
}
/// <summary>DHCPEnabled</summary>
public void EnableDHCP()
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
if (!(bool)mo["DHCPEnabled"])
{
iObj = mo.GetMethodParameters("EnableDHCP");
oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
}
}
}
}
}
测试代码:
using System.Management;
using modify;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对第二个问题验证
ChangeIP test = new ChangeIP();
string[] str1 = new string[] {"192.168.1.5"};
string[] str2 = new string[] { "255.255.255.0" };
test.ChangeTo(str1, str2);
MessageBox.Show("IP已经更改,请验证");
//对第一个问题的验证,如果IP可用,当前并不是自动获取IP,这时下面的方法会自动获取IP
test.EnableDHCP();
}
}
}
1.获取MAC地址操作:
using System;
using System.Runtime.InteropServices;
namespace UtilityControl
{
/// <summary>
/// 关于IP地址的若干操作
/// </summary>
public class IP
{
public IP()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
[DllImport("Iphlpapi.dll")]
private static extern int SendARP(Int32 dest,Int32 host,ref Int64 mac,ref Int32 length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
/// <summary>
/// 根据ip得到网卡mac地址
/// </summary>
/// <param name="ip">给出的ip地址</param>
/// <returns>对应ip的网卡mac地址</returns>
public static Int64 GetMACByIP(string ip)
{
Int32 ldest= inet_addr(ip); //目的地的ip
try
{
Int64 macinfo = new Int64();
Int32 len = 6;
int res = SendARP(ldest,0, ref macinfo, ref len);
return macinfo;
}
catch(Exception err)
{
Console.WriteLine("Error:{0}",err.Message);
}
return 0;
}
}
}
-----------------------------------------------------------------
用C#编写获取远程IP,MAC的方法
[DllImport("Iphlpapi.dll")]
private static unsafe extern int SendARP(Int32 dest,Int32 host,ref IntPtr mac,ref IntPtr length);
[DllImport("Ws2_32.dll")]
private static extern Int32 inet_addr(string ip);
Int32 ldest= inet_addr("157.60.68.163");//目的地的ip
Int32 lhost= inet_addr("157.60.68.33");//本地的ip
try
{
Byte[] macinfo=new Byte[6];
Int32 length=6;
IntPtr mac=new IntPtr(macinfo[0]);
IntPtr len=new IntPtr(6);
int ii=SendARP(ldest,lhost, ref mac, ref len);
Console.WriteLine("Mac Add:"+mac);
Console.WriteLine("length:"+len);
}
catch(Exception err)
{
Console.WriteLine(err);
}
-----------------------------------------------
用WMI
使用时首先添加System.Management.dll,然后引用
using System.Management;
using System.Threading;
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if((bool)mo["IPEnabled"] == true)
Response.Write("MAC address"+mo["MacAddress"].ToString()+"<br/>");
mo.Dispose();
}
}
2.手动设置IP或设置动态IP
如果是winform的话,需要添加对System.Management.dll的引用
然后使用下面的类就可以实现你的前2个要求:
namespace modify
{
public class ChangeIP
{
public static readonly System.Version myVersion = new System.Version(1, 1);
private ManagementBaseObject iObj = null;
private ManagementBaseObject oObj = null;
private ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
private readonly ManagementObjectCollection moc;
/// <summary>
/// example:
/// <code>
/// ArLi.CommonPrj.ChangeIP o = new ArLi.CommonPrj.ChangeIP();
/// string[] ipList = new string[]{"192.168.0.253","192.168.0.250"};
/// string[] subnetList = new string[]{"255.255.255.0","255.255.255.0"};
/// o.ChangeTo(ipList,subnetList);
/// </code>
/// </summary>
public ChangeIP()
{
moc = mc.GetInstances();
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
}
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
}
}
/// <summary>cortrol</summary>
/// <param name="ipAddr">IPAddr List</param>
/// <param name="subnetMask">subnetMask List</param>
/// <param name="gateways">gateway List</param>
/// <param name="gatewayCostMetric">gateway CostMetric List, example: 1</param>
/// <param name="dnsServer">DNSServer List</param>
public void ChangeTo(string[] ipAddr, string[] subnetMask, string[] gateways, string[] gatewayCostMetric, string[] dnsServer)
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
iObj = mo.GetMethodParameters("EnableStatic");
iObj["IPAddress"] = ipAddr;
iObj["SubnetMask"] = subnetMask;
oObj = mo.InvokeMethod("EnableStatic", iObj, null);
iObj = mo.GetMethodParameters("SetGateways");
iObj["DefaultIPGateway"] = gateways;
iObj["GatewayCostMetric"] = gatewayCostMetric;
oObj = mo.InvokeMethod("SetGateways", iObj, null);
iObj = mo.GetMethodParameters("SetDNSServerSearchOrder");
iObj["DNSServerSearchOrder"] = dnsServer;
oObj = mo.InvokeMethod("SetDNSServerSearchOrder", iObj, null);
}
}
/// <summary>DHCPEnabled</summary>
public void EnableDHCP()
{
foreach (ManagementObject mo in moc)
{
if (!(bool)mo["IPEnabled"]) continue;
if (!(bool)mo["DHCPEnabled"])
{
iObj = mo.GetMethodParameters("EnableDHCP");
oObj = mo.InvokeMethod("EnableDHCP", iObj, null);
}
}
}
}
}
测试代码:
using System.Management;
using modify;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//对第二个问题验证
ChangeIP test = new ChangeIP();
string[] str1 = new string[] {"192.168.1.5"};
string[] str2 = new string[] { "255.255.255.0" };
test.ChangeTo(str1, str2);
MessageBox.Show("IP已经更改,请验证");
//对第一个问题的验证,如果IP可用,当前并不是自动获取IP,这时下面的方法会自动获取IP
test.EnableDHCP();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2015-10-13 · 知道合伙人互联网行家
关注
展开全部
需要把内网的ip设置,放到公网上,外网自然就获取到了内网ip。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不能
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询