By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,770 Members | 1,564 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,770 IT Pros & Developers. It's quick & easy.

Detecting loss of network

P: n/a
I am using the following code to monitor a socket port. The code normally
works fine unless a network connection is lost. Since both ends of the
conversation on the same computer (normally) this causes a problem because
they can no longer connect. On my system if the network is reconnected the
two system can start communicating again but on other systems I am told they
can not.

One program is a service and the other is the user interface which is why I
am doing this.

So the questions:

1) Is there a way of handling this so a loss of the network connection does
not drop the connect when both programs are on the same system?

2) Is there anyway for the listener to determine that the network connect
has been lost?

3) In the latest version of .NET have them implemented memory pipes yet?

Regards,
John
CODE:

IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
foreach(IPAddress ipAddress in ipHostInfo.AddressList)
{
try
{
int ConnectionPort = (int)Utils.MiscFuns.ReadRegData(false, "Socket",
"Monitor Port", 12000);
int MaxThrds = (int)Utils.MiscFuns.ReadRegData(false, "Socket", "Max
Threads", 5);

IPEndPoint localEndPoint = new IPEndPoint(ipAddress, ConnectionPort);
if (localEndPoint.AddressFamily == AddressFamily.InterNetwork)
{
Socket listener = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
listener.Bind(localEndPoint);
listener.Listen(MaxThrds);
this.ListenerList.Add(listener);
}
}
catch(System.Threading.ThreadAbortException abEx)
{
throw abEx;
}
catch(Exception ex)
{
this.onException(ex);
System.Diagnostics.Debug.WriteLine("Monitor Thread (IPAddress)" +
ex.Message);
}
}
while(this.IsRun)
{
try
{
ArrayList tmpPortList = new ArrayList();
foreach(object o in this.ListenerList)
tmpPortList.Add(o);

if(tmpPortList.Count > 0)
{
Socket.Select(tmpPortList, null, null, 1000);
foreach(object o in tmpPortList)
{
Socket s = ((Socket)o).Accept();
/// start thread to do something
}
}
}
catch(System.Threading.ThreadAbortException abEx)
{
throw abEx;
}
catch(Exception ex)
{
this.onException(ex);
System.Diagnostics.Debug.WriteLine("Monitor Thread (IPAddress2)" +
ex.Message);
}
}
Mar 29 '06 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.