PC Review


Reply
Thread Tools Rate Thread

Defensive socket programming

 
 
Rob.J.White@gmail.com
Guest
Posts: n/a
 
      10th Nov 2005
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

 
Reply With Quote
 
 
 
 
altakirad@gmail.com
Guest
Posts: n/a
 
      10th Nov 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

 
Reply With Quote
 
Arkady Frenkel
Guest
Posts: n/a
 
      11th Nov 2005
Just use NotifyAddrChange() if you haven't any IP
Arkady

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Rob.J.White@gmail.com
Guest
Posts: n/a
 
      11th Nov 2005
Superb answers both of you, I'm not sure which I'll use, but they both
look good.
Cheers.
Rob

 
Reply With Quote
 
Alun Jones
Guest
Posts: n/a
 
      15th Nov 2005
(E-Mail Removed) 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 | (E-Mail Removed).
Washington WA 98072-8661 | WFTPD, WFTPD Pro are Windows FTP servers.
Fax/Voice +1(425)807-1787 | Try our NEW client software, WFTPD Explorer.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Defensive Programming - Where do you Draw the Line? Smithers Microsoft C# .NET 11 2nd Oct 2007 10:09 PM
socket programming in C# sebastian.harko@gmail.com Microsoft C# .NET 5 26th Jul 2007 12:59 AM
Socket programming in C# =?Utf-8?B?TGluYQ==?= Microsoft Dot NET 1 16th Jul 2006 11:49 PM
socket programming =?Utf-8?B?QXRoZXJNdXJ0dXphcHVyd2FsYQ==?= Microsoft C# .NET 5 8th Dec 2004 07:34 PM
Try and Catch VS Regular Defensive Programming Martin Ortiz Microsoft VB .NET 6 27th Sep 2003 07:50 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:56 PM.