Checking is a networked computer is online before accessing a file

M

MattPKaiser

I am trying to find a way to check if a computer on a network is
"online" so that I can access a file on a share.

In short I maintain a list of computers that have my service running
(in the client application) and in my client program I allow the user
to change the computer that they are accessing.

If a computer that is on the list becomes unplugged and I try to
access a file with a FileStream the filestream creation takes 8-10
seconds before timing out. I wanted to know if there was a way to
quickly (1-2 seconds) determine if the computer is online? Otherwise
is there a way to access the file and specify a timeout?

Thanks

Matt
 
M

Mike

Ping it

That'll tell you if the pc is online or not.
If you get 'request timed out' then its offline, if you get an IP address
then its online.
 
G

gary

I am trying to find a way to check if a computer on a network is
"online" so that I can access a file on a share.

In short I maintain a list of computers that have my service running
(in the client application) and in my client program I allow the user
to change the computer that they are accessing.

If a computer that is on the list becomes unplugged and I try to
access a file with a FileStream the filestream creation takes 8-10
seconds before timing out. I wanted to know if there was a way to
quickly (1-2 seconds) determine if the computer is online? Otherwise
is there a way to access the file and specify a timeout?

Thanks

Matt

Hi Matt,

Take a look at the System.Net.NetworkInformation.Ping class.

HTH,
Gary
http://www.garyshort.org
 
M

MattPKaiser

Hi Matt,

Take a look at the System.Net.NetworkInformation.Ping class.

HTH,
Garyhttp://www.garyshort.org

Gary,

Thanks a bunch. I didn't realize that a Ping class existed in .Net.
It must be new to .Net 2.0. Anyways this worked great.

Thanks Again,

Matt
 
P

Peter Duniho

Thanks a bunch. I didn't realize that a Ping class existed in .Net.
It must be new to .Net 2.0. Anyways this worked great.

Note that a computer (or other network device) may have blocking or
disabled ping responses. So this method is only reliable if you know for
sure that the target device does in fact respond to pings.

Also note that pinging will tell you the state of the computer at the
moment that you ping it. Immediately after you ping it, the computer
could drop off the network or otherwise become unreachable. So you still
need to be able to handle the case when you try to connect but cannot.

I'm not aware of a way to specify a timeout when creating a FileStream
instance, though there may be one. However, you can implement it yourself
by using a separate thread to create the instance, with a waitable event
to signal to you when it's done. You can wait on a waitable event with a
timeout, so that allows you to specify the timeout. The tricky part is if
the thread creating the instance eventually succeeds, but after your
timeout. You need a way to deal with cleaning up the FileStream instance
in that case, but that should be as simple as setting a flag that the
thread can check to see whether it needs to clean up the instance or not.

Which method to use depends on just how reliably you need your goal to
accomplished. If you *never* want the user to have to wait the 8-10
seconds that it could take when timing out, then you need to implement
your timeout yourself (or figure out how to change the timeout inherent in
creating a FileStream instance). If you're okay with it being quick most
of the time, with the occasional chance for still waiting the timeout,
then the "ping" method should be just fine.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top