I am developing a Ping application for Android 2.2.
I try my code and it works, but only in local IPs, that's my problem I want to do ping to external servers too.
Here is my code:
private OnClickListener milistener = new OnClickListener() {
public void onClick(View v) {
TextView info = (TextView) findViewById(R.id.info);
EditText edit = (EditText) findViewById(R.id.edit);
Editable host = edit.getText();
InetAddress in;
in = null;
// Definimos la ip de la cual haremos el ping
try {
in = InetAddress.getByName(host.toString());
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Definimos un tiempo en el cual ha de responder
try {
if (in.isReachable(5000)) {
info.setText("Responde OK");
} else {
info.setText("No responde: Time out");
}
} catch (IOException e) {
// TODO Auto-generated catch block
info.setText(e.toString());
}
}
};
Ping 127.0.0.1 -> OK
Ping 8.8.8.8 (Google DNS) -> Time Out
I put the following line at Manifest XML too:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
Can anyone suggest me where I'm doing wrong?
I tried following code, which works for me.
private boolean executeCommand(){
System.out.println("executeCommand");
Runtime runtime = Runtime.getRuntime();
try
{
Process mIpAddrProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int mExitValue = mIpAddrProcess.waitFor();
System.out.println(" mExitValue "+mExitValue);
if(mExitValue==0){
return true;
}else{
return false;
}
}
catch (InterruptedException ignore)
{
ignore.printStackTrace();
System.out.println(" Exception:"+ignore);
}
catch (IOException e)
{
e.printStackTrace();
System.out.println(" Exception:"+e);
}
return false;
}
This is a simple ping I use in one of the projects:
public static class Ping {
public String net = "NO_CONNECTION";
public String host = "";
public String ip = "";
public int dns = Integer.MAX_VALUE;
public int cnt = Integer.MAX_VALUE;
}
public static Ping ping(URL url, Context ctx) {
Ping r = new Ping();
if (isNetworkConnected(ctx)) {
r.net = getNetworkType(ctx);
try {
String hostAddress;
long start = System.currentTimeMillis();
hostAddress = InetAddress.getByName(url.getHost()).getHostAddress();
long dnsResolved = System.currentTimeMillis();
Socket socket = new Socket(hostAddress, url.getPort());
socket.close();
long probeFinish = System.currentTimeMillis();
r.dns = (int) (dnsResolved - start);
r.cnt = (int) (probeFinish - dnsResolved);
r.host = url.getHost();
r.ip = hostAddress;
}
catch (Exception ex) {
Timber.e("Unable to ping");
}
}
return r;
}
public static boolean isNetworkConnected(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
#Nullable
public static String getNetworkType(Context context) {
ConnectivityManager cm =
(ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
if (activeNetwork != null) {
return activeNetwork.getTypeName();
}
return null;
}
Usage: ping(new URL("https://www.google.com:443/"), this);
Result: {"cnt":100,"dns":109,"host":"www.google.com","ip":"212.188.10.114","net":"WIFI"}
Run the ping utility in Android's command and parse output (assuming you have root permissions)
See the following Java code snippet:
executeCmd("ping -c 1 -w 1 google.com", false);
public static String executeCmd(String cmd, boolean sudo){
try {
Process p;
if(!sudo)
p= Runtime.getRuntime().exec(cmd);
else{
p= Runtime.getRuntime().exec(new String[]{"su", "-c", cmd});
}
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;
String res = "";
while ((s = stdInput.readLine()) != null) {
res += s + "\n";
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
In my case ping works from device but not from the emulator. I found this documentation:
http://developer.android.com/guide/developing/devices/emulator.html#emulatornetworking
On the topic of "Local Networking Limitations" it says:
"Depending on the environment, the emulator may not be able to support
other protocols (such as ICMP, used for "ping") might not be
supported. Currently, the emulator does not support IGMP or
multicast."
Further information:
http://groups.google.com/group/android-developers/browse_thread/thread/8657506be6819297
this is a known limitation of the QEMU user-mode network stack.
Quoting from the original doc: Note that ping is not supported
reliably to the internet as it would require root privileges. It
means you can only ping the local router (10.0.2.2).
Ping for the google server or any other server
public boolean isConecctedToInternet() {
Runtime runtime = Runtime.getRuntime();
try {
Process ipProcess = runtime.exec("/system/bin/ping -c 1 8.8.8.8");
int exitValue = ipProcess.waitFor();
return (exitValue == 0);
} catch (IOException e) { e.printStackTrace(); }
catch (InterruptedException e) { e.printStackTrace(); }
return false;
}
I implemented "ping" in pure Android Java and hosted it on gitlab. It does the same thing as the ping executable, but is much easier to configure. It's has a couple useful features like being able to bind to a given Network.
https://github.com/dburckh/AndroidPing
This is what I implemented myself, which returns the average latency:
/*
Returns the latency to a given server in mili-seconds by issuing a ping command.
system will issue NUMBER_OF_PACKTETS ICMP Echo Request packet each having size of 56 bytes
every second, and returns the avg latency of them.
Returns 0 when there is no connection
*/
public double getLatency(String ipAddress){
String pingCommand = "/system/bin/ping -c " + NUMBER_OF_PACKTETS + " " + ipAddress;
String inputLine = "";
double avgRtt = 0;
try {
// execute the command on the environment interface
Process process = Runtime.getRuntime().exec(pingCommand);
// gets the input stream to get the output of the executed command
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
inputLine = bufferedReader.readLine();
while ((inputLine != null)) {
if (inputLine.length() > 0 && inputLine.contains("avg")) { // when we get to the last line of executed ping command
break;
}
inputLine = bufferedReader.readLine();
}
}
catch (IOException e){
Log.v(DEBUG_TAG, "getLatency: EXCEPTION");
e.printStackTrace();
}
// Extracting the average round trip time from the inputLine string
String afterEqual = inputLine.substring(inputLine.indexOf("="), inputLine.length()).trim();
String afterFirstSlash = afterEqual.substring(afterEqual.indexOf('/') + 1, afterEqual.length()).trim();
String strAvgRtt = afterFirstSlash.substring(0, afterFirstSlash.indexOf('/'));
avgRtt = Double.valueOf(strAvgRtt);
return avgRtt;
}
Maybe ICMP packets are blocked by your (mobile) provider. If this code doesn't work on the emulator try to sniff via wireshark or any other sniffer and have a look whats up on the wire when you fire the isReachable() method.
You may also find some info in your device log.
Here is simple code to get latency in kotlin: less if, using ip:String as input, using regex to split format, using avg for latency in ms
var avg = LATENCY_ERROR
val process = Runtime.getRuntime().exec("/system/bin/ping -c 1 $ip")
BufferedReader(InputStreamReader(process.inputStream)).forEachLine {
// it: "PING 194.194.194.194 (194.194.194.194) 56(84) bytes of data."
// it: "rtt min/avg/max/mdev = 326.137/326.137/326.137/0.000 ms"
if (it.contains("rtt ")) {
avg = Regex("\\d+\\.\\d+").findAll(it).toList()[1].value.toFloat().roundToInt()
}
}
To get the boolean value for the hit on the ip
public Boolean getInetAddressByName(String name)
{
AsyncTask<String, Void, Boolean> task = new AsyncTask<String, Void, Boolean>()
{
#Override
protected Boolean doInBackground(String... params) {
try
{
return InetAddress.getByName(params[0]).isReachable(2000);
}
catch (Exception e)
{
return null;
}
}
};
try {
return task.execute(name).get();
}
catch (InterruptedException e) {
return null;
}
catch (ExecutionException e) {
return null;
}
}
Use this Code: this method works on 4.3+ and also for below versions too.
try {
Process process = null;
if(Build.VERSION.SDK_INT <= 16) {
// shiny APIS
process = Runtime.getRuntime().exec(
"/system/bin/ping -w 1 -c 1 " + url);
}
else
{
process = new ProcessBuilder()
.command("/system/bin/ping", url)
.redirectErrorStream(true)
.start();
}
BufferedReader reader = new BufferedReader(new InputStreamReader(
process.getInputStream()));
StringBuffer output = new StringBuffer();
String temp;
while ( (temp = reader.readLine()) != null)//.read(buffer)) > 0)
{
output.append(temp);
count++;
}
reader.close();
if(count > 0)
str = output.toString();
process.destroy();
} catch (IOException e) {
e.printStackTrace();
}
Log.i("PING Count", ""+count);
Log.i("PING String", str);
Pink ip Address
public static int pingHost(String host, int timeout) throws IOException,
InterruptedException {
Runtime runtime = Runtime.getRuntime();
timeout /= 1000;
String cmd = "ping -c 1 -W " + timeout + " " + host;
Process proc = runtime.exec(cmd);
Log.d(TAG, cmd);
proc.waitFor();
int exit = proc.exitValue();
return exit;
}
Ping a host and return an int value of 0 or 1 or 2 0=success, 1=fail,
* 2=error
Related
Hey all not sure if there is a solution to my situation but here goes.
I am sending some 9 cmd.exe commands:
static String startDaemon_2 = "scm daemon start"; //Start Daemon
static String isRQRunning_3 = "RQAdapter.bat status"; //Check if RQ is running
static String startRQ_4 = "RQAdapter.bat start"; //Start RQ
...etc etc....
And this is my main code to fire off the commands:
static ProcessBuilder processBuilder = new ProcessBuilder();
static Process process = null;
static BufferedReader reader = null;
public static Object steps(String cmds, String dir_) {
processBuilder.command("cmd.exe", "/c", cmds);
processBuilder.directory(new File(dir_));
try {
process = processBuilder.start();
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
if (line.contains("Key:")) {
return true;
} else if (line.contains("successfully")) {
return true;
} else if (line.contains("background")) {
return true;
} else if (line.contains("not")) {
//Load the adapter then
return "startRQM_4";
} else if (line.contains("sandbox")) {
return true;
} else if (line.contains("Misfit")) {
return true;
// repair goes here
} else if (line.contains("Unresolved")) {
//at this point theres no need to carry on. theres a conflick
return "fix";
}
}
int exitCode = process.waitFor();
System.out.println(line);
if (exitCode == 0) {
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}
And I call that function this way:
result = steps(startDaemon_2, scmDir);
if (result == "false") { System.out.println("not true"); }
result = steps(isRQMRunning_3, scmDir);
etc..... etc......
When I fire these same commands off in an actual cmd window it works just fine. Does what it should do. However, when the java program does the exact same thing it seems not to work even though there are no error.
Then I started thinking that maybe I need to define ProcessBuilder, process, reader
But it seems to still not work and without errors.
Is there some other type of way to do a few cmd.exe in the same setting? Seems it's just doing new stuff for each command.
What I mean by the last sentence is that I log in using the first command and then it goes to the other one which is start daemon, etc. But it seems to act like its brand new for each call (a.k.a. Isn't not logged in when firing the 2nd command, etc...)
I'm trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write su and press enter, so write:
screenrecord --time-limit 10 /sdcard/MyVideo.mp4
and press again enter and start the recording of the screen using the new function of android kitkat.
so, i try to execute the same code from java using this:
Process su = Runtime.getRuntime().exec("su");
Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");
But don't work because the file is not created. obviously i'm running on a rooted device with android kitkat installed. where is the problem? how can i solve? because from terminal emulator works and in Java not?
You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.
Try something like this:
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
outputStream.flush();
outputStream.writeBytes("exit\n");
outputStream.flush();
su.waitFor();
}catch(IOException e){
throw new Exception(e);
}catch(InterruptedException e){
throw new Exception(e);
}
A modification of the code by #CarloCannas:
public static void sudo(String...strings) {
try{
Process su = Runtime.getRuntime().exec("su");
DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());
for (String s : strings) {
outputStream.writeBytes(s+"\n");
outputStream.flush();
}
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
outputStream.close();
}catch(IOException e){
e.printStackTrace();
}
}
(You are welcome to find a better place for outputStream.close())
Usage example:
private static void suMkdirs(String path) {
if (!new File(path).isDirectory()) {
sudo("mkdir -p "+path);
}
}
Update:
To get the result (the output to stdout), use:
public static String sudoForResult(String...strings) {
String res = "";
DataOutputStream outputStream = null;
InputStream response = null;
try{
Process su = Runtime.getRuntime().exec("su");
outputStream = new DataOutputStream(su.getOutputStream());
response = su.getInputStream();
for (String s : strings) {
outputStream.writeBytes(s+"\n");
outputStream.flush();
}
outputStream.writeBytes("exit\n");
outputStream.flush();
try {
su.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
res = readFully(response);
} catch (IOException e){
e.printStackTrace();
} finally {
Closer.closeSilently(outputStream, response);
}
return res;
}
public static String readFully(InputStream is) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length = 0;
while ((length = is.read(buffer)) != -1) {
baos.write(buffer, 0, length);
}
return baos.toString("UTF-8");
}
The utility to silently close a number of Closeables (SoŃket may be no Closeable) is:
public class Closer {
// closeAll()
public static void closeSilently(Object... xs) {
// Note: on Android API levels prior to 19 Socket does not implement Closeable
for (Object x : xs) {
if (x != null) {
try {
Log.d("closing: "+x);
if (x instanceof Closeable) {
((Closeable)x).close();
} else if (x instanceof Socket) {
((Socket)x).close();
} else if (x instanceof DatagramSocket) {
((DatagramSocket)x).close();
} else {
Log.d("cannot close: "+x);
throw new RuntimeException("cannot close "+x);
}
} catch (Throwable e) {
Log.x(e);
}
}
}
}
}
Process p;
StringBuffer output = new StringBuffer();
try {
p = Runtime.getRuntime().exec(params[0]);
BufferedReader reader = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
output.append(line + "\n");
p.waitFor();
}
}
catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
String response = output.toString();
return response;
Late reply, but it will benefit someone. You can use the sh command in the exec() method.
Here is my example:
try {
File workingDirectory = new File(getApplicationContext().getFilesDir().getPath());
Process shProcess = Runtime.getRuntime().exec("sh", null, workingDirectory);
try{
PrintWriter outputExec = new PrintWriter(new OutputStreamWriter(shProcess.getOutputStream()));
outputExec.println("PATH=$PATH:/data/data/com.bokili.server.nginx/files;export LD_LIBRARY_PATH=/data/data/com.bokili.server.nginx/files;nginx;exit;");
outputExec.flush();
} catch(Exception ignored){ }
shProcess.waitFor();
} catch (IOException ignored) {
} catch (InterruptedException e) {
try{ Thread.currentThread().interrupt(); }catch(Exception ignored){}
} catch (Exception ignored) { }
What have I done with this?
First I call the shell, then I change (set) the necessary environments in it, and finally I start my nginx with it.
This works on unrooted devices too.
Greetings.
I am trying to execute a c++ code from java on a remote Windows machine. In order to deal with the remote part, I have created a Web service from where the actual command is run using Runtime.exec(). The c++ exe is not being called directly from the java code. I have a batch file that eventually calls the exe.
The problem is, both java and c++ processes hang. The java code on server side does handle the output stream and error stream. Also, the c++ code is logging everything in a file on Windows. The strange thing is that, when I remove the WS call and run the java code on server side as a standalone java program, it succeeds. Here is the java code:
public class RunCPlusPlusExecutable {
public int runExecutable() {
int exitValue = 0;
try {
Process p = null;
Runtime rt = Runtime.getRuntime();
System.out.println("About to execute" + this + rt);
p = rt.exec("c:/temp/execcplusplus.bat");
System.out.println("Process HashCode=" + p.hashCode());
StreamProcessor errorHandler = new StreamProcessor(p.getErrorStream(), "Error");
StreamProcessor outputHandler = new StreamProcessor(p.getInputStream(), "Output");
errorHandler.start();
outputHandler.start();
exitValue = p.waitFor();
System.out.println("Exit value : " + exitValue);
if (exitValue == 0)
System.out.println("SUCCESS");
else
System.out.println("FAILURE");
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
}
return exitValue;
}
class StreamProcessor extends Thread {
private InputStream is = null;
private String type = null;
private InputStreamReader isr = null;
private BufferedReader br = null;
private FileWriter writer = null;
private BufferedWriter out = null;
StreamProcessor(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
writer = new FileWriter("*******path to log file********");
out = new BufferedWriter(writer);
String line = null;
while ((line = br.readLine()) != null) {
Date date = new Date();
out.write("[" + type + "]: " + date + " : " + line);
out.newLine();
}
writer.flush();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (isr != null)
isr.close();
if (out != null)
out.close();
if (writer != null)
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Any idea what is causing the problem and how to debug it? Please note that I won't be able to debug the c++ code.
Thanks
Update 1:
Here are some more details...
The WS server is running from some admin user. And I have been running the standalone java program from some other user.
*It seems that the c++ executable is giving referenced memory error while executing from WS call. There are pop-ups citing the error with OK and Cancel buttons. *
Update 2:
The tomcat server where the WS is deployed is running as a Windows NT service. Can that be the cause of the error? If yes, how to resolve this?
I have an app which should execute some root commands.
SuperSU version is 1.04.
Su version is 1.02.
Android 4.1.1.
Device is Samsung Galaxy S3 - rooted.
The problem is I cannot get a permission prompt from SuperSU.
I've tried many things, but prompt never shows up.
For RootChecker basic, ADB and other apps it shows up.
Here is my procedure - maybe I'm doing something wrong.
private static String runShellCommand(String command) {
DataOutputStream os = null;
Process process = null;
try {
String [] env = {"PATH=/sbin:/vendor/bin:/system/sbin:/system/bin:/system/xbin"};
process = Runtime.getRuntime().exec("su", env, Environment.getExternalStorageDirectory() );
os = new DataOutputStream(process.getOutputStream());
InputStreamHandler err = new InputStreamHandler(process.getErrorStream(), false);
InputStreamHandler out = new InputStreamHandler(process.getInputStream(), false);
os.writeBytes(command + "\n");
os.flush();
os.writeBytes(EXIT);
os.flush();
os.close();
Log.d(LOGTAG, "Waiting on: " + process.waitFor());
String errOut = err.getOutput();
String stdOut = out.getOutput();
Log.d(LOGTAG, "Exit code: " + process.exitValue());
Log.d(LOGTAG, command + " erroutput: [" + errOut + "]");
Log.d(LOGTAG, command + " output: [" + stdOut + "]");
if (errOut != null && !errOut.equals(""))
return errOut;
else if (stdOut != null&& !stdOut.equals(""))
return stdOut;
else
return null;
} catch (Exception e) {
Log.e(LOGTAG, "runShellCommand error: ", e);
return null;
} finally {
try {
if (os != null) {
os.close();
}
if (process != null) {
Log.d(LOGTAG, "Exit val: " + process.exitValue());
process.destroy();
}
} catch (Exception ignored) {}
}
}
InputStream handler is:
private static class InputStreamHandler extends Thread {
private final InputStream stream;
private final boolean devNull;
StringBuffer output;
public String getOutput() {
return output.toString();
}
InputStreamHandler(InputStream stream, boolean devNull) {
this.devNull = devNull;
this.stream = stream;
output = new StringBuffer();
start();
}
#Override
public void run() {
try {
if (devNull) {
while (stream.read() != -1) {}
} else {
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
while (true) {
String line = br.readLine();
if (line == null) {
Log.d(LOGTAG, "Ended with reading!");
br.close();
return;
}
output.append(line);
}
}
} catch (IOException ignored) {
Log.e(LOGTAG, "Error", ignored);
}
}
}
Anyone have an idea why does it block so it doesn't show permission window?
Thanks.
I'm not exactly sure why this wouldn't work. The first thing that strikes me as odd is passing Environment.getExternalStorageDirectory() to exec. Maybe this (path) is not allowed (to execute from) ?
Why not simply use libsuperuser, written specifically to perform this very task ? It is open source, tiny, fully commented, has an example project, and even a document detailing the common problems you may encounter when trying to do this very operation.
I finally figured it out. The trick is to create a process (execute Runtime.getRuntime().exec("su");) separately in another thread. I'm not sure why this works, but it does.
Similar way is used in RootTools.
This has solved my problem.
I have already created simple instant messaging application in Java using Sockets and Swing. Right now it's communicating by resolving the hostname (PC name) or IP that's passes as a parameter. But is there a way to make it send a message with the Windows user ID (i.e. the user ID you use when you logon to Windows) as the parameter? This seems be easily done in C#, but how do I do it in Java?
Getting the username can be done using System.getProperty:
String name = System.getProperty("user.name");
This seems be easily done in C#
A 3rd party app (Winsent's Sent utility - winsentmessenger.com/sent) apparently can do this.
http://www.winsentmessenger.com/netsend/
The application in question is simply a wrapper around net send.
You could do the same, and invoke the process directly.
A solution lifted from:
http://members.iinet.net.au/~alw1746/awhome/freeware/WinPopup_java.txt
/*
WinPopup: send message to PC(s) on a Windows network from a Java program (like winpopup or net send).
Usage:
java WinPopup "user1,user2,..." "message"
eg. java WinPopup "peter" "where are you?" or java WinPopup 192.168.14.20 "Hello"
Build:
javac WinPopup.java
Alex Wong, Feb 2001
*/
import java.util.*;
import java.text.*;
import java.io.*;
public class WinPopup {
public static void main(String args[]) throws Exception {
String status;
if (args.length < 2) {
System.out.println("Usage: java WinPopup \"user1,user2,...\" \"msg\"");
System.exit(1);
}
if (args[0].length() < 1) {
System.out.println("User not found");
System.exit(1);
}
if (args[1].length() < 1) {
System.out.println("Message not found");
System.exit(1);
}
WinPopup popup=new WinPopup();
status=popup.alert(args[0],args[1]);
if (!status.equals("OK"))
System.out.println(status);
}
public String alert(String users,String msg) {
//loop thru list of users and net send the msg.
String buf,userList,user;
StringBuffer popup;
int ulen;
try {
if (users.length() < 1)
throw new Exception("User list not found.");
if (msg.length() < 1)
throw new Exception("Message not found.");
popup=new StringBuffer();
StringTokenizer st=new StringTokenizer(users,",");
while (st.hasMoreTokens()) {
buf=st.nextToken();
popup.append(buf).append(",");
}
if (popup.length() > 0) {
popup=popup.deleteCharAt(popup.length()-1);
userList=popup.toString();
ulen=userList.length();
for (int start=0,fin=0; fin <= ulen; fin++) {
if ((fin==ulen && fin > start) || userList.charAt(fin)==',') {
user=userList.substring(start,fin);
dosCmd("net send "+user+" \""+msg+"\"");
fin++;
start=fin;
}
}
}
return "OK";
}
catch (Exception e) {
return e.toString();
}
}
public void dosCmd(String cmd) {
//spawns a DOS process to run the net send command.
java.lang.Runtime rt;
Process proc;
try {
rt=java.lang.Runtime.getRuntime();
proc=rt.exec("c:\\winnt\\system32\\cmd.exe /C "+cmd);
StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
errorGobbler.start();
outputGobbler.start();
int exitVal=proc.waitFor();
}
catch (Exception e1) {
System.out.println("dosCmd exception.");
System.out.println(e1.toString());
}
}
class StreamGobbler extends Thread {
//eat all stderr and stdout output.
InputStream is;
String type;
StreamGobbler(InputStream is, String type) {
this.is = is;
this.type = type;
}
public void run() {
try {
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line=null;
while ( (line = br.readLine()) != null)
;
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}