Defensive socket programming

  • Thread starter Thread starter Rob.J.White
  • Start date Start date
R

Rob.J.White

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
 
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
 
Superb answers both of you, I'm not sure which I'll use, but they both
look good.
Cheers.
Rob
 
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.]
 
Back
Top