How do I determine if LAN connection exists?

  • Thread starter Thread starter Chuck
  • Start date Start date
C

Chuck

I am developing an application for use on laptop computers. I need to be
able to programmatically determine if the laptop is connected to the
network. If the connection exists I want to use the database on the
server, otherwise I want to use a cache on the laptop. I do know the
address of a server on the LAN.

The only way I can see is to attempt to use
System.Data.OleDbConnection.Open(), and catch any exceptions it throws when
it can't find the server. I hope one of you can suggest a better answer, as
this one has problems.

Anyone have any ideas?


Thanks
Chuck
 
Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}


--
Greetz

Jan Tielens
________________________________
Read my weblog: http://weblogs.asp.net/jan
 
Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot
 
I think it always works, I'm using dhcp too. Please let me know if you have
problems...

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Kot Behemot said:
Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot

Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}
 
Kot
The problem is that the laptop may not be connected to the LAN. When it is
I want to use a database on a known server. When disconnected I need to use
a cache that is created/updated while connected. I want the application to
auto-detect the LAN connection status and transparently use the correct db.

Jan
Your code almost worked. I used:

try
{
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName("hostname");
// found host
}
catch(System.Net.Socket.SocketException)
{
//host not found == LAN not connected!
}


Kot Behemot said:
Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot

Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}
 
Chuck

Thx for sharing! I wrote the code from the top of my head... :-/
I'm glad you figured it out!

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Chuck said:
Kot
The problem is that the laptop may not be connected to the LAN. When it is
I want to use a database on a known server. When disconnected I need to use
a cache that is created/updated while connected. I want the application to
auto-detect the LAN connection status and transparently use the correct db.

Jan
Your code almost worked. I used:

try
{
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName("hostname");
// found host
}
catch(System.Net.Socket.SocketException)
{
//host not found == LAN not connected!
}


Kot Behemot said:
Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot

Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}
 
how about using SystemInformation.Network boolean value, true if network
exists otherwise false.

--
Johnny
Jan Tielens said:
Chuck

Thx for sharing! I wrote the code from the top of my head... :-/
I'm glad you figured it out!

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Chuck said:
Kot
The problem is that the laptop may not be connected to the LAN. When it is
I want to use a database on a known server. When disconnected I need to use
a cache that is created/updated while connected. I want the
application
to
auto-detect the LAN connection status and transparently use the correct db.

Jan
Your code almost worked. I used:

try
{
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName("hostname");
// found host
}
catch(System.Net.Socket.SocketException)
{
//host not found == LAN not connected!
}


Kot Behemot said:
Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot


Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}
 
Tried your solution. It didn't work.
Even when I removed my Wireless net card, SystemInformation.Network returns
true. Maybe its keying on the network connection software?

Johnny said:
how about using SystemInformation.Network boolean value, true if network
exists otherwise false.

--
Johnny
Jan Tielens said:
Chuck

Thx for sharing! I wrote the code from the top of my head... :-/
I'm glad you figured it out!

--
Greetz,
Jan
__________________________________
Read my weblog: http://weblogs.asp.net/jan
Chuck said:
Kot
The problem is that the laptop may not be connected to the LAN. When
it
is
I want to use a database on a known server. When disconnected I need
to
use
a cache that is created/updated while connected. I want the
application
to
auto-detect the LAN connection status and transparently use the
correct
db.
Jan
Your code almost worked. I used:

try
{
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName("hostname");
// found host
}
catch(System.Net.Socket.SocketException)
{
//host not found == LAN not connected!
}


Hello

Would not it work only if you are using DHCP?
I, for example, use static IP on my local network and always have the
same address.

Kot


Try this:

string hostName = System.Net.Dns.GetHostName();
System.Net.IPHostEntry entry = System.Net.Dns.GetHostByName(hostName);
if(entry.AddressList[0].ToString=="127.0.0.1")
{
//Not connected
}
else
{
//Connected
}
 
Back
Top