I've written a IRC Bot in Java, and I've been looking into a way to start and stop the entire program from PHP.
I've thought about using exec() for starting it, and having a socket listener to make it quit, but I've never used a socket listener before, let alone send data to it from PHP!
(Running on windows server, no screen pls)
Any ideas? (Examples? Links?)
Thanks guys.
EDIT: A friend bounced an idea off me, is there some way that I make the program stop by sending POST or GET data to a Java Program?
Just a suggestion of various other options...
Keep reading from the socket for the incoming requests...Say on port 9000 and parse the request.
So for instance you receive the request param say : stop_server=true then on reading such param stop the java program by calling a
System.exit(1);
Here's something for reference.
1.) Open a socket on a non standard port.
server_socket = new ServerSocket(9090, 0, localhost);
2.) Now start listening.
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (true) {
client = server_socket.accept();
Log.v("test","accepted");
IncomingMessage incomingMessage=new IncomingMessage(client);
incomingMessage.processMessage();
}
} catch (Exception e) {
// TODO: handle exception
Log.v("Exception", "", e);
}
}
}).start();
3.) Now parse your request.
streamReader = new InputStreamReader(incomingStream.getInputStream());
reader = new BufferedReader(streamReader);
while ((str1 = reader.readLine()).startsWith("GET")) {
if (str1.contains("phone")
& str1.contains("LOCATION=false")) {
String params = str1.substring(5);
Log.d("substring", params);
String paramStart = params.substring(1,
"phone".length() + 13);
Log.d("substring-", paramStart);
smsNumber = paramStart.split("=")[1];
} else if (str1.contains("LOCATION=true")
& str1.contains("phone")) {
String params = str1.substring(5);
Log.d("substring", params);
String paramStart = params.substring(1,
"phone".length() + 13);
smsNumber = paramStart.split("=")[1];
LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(true);
criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
String provider = manager.getBestProvider(criteria, true);
Location location = manager.getLastKnownLocation(provider);
double latitude = location.getLatitude();
double longitude = location.getLongitude();
CharSequence location_string = String.valueOf(latitude)
+ "," + String.valueOf(longitude).toString();
MESSAGE = MAP_LINk + location_string;
} else if (str1.contains("IMAGE=true")) {
try {
Camera mcamera = Camera.open();
mcamera.startPreview();
mcamera.takePicture(null, null, new PictureCallback() {
@Override
public void onPictureTaken(byte[] data,
Camera camera) {
// TODO Auto-generated method stub
File mpicture = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (mpicture == null) {
Log.d("*************",
"NULL****************");
Log.v("test", "image not clicked");
writer.write("Picture could not be clicked:");
writer.flush();
}
try {
FileOutputStream outputfile = new FileOutputStream(
"sdcard/DCIM/Camera/test.jpg");
outputfile.write(data);
outputfile.close();
MESSAGE = String.valueOf(data);
writer.write("Done-------------------");
writer.flush();
Log.v("test", "image clicked and saved in sdcard");
} catch (FileNotFoundException e) {
// TODO: handle exception
Log.d("EXCEPTION", "MESSAGE", e);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
mcamera.release();
} catch (Exception e) {
// TODO: handle exception
Log.v("EXCEPTION", "The picture could not be taken.", e);
}
}
if (!str1.contains("IMAGE=true")) {
SmsManager manager = SmsManager.getDefault();
manager.sendTextMessage(smsNumber, null, MESSAGE, null,
null);
}
writer.write("MESSAGE SENT\nMESSGE-DETAILS\n" + MESSAGE+smsNumber);
writer.flush();
}
writer.close();
reader.close();
Log.v("Sockets", "closed");
}
FYI
Don't just copy paste it, its actually in context with a project i made in android. So i have tried to skip the platform specific things. Its just to give an idea on how to send a http request to a java program.
Further reading on sockets
Getting started
http://www.tutorialspoint.com/java/java_networking.htm
Some more...