Click here to Skip to main content
15,887,272 members
Home / Discussions / C#
   

C#

 
QuestionC# WinForms - Printing Multiple Pages Pin
Matt U.30-Nov-12 4:29
Matt U.30-Nov-12 4:29 
AnswerRe: C# WinForms - Printing Multiple Pages Pin
BobJanova30-Nov-12 4:34
BobJanova30-Nov-12 4:34 
GeneralRe: C# WinForms - Printing Multiple Pages Pin
Matt U.30-Nov-12 4:48
Matt U.30-Nov-12 4:48 
QuestionFind the differences between two images for screen sharing apps Pin
Tridip Bhattacharjee30-Nov-12 3:13
professionalTridip Bhattacharjee30-Nov-12 3:13 
AnswerRe: Find the differences between two images for screen sharing apps Pin
Dave Kreskowiak30-Nov-12 4:07
mveDave Kreskowiak30-Nov-12 4:07 
AnswerRe: Find the differences between two images for screen sharing apps Pin
BobJanova30-Nov-12 4:32
BobJanova30-Nov-12 4:32 
GeneralRe: Find the differences between two images for screen sharing apps Pin
Richard Deeming30-Nov-12 4:42
mveRichard Deeming30-Nov-12 4:42 
QuestionAdding/Removing multiple IP addresses to NIC Pin
lorenzo.santoro29-Nov-12 21:07
lorenzo.santoro29-Nov-12 21:07 
My application is C# .net Framework 3.5.

The main functionality of the application is:

let the user choose a network interface card (NIC)
assign to the user selected NIC an IP address (and subnet mask) - I use WMI - EnableStatic method of Win32_NetworkAdapterConfiguration class.
start through a Process a 3rd party C++ exe component, behaving like a server, which will be listening to the given IP address - the binding functionality is implemented by the server, so on Process start up I just give pass the proper IP address and it starts listening on that one.
Operation 2 and 3 can be repeated an unlimited number of times, thus it's possible to assign to the very same NIC several IP addresses and have multiple servers, each one listening to the its own IP address.

To assign the IP address to the given NIC I use WMI, in particular this code, where adapterGUID is the GUID of the user selected NIC and newSettings it's a class holding a list of IPs and subnet masks:

C#
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;
        }
    }

Now, my problem comes when the user wants to kill one the active servers. On server closure I have to remove from the NIC the IP address the server was listening to.

Killing the process it's not a problem, but when I call my ChangeNetworkInterfaceIPs to update the IP assigned to NIC (removing the one of the server no longer in use) using a new list of IP address (namely: the old list without the ip address of the killed server) something very strange happens: randomly some of other running servers get a SOCKET_ERROR and their connection it's closed.

Any idea on what's happening? Why the running servers are randomly getting SOCKET_ERRORs when I remove an unused IP address from the NIC ? Additionally, I know that probably setting a whole list of IP addresses just to remove one it's not really a best practice: is there a way to remove just a given IP address?

I hope the question is clear enough. Thank you for your time.
AnswerRe: Adding/Removing multiple IP addresses to NIC Pin
Dave Kreskowiak30-Nov-12 4:05
mveDave Kreskowiak30-Nov-12 4:05 
GeneralRe: Adding/Removing multiple IP addresses to NIC Pin
lorenzo.santoro12-Dec-12 5:26
lorenzo.santoro12-Dec-12 5:26 
QuestionDetecting Webcam Pin
sarang_k29-Nov-12 19:51
sarang_k29-Nov-12 19:51 
AnswerRe: Detecting Webcam Pin
Pete O'Hanlon29-Nov-12 20:35
mvePete O'Hanlon29-Nov-12 20:35 
GeneralRe: Detecting Webcam Pin
sarang_k2-Dec-12 18:17
sarang_k2-Dec-12 18:17 
GeneralRe: Detecting Webcam Pin
Pete O'Hanlon2-Dec-12 19:23
mvePete O'Hanlon2-Dec-12 19:23 
GeneralRe: Detecting Webcam Pin
sarang_k9-Dec-12 22:41
sarang_k9-Dec-12 22:41 
QuestionResourceBundle class in C#? Pin
LAPEC29-Nov-12 12:17
LAPEC29-Nov-12 12:17 
AnswerRe: ResourceBundle class in C#? Pin
Pete O'Hanlon29-Nov-12 12:53
mvePete O'Hanlon29-Nov-12 12:53 
QuestionHow do you properly deal with ContextSwitchDeadlock or DisconnectedContext? Pin
turbosupramk329-Nov-12 11:13
turbosupramk329-Nov-12 11:13 
QuestionSome ideas needed on the response part of a queued WCF example Pin
Ger Hayden29-Nov-12 10:28
Ger Hayden29-Nov-12 10:28 
QuestionI NEED YOUR HELP Pin
OğuzhanÇALIŞKAN29-Nov-12 10:08
OğuzhanÇALIŞKAN29-Nov-12 10:08 
AnswerRe: I NEED YOUR HELP Pin
OriginalGriff29-Nov-12 20:21
mveOriginalGriff29-Nov-12 20:21 
QuestionDebugger stepping into referenced class first and bypassing parent Pin
MichCl29-Nov-12 9:27
MichCl29-Nov-12 9:27 
AnswerRe: Debugger stepping into referenced class first and bypassing parent Pin
MichCl30-Nov-12 7:49
MichCl30-Nov-12 7:49 
QuestionIframes cross domain Exception Handling---PLEASE HELP Pin
Ved Yo29-Nov-12 8:46
Ved Yo29-Nov-12 8:46 
QuestionClient Side Crystal Report Error Pin
Taskeen Asif29-Nov-12 8:16
Taskeen Asif29-Nov-12 8:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.