473,320 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I determine if LAN connection exists?

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
Nov 15 '05 #1
7 12338
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
"Chuck" <ce*****@austin.rr.com> wrote in message
news:1i******************@fe1.texas.rr.com...
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

Nov 15 '05 #2
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

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
news:O4**************@TK2MSFTNGP12.phx.gbl:
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
}

Nov 15 '05 #3
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" <no**@none.com> schreef in bericht
news:Xn*************************@206.141.192.32...
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

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
news:O4**************@TK2MSFTNGP12.phx.gbl:
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
}

Nov 15 '05 #4
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" <no**@none.com> wrote in message
news:Xn*************************@206.141.192.32...
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

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
news:O4**************@TK2MSFTNGP12.phx.gbl:
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
}

Nov 15 '05 #5
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" <ce*****@austin.rr.com> schreef in bericht
news:Hl*******************@twister.austin.rr.com.. .
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" <no**@none.com> wrote in message
news:Xn*************************@206.141.192.32...
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

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
news:O4**************@TK2MSFTNGP12.phx.gbl:
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
}


Nov 15 '05 #6
how about using SystemInformation.Network boolean value, true if network
exists otherwise false.

--
Johnny
"Jan Tielens" <ja*@no.spam.please.leadit.be> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <ce*****@austin.rr.com> schreef in bericht
news:Hl*******************@twister.austin.rr.com.. .
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" <no**@none.com> wrote in message
news:Xn*************************@206.141.192.32...
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

"Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
news:O4**************@TK2MSFTNGP12.phx.gbl:

> 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
> }



Nov 15 '05 #7
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" <jo*****@hotmail.com> wrote in message
news:uj**************@TK2MSFTNGP12.phx.gbl...
how about using SystemInformation.Network boolean value, true if network
exists otherwise false.

--
Johnny
"Jan Tielens" <ja*@no.spam.please.leadit.be> skrev i meddelandet
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <ce*****@austin.rr.com> schreef in bericht
news:Hl*******************@twister.austin.rr.com.. .
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" <no**@none.com> wrote in message
news:Xn*************************@206.141.192.32...
> 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
>
> "Jan Tielens" <ja*@no.spam.please.leadit.be> wrote in
> news:O4**************@TK2MSFTNGP12.phx.gbl:
>
> > 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
> > }



Nov 15 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Phillip J. Allen | last post by:
Hi all, I have a table with an “autonumber” primary key field that also acts as a foreign key in 2 other tables. I would like to programmatically add a new record to the first table and the...
6
by: Opie | last post by:
What would be a more efficient way for me to determine if a record in an SQL DB table exists? Right now, I have a try/catch like this: try {...
7
by: MLH | last post by:
I have been using variations of the following to determine whether a functional connection to internet exists. I'm thinking of replacing it with a simple PING, but I've never seen any code for...
15
by: ezmiller | last post by:
Does anybody know how to use javascript to test whether or not an internet connection exists? Is this possible even?
7
by: Stephen E. Weber | last post by:
I need to determine if a file exists using code. I tried using the system.io.file.exists function, that will appears to locate the file if I use the complete filespec, when I move the project to...
10
by: Steven Blair | last post by:
As I understand it, if I create a connection object in my application and close the connection, the next time I open a connection with the same connection string I should be using a pooled...
25
by: _DD | last post by:
I'd like to include a 'Test Connection' button in an app, for testing validity of a SQL connection string. I'd prefer to keep the timeout low. What is the conventional way of doing this?
7
by: semedao | last post by:
Hi all, I view many posts about this issue , the connected property does not tell us the current status of the socket. based on couple of suggestions of msdn , and some article here , I try to...
0
bartonc
by: bartonc | last post by:
This is a work in progress (current and active). There are some issues to resolve in supporting multiple connection types and I plan to add PySQLite to the mix. The this update is due to my...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.