2012-11-26 2 views
0

내 응용 프로그램은 C# .net Framework 3.5입니다.C# WMI : NIC에 복수 IP 주소 추가/제거

응용 프로그램의 주요 기능은 다음과 같습니다

  1. 사용자가
  2. 는 NIC의 IP 주소 (및 서브넷 마스크) 선택한 사용자에게 할당 네트워크 인터페이스 카드 (NIC)를 선택할 수 있습니다 - 나는 WMI를 사용 - Win32_NetworkAdapterConfiguration 클래스의 EnableStatic 메소드.
  3. 통해 시작 Process 타사 C++ exe 구성 요소, 주어진 IP 주소를 수신 대기하는 서버처럼 작동 - 바인딩 기능이 서버에서 구현되므로 시작시 적절한 IP를 전달하십시오. 주소에서 듣기 시작합니다.

작업 2와 3은 무제한 반복 될 수 있으므로 매우 동일한 NIC에 여러 IP 주소를 할당하고 각자 자신의 IP 주소를 수신하는 여러 서버를 가질 수 있습니다.

public static bool ChangeNetworkInterfaceIPs(string adapterGUID, IpSettings newSettings) 
    { 
     try 
     { 
      if (String.IsNullOrEmpty(adapterGUID)) 
        throw new ArgumentException("adapterGUID"); 

       ManagementBaseObject inPar = null; 

       ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); 
       ManagementObjectCollection moc = mc.GetInstances(); 
       ManagementObject moTarget = null; 

       //Look for the correct network interface 
       foreach (ManagementObject mo in moc) 
       { 
        //find the target management object 
        if ((string) mo["SettingID"] == adapterGUID) 
        { 
         moTarget = mo; 
         break; 
        } 
       } 
       if (moTarget == null) 
       { 
        mc = null; 
        return false; 
       } 

       //we found the correct NIC. Save the current gateways, dns and wins 
       object winsSecondary = moTarget.GetPropertyValue("WINSSecondaryServer"); 
       object gateways = moTarget.GetPropertyValue("DefaultIPGateway"); 
       object dnsDomain = moTarget.GetPropertyValue("DNSDomain"); 
       object dnsServers = moTarget.GetPropertyValue("DNSServerSearchOrder"); 
       object winsPrimary = moTarget.GetPropertyValue("WINSPrimaryServer"); 

       if (newSettings.DHCP) 
       { 
        inPar = moTarget.GetMethodParameters("EnableDHCP"); 
        moTarget.InvokeMethod("EnableDHCP", inPar, null); 
       } 
       else 
       { 
        inPar = moTarget.GetMethodParameters("EnableStatic"); 
        inPar["IPAddress"] = newSettings.Ips; 
        inPar["SubnetMask"] = newSettings.Netmasks; 
        moTarget.InvokeMethod("EnableStatic", inPar, null); 
       } 

       //restore the gateways, dns and wins 
       if (gateways != null && !newSettings.DHCP) 
       { 
        inPar = moTarget.GetMethodParameters("SetGateways"); 
        inPar["DefaultIPGateway"] = gateways; 
        outPar = moTarget.InvokeMethod("SetGateways", inPar, null); 
       } 
       if (dnsDomain != null && !newSettings.DHCP) 
       { 
        inPar = moTarget.GetMethodParameters("SetDNSDomain"); 
        inPar["DNSDomain"] = dnsDomain; 
        outPar = moTarget.InvokeMethod("SetDNSDomain", inPar, null); 
       } 
       if (dnsServers != null && !newSettings.DHCP) 
       { 
        //Do not restore DNS Servers in case of DHCP. Will be retrieved from DHCP Server 
        inPar = moTarget.GetMethodParameters("SetDNSServerSearchOrder"); 
        inPar["DNSServerSearchOrder"] = dnsServers; 
        outPar = moTarget.InvokeMethod("SetDNSServerSearchOrder", inPar, null); 
       } 
       if (winsPrimary != null && !newSettings.DHCP) 
       { 
        inPar = moTarget.GetMethodParameters("SetWINSServer"); 
        inPar["WINSPrimaryServer"] = winsPrimary; 
        if (winsSecondary != null) 
        { 
         inPar["WINSSecondaryServer"] = winsSecondary; 
        } 
        outPar = moTarget.InvokeMethod("SetWINSServer", inPar, null); 
       } 

       return true; 
     } 
     catch 
     { 
      return false; 
     } 
    } 
:

특히 adapterGUID는 사용자의 GUID입니다이 코드는, NIC 및 newSettings 그것이 IP를 및 서브넷 마스크 목록을 들고 클래스의 선택, 내가 WMI를 사용하여 주어진 NIC에 IP 주소를 할당하려면

지금, 내 문제는 사용자가 활성 서버를 죽이려고 할 때입니다. 서버를 닫을 때 서버에서 수신 대기중인 IP 주소를 NIC에서 제거해야합니다.

프로세스를 죽이는 것이 아니라 ChangeNetworkInterfaceIP를 호출하여 NIC에 할당 된 IP를 업데이트 할 때 (더 이상 사용하지 않는 서버 중 하나를 제거) IP 주소의 새 목록을 사용합니다 (예 : 이전 목록 죽은 서버의 IP 주소없이) 뭔가 이상한 일이 일어난다 : 무작위로 다른 실행중인 서버 중 일부는 SOCKET_ERROR를 받고 그 연결은 닫힌다.

무슨 일이 일어나고 있는지 알고 싶습니다. NIC에서 사용하지 않은 IP 주소를 제거하면 실행중인 서버가 SOCKET_ERRORs를 무작위로 가져 오는 이유는 무엇입니까? 또한 IP 주소의 전체 목록을 제거하는 것만으로도 모범 사례가 아니므로 IP 주소를 제거하는 방법이 있습니까?

질문이 충분히 명확 해지기를 바랍니다. 감사합니다.

답변

0

게시물에있는 모든 질문에 답하지 않고 있지만 게시하는 것이 나 같은 시동에 도움이 될 수 있습니다.

질문 : 지정된 IP 주소를 제거하는 방법이 있습니까?
답변 : C#에서 프로세스로

Process p = new Process(); 
p.StartInfo.UseShellExecute = false; 
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; 
p.StartInfo.RedirectStandardOutput = true; 
p.StartInfo.FileName = "netsh"; 
p.StartInfo.Arguments = "netsh interface ipv4 delete address \"my NIC Name\" addr=192.168.1.1"; 
p.Start(); 
string response = p.StandardOutput.ReadToEnd(); 
p.Close(); 

참고 netsh 명령을 실행 :이 명령은 관리자에 실행되어야한다.