How can I use Java to control external programs based on my network latency?

In summary: I'm not sure how to make it loop though.In summary, the program can detect if the latency is low or high and depending on that it will open a program or not.
  • #1
sanitykey
93
0
Hi, i would like to make a program which can detect my latency, if it's low then it would open a program and if it's high it'd do nothing, i'd also like to loop it so it tries every half an hour or so. Would this be possible? If so any advice on how to start I'm a bit rusty and have only tried simple programs before (although i suppose this probably is simple :tongue2: ).

Could it use "ping www.google.co.uk" in cmd to get my latency?

Thanks
Richy
 
Technology news on Phys.org
  • #2
You can't do a ping with Java directly because it can't send ICMP packets, but you can have Java call ping.exe and read the standard output, for example. I've done something similar with c++.
 
  • #3
Thanks -Job- i managed to make it, my code is a bit messy but here it is if anyone is interested =)

import java.io.IOException;
import java.io.InputStream;

public class PingDownload {

public PingDownload() {}

public static void main(String[] args) {

int ReadCounter = 0;
int NumTotal = 0;
int LoopParameter = 0;

try {

String command = "ping www.google.co.uk";
String command2 = "cmd /c start C:\\ProgramShortcut";

while(LoopParameter != 1) {

Process child = Runtime.getRuntime().exec(command);
//Executes the command string
InputStream in = child.getInputStream();

while (ReadCounter != 169) {

int CharA = in.read();
//Reads the first character and stores its ASCII value
int CharB = in.read();
//Reads the second character and stores its ASCII value
int CharC = in.read();
//Reads the third character and stores its ASCII value

if(47<CharA && CharA<58 && ReadCounter>150) NumTotal = NumTotal + CharA;
if(47<CharB && CharB<58 && ReadCounter>150) NumTotal = NumTotal + CharB;
if(47<CharC && CharC<58 && ReadCounter>150) NumTotal = NumTotal + CharC;

//If any of the characters are numbers and the counter is near the end
//(above 150)
//add them to NumTotal this ensures only the last 3 numbers from the ping
//command are taken which are the maximum minimum and average pings
//detected


ReadCounter++; //Increment the counter

}

//When my ping is high all three numbers consist of three digits and the
//lowest
//value these can take is 0 so i know if the total is below 48x9 = 432 my ping
//is low

if(NumTotal < 432) {

child = Runtime.getRuntime().exec(command2);
LoopParameter = 1; //If the program is opened end the loop
}

}
}

catch (IOException e) {}
}
}

*cough* There might be bits wrong with it other than the way I've went about it but it worked in Netbeans and compiled ok.
 
Last edited:

1. What is Java External Program Control?

Java External Program Control refers to the process of using Java programming language to control or communicate with external programs, such as applications or devices, on a computer. This allows for the integration of different software systems and enables automation of tasks.

2. How can I use Java to control an external program?

To control an external program using Java, you can use the ProcessBuilder class to create a process and execute commands or send input to the external program. You can also use the Runtime class to execute commands directly in the operating system's command line.

3. What are the benefits of using Java for external program control?

One of the main benefits of using Java for external program control is its platform independence. Java programs can run on any operating system that has a Java Virtual Machine installed. This makes it easier to develop and maintain cross-platform applications that need to communicate with external programs.

4. Can I control multiple external programs using Java?

Yes, you can control multiple external programs using Java. You can use the ProcessBuilder class to create multiple processes and execute commands simultaneously. You can also use multithreading to control multiple external programs concurrently.

5. Is there any security risk associated with Java External Program Control?

There can be security risks associated with Java External Program Control if proper precautions are not taken. To mitigate these risks, make sure to only execute trusted programs and inputs, and use appropriate security measures such as input validation and access control. It is also recommended to run Java programs in a sandbox or restricted environment to prevent malicious programs from being executed.

Similar threads

  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • STEM Academic Advising
Replies
12
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • STEM Academic Advising
Replies
5
Views
935
  • Programming and Computer Science
Replies
3
Views
1K
  • STEM Academic Advising
Replies
4
Views
1K
  • Programming and Computer Science
Replies
9
Views
7K
Replies
2
Views
882
Back
Top