I've written a windows shell that is controlled from and reports to a
network based management system. As such communication with the network
must be ensured. The problem I have is that I'm working on a DHCP
network and in some cases it is possible that my shell has loaded and
bound to a local address before DHCP has responded. In this instance
the IP address changes and my sockets no longer receive data.

As an intermediary measure I have constructed a thread that monitors
the IP address of the adapters and if they change it restarts the
networked side of my application. This seems a bit like a kludge;
surely this is something that people must come up against all the time,
for instance writers of windows services must have to take this into
account not only for start up, but also when a user does something like
ipconfig /renew, which could very well change the IP. Is there an
elegant way of doing what I'm trying to do, or indeed is there a way
that I can bind a socket to a physical adapter and not an IP address?
Oh and I'm working in C#, but if someone has a compelling reason to
switch to native code I'm game.

Any thoughts or theories would be greatly appreciated.
Rob

Re: Defensive socket programming by altakirad

altakirad
Thu Nov 10 16:18:32 CST 2005

You can register for an event to be notified on network changes. There
is sample code for it in a Microsoft Hands-On Lab. Note that while
this is a tabletPc lab, I'm using this code on WindowsXP with no
trouble.

See http://msdn.microsoft.com/mobility/tabletpc/hols/ The Lab you want
is "Building Applications That are Aware of Network Locations (Hands-on
Lab)". See source files NLS.cs, Network.cs, and
NetworkChangedEventArgs.cs. The magic is in the method
NLS.WaitForNetworkChanges().

I tried the ipconfig /renew and it did cause the event to fire. You'll
have to dig in though and see what all it's doing.

Happy coding!

-Jeff


Re: Defensive socket programming by Arkady

Arkady
Fri Nov 11 03:40:54 CST 2005

Just use NotifyAddrChange() if you haven't any IP
Arkady

<Rob.J.White@gmail.com> wrote in message
news:1131644273.451633.282380@g49g2000cwa.googlegroups.com...
> I've written a windows shell that is controlled from and reports to a
> network based management system. As such communication with the network
> must be ensured. The problem I have is that I'm working on a DHCP
> network and in some cases it is possible that my shell has loaded and
> bound to a local address before DHCP has responded. In this instance
> the IP address changes and my sockets no longer receive data.
>
> As an intermediary measure I have constructed a thread that monitors
> the IP address of the adapters and if they change it restarts the
> networked side of my application. This seems a bit like a kludge;
> surely this is something that people must come up against all the time,
> for instance writers of windows services must have to take this into
> account not only for start up, but also when a user does something like
> ipconfig /renew, which could very well change the IP. Is there an
> elegant way of doing what I'm trying to do, or indeed is there a way
> that I can bind a socket to a physical adapter and not an IP address?
> Oh and I'm working in C#, but if someone has a compelling reason to
> switch to native code I'm game.
>
> Any thoughts or theories would be greatly appreciated.
> Rob
>



Re: Defensive socket programming by Rob

Rob
Fri Nov 11 04:05:12 CST 2005

Superb answers both of you, I'm not sure which I'll use, but they both
look good.
Cheers.
Rob


Re: Defensive socket programming by Alun

Alun
Tue Nov 15 18:01:43 CST 2005

Rob.J.White@gmail.com wrote:
> As an intermediary measure I have constructed a thread that monitors
> the IP address of the adapters and if they change it restarts the
> networked side of my application. This seems a bit like a kludge;
> surely this is something that people must come up against all the time,
> for instance writers of windows services must have to take this into
> account not only for start up, but also when a user does something like
> ipconfig /renew, which could very well change the IP. Is there an
> elegant way of doing what I'm trying to do, or indeed is there a way
> that I can bind a socket to a physical adapter and not an IP address?
> Oh and I'm working in C#, but if someone has a compelling reason to
> switch to native code I'm game.

In addition to the suggestions others have made, to get a signal when
interfaces are added or deleted, have you considered simply binding to IP
address 0.0.0.0 ( INADDR_ANY ), so that you get _all_ incoming data on _all_
interfaces, new or old?

If you're using TCP, the getsockname() function will tell you on which
address the accepted socket has been accepted, if you need to know that
information.

If you're using UDP, getsockname() may not be reliable.

Alun.
~~~~
[Please don't email posters, if a Usenet response is appropriate.]
--
Texas Imperial Software | Find us at http://www.wftpd.com or email
23921 57th Ave SE | alun@wftpd.com.
Washington WA 98072-8661 | WFTPD, WFTPD Pro are Windows FTP servers.
Fax/Voice +1(425)807-1787 | Try our NEW client software, WFTPD Explorer.