UDP网络通讯问题。。。
建立一个Socket绑定到IPAddress.Any的50000端口(ss),本地有一网卡网卡有多个IP,例如有以下几个ip192.168.0.10
192.168.0.20
192.168.0.30
三个IP,10为主IP,直接只用ss.SendTo发送UDP数据包出去使用的是主IP的身份,怎样灵活的在以上三个IP中切换发送时的IP身份? --------------------编程问答-------------------- 参考:
using System;
using System.Management;
namespace ArLi.CommonPrj {
public class ChangeIP {
/// <summary>
/// Build of ArLi 2003.6.3
/// </summary>
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>
/// 例:
/// <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>控制连接</summary>
/// <param name="ipAddr">IP地址列表</param>
/// <param name="subnetMask">对应子网掩码列表</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>控制连接</summary>
/// <param name="ipAddr">IP地址列表</param>
/// <param name="subnetMask">对应子网掩码列表</param>
/// <param name="gateways">网关列表</param>
/// <param name="gatewayCostMetric">网关接口跃点数列表</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>控制连接</summary>
/// <param name="ipAddr">IP地址列表</param>
/// <param name="subnetMask">对应子网掩码列表</param>
/// <param name="gateways">网关列表</param>
/// <param name="gatewayCostMetric">网关接口跃点数列表</param>
/// <param name="dnsServer">DNS 服务器列表</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>控制连接,使它使用 DHCP</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);
}
}
}
}
}
--------------------编程问答-------------------- 楼上的CathySun118理解错了意思 --------------------编程问答-------------------- 顶楼主你一下。
http://qzshi.com
网站建设技术论坛
另外招版主,或合作管理员,有兴趣的加群4228406 --------------------编程问答-------------------- 你要指定地址的话,绑定的时候就不要使用IPAddress.Any,而是应该与具体的地址绑定。 --------------------编程问答-------------------- --------------------编程问答-------------------- 绑定的时候就不要使用IPAddress.Any,而是应该与具体的地址绑定
如果这样做的话,我要帮定好多IP地址 --------------------编程问答-------------------- 需要使用哪个地址发送数据,就绑定到哪个,怎么会要绑定好多呢? --------------------编程问答-------------------- 我要是有20个公网IP地址,那不是要帮定20个socket --------------------编程问答-------------------- 我现在就是用绑定固定IP的方式,但Socket太多,我想只绑定一个0.0.0.0的这个IP地址。 --------------------编程问答-------------------- 不管你有多少个IP,需要用哪个发数据,就绑定哪个,绑定以后的Socket就以该IP发送数据。
当然,你喜欢把这些个IP都一次性地绑定到对应的Socket上,只要你不嫌麻烦,我也没意见。
补充:.NET技术 , C#