Salve a tutti, ho avuto la necessità di realizzare un piccolo programmino java che effettui un ping ogni 25 minuti perchè altrimenti la mia connessione umts viene resettata ogni 30 minuti di inattività.
Mi chiedo però se con questo codice, il traffico generato in un giorno non sia troppo.
Se qualcuno può aiutarmi dandomi qualche suggerimento gli sarei molto grato.
Codice PHP:import java.io.*;
import java.util.*;
import java.util.Date.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.*;
public class PingApp extends MIDlet implements CommandListener {
private Display display;
private String url = "http://www.google.it";
private Form form;
private Command exit;
private Command stop;
private Timer timer;
private RunTimerTask tt;
private int count = 0;
boolean isPing = false;
public PingApp() {
display = Display.getDisplay(this);
form = new Form("Ping");
exit = new Command("Exit", Command.EXIT, 1);
//stop= new Command("Stop", Command.STOP, 2);
}
public void startApp() {
form.addCommand(exit);
//form.addCommand(stop);
form.setCommandListener(this);
// Repeating every 25 minutes
timer = new Timer();
tt = new RunTimerTask();
timer.schedule(tt,0, 1000*1500);
/*if(isPing) {
timer.cancel();
}*/
display.setCurrent(form);
}
public void pauseApp() { }
public void destroyApp(boolean unconditional) { }
public void commandAction(Command c, Displayable d) {
if (c == stop) {
timer.cancel();
} else if (c == exit) {
destroyApp(false);
notifyDestroyed();
}
}
/*--------------------------------------------------
* RunTimerTask Class - Run the task
*-------------------------------------------------*/
private class RunTimerTask extends TimerTask {
private String DateToday() {
Date today = new Date();
return today.toString();
}
public final void run() {
HttpConnection c = null;
OutputStream os = null;
InputStream is = null;
int ch;
StringBuffer b = new StringBuffer();
try {
c = (HttpConnection)Connector.open(url);
// Set the request method and headers
c.setRequestMethod(HttpConnection.POST);
c.setRequestProperty(
"If-Modified-Since","7 Sep 2005 19:43:31 GMT");
c.setRequestProperty(
"User-Agent","Profile/MIDP-1.0 Configuration/CLDC-1.0");
c.setRequestProperty("Content-Language", "en-US");
// Getting the output stream may flush the headers
os = c.openOutputStream();
os.write("Ping".getBytes());
os.flush();
is = c.openInputStream();
while ((ch = is.read()) != -1) {
b.append((char)ch);
}
isPing = true;
form.deleteAll();
form.append(DateToday()+"\n\nPing OK! Nuovo ping tra 25 minuti.\n\n");
} catch(IOException e) {
isPing = false;
form.append("Pinging.....");
} finally {
/*is.close();
os.close();
c.close();*/
}
}
}
}

Rispondi quotando