Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Test for Network Connection (Java)

Mon Aug 24, 2009 1:28 pm

Hey guys, I'm looking at trying to find an elegant solution to a hacked up job I already have in place. At my job, we (myself & one other guy) have written a testing program for our products (transducers). Besides just database saving, the program requires a connection to our internal network for certain files (wire codes, inventory of resistors available, certificate of calibration templates, etc.) stored on our server. Currently, on program start I test the connection by attempting to read in a text file on the server, and if it fails, change the default locations (in our stored, internal config. file) to the local copies. To me, there has to be a much nicer way to do this, but I can't seem to find any sort of package out there, nor good suggestions (my google-fu is weak). If anyone has any suggestions, lemme know!
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
wibeasley
Gerbil Elite
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Test for Network Connection (Java)

Mon Aug 24, 2009 2:05 pm

I like your test. What don't you like about it, does it slow performance?

In .net, you have to ability to assert permissions. Does java have something similar? Here's an example that tests if you have permission to do things with File IO in a specific directory at runtime: http://msdn.microsoft.com/en-us/library/91wteedy.aspx
 
titan
Grand Gerbil Poohbah
Posts: 3376
Joined: Mon Feb 18, 2002 7:00 pm
Location: Great Smoky Mountains
Contact:

Re: Test for Network Connection (Java)

Mon Aug 24, 2009 2:40 pm

I can see that test running into timeout issues as it tries to discover the server.

Maybe you can check to see if the default NIC has an IP address that conforms to the internal network usage policy then ping the server and see if the ping comes back within a reasonable amount of time -- such as less than 50ms if the server is in the same network or 1000ms if the ping has to traverse the Internet. (Very generous ping times, I know, but it'll allow for congestion that doesn't really hamper performance.)

You'll still have to check to see if access to the file is available, so your current check will ultimately have to remain. It'll just be the final check.

I don't know anything about Java programming, so you'll have to figure that out for yourself.
The best things in life are free.
http://www.gentoo.org
Guy 1: Surely, you will fold with me.
Guy 2: Alright, but don't call me Shirley.
 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Re: Test for Network Connection (Java)

Wed Aug 26, 2009 10:12 am

wibeasley wrote:
I like your test. What don't you like about it, does it slow performance?

In .net, you have to ability to assert permissions. Does java have something similar? Here's an example that tests if you have permission to do things with File IO in a specific directory at runtime: http://msdn.microsoft.com/en-us/library/91wteedy.aspx


Honestly, there isn't anything 'wrong' with it per se, as if the file I'm checking for is deleted or missing then the program couldn't operate normally anyway. It's hardly noticeable if the program can get to it. I just try to open the file...
try
{
     FileReader fi = new FileReader(Configuration.defaultTestDataPath + "infofile.txt")
}
catch(FileNotFoundException e)
{
     //Redirections
     System.err.println("Error accessing network data files, redirecting to local copies.");
}


But to me it seems there should be some way to do basically what titan suggests (which is what I was looking for, hehe) but I can't seem to find any packages to do so for Java. Some sort of package that basically lets you feed it an IP and then you can just ping it. And yes, the check will have to remain but that line itself isn't really anything that takes time as long as the file is actually there.
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64
 
notfred
Maximum Gerbil
Posts: 4610
Joined: Tue Aug 10, 2004 10:10 am
Location: Ottawa, Canada

Re: Test for Network Connection (Java)

Wed Aug 26, 2009 10:24 am

Google for "java ping" has this:
http://blog.taragana.com/index.php/arch ... and-above/

I couldn't find any way to dump the routing table, but you could always just call the "route" CLI from Java.
 
wobbles-grogan
Gerbil
Posts: 31
Joined: Fri Jul 31, 2009 3:56 am

Re: Test for Network Connection (Java)

Wed Aug 26, 2009 10:26 am

Ah but java is a wonderful beast that can do anything you like :)

Hava a look at this

http://devdaily.com/java/edu/pj/pj010016/

or (and probably better)

http://java.sun.com/j2se/1.4.2/docs/api ... ocess.html

The process class is basically a class that lets you run commands from within your java program,
Theres a method there that sets the command (in your case your going to want to set it to "ping <ip address>"
Then get result, this will return whatever the program returns!

Note that this wont get teh file from the server, it will just check if the connection is there!

Hope this is a help!
 
wibeasley
Gerbil Elite
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Test for Network Connection (Java)

Wed Aug 26, 2009 10:40 am

Here's one way to check if you have write permissions at runtime: http://java.sun.com/javase/7/docs/api/index.html?java/io/FilePermission.html.
wobbles-grogan wrote:
The process class is basically a class that lets you run commands from within your java program.
If you can ping it with the .isReachable() function, I think that's preferable than writing you own commands for two reasons. First, the java function is probably just a wrapper around the same system calls, so you save time programming. Second, I imagine the resulting code is more portable when you avoid making calls to the OS.
 
CampinCarl
Graphmaster Gerbil
Topic Author
Posts: 1363
Joined: Mon Jul 04, 2005 9:53 pm

Re: Test for Network Connection (Java)

Wed Aug 26, 2009 12:51 pm

Woo thanks guys. The is Reachable() thing was exactly what I was looking for! I told you my google-fu was weak ;)

wobbles: I didn't want to use a process thing because parsing through and making decisions based upon the data provided wasn't exactly in my list of ideas for a good time ;) I use it for a few other things in the program, but mostly just to interface with external programs (Excel, etc.).

Appreciate the help, everyone.


Edit: After adding the Is Reachable() stuff, I shaved about 7 seconds off of program start time when the server is unreachable. Sweet.
Gigabyte AB350M Gaming-3 | R7 1700X | 2x8 GB Corsair Vengeance DDR4-3200 (@DDR4-2933)| Samsung 960 Evo 1TB SSD | Gigabyte GTX1080 | Win 10 Pro x86-64

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On