How can I run a socket server in a JavaOP plugin to receive commands without it freezing up? It is the only way I can link my bots at the moment.
Here is my server method:
public void run() throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4167);
} catch (IOException e) {
System.err.println("Could not listen on port.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
out.sendText(inputLine);
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
}
}
You'd probably have to run the listener in another thread.
I'll look up Concurrency in java.sun.com tuts then. Thanks :)
What you need to do is load up your plugin, and toss the contents of that run method into a new thread. That way it forks off and does it's own thing while the plugin turns around and tells JavaOp "okay, yeah, I'm finished loading". What's happening now is the plugin isn't telling JavaOp that until all the socket work is done (unreachable state), hence the freezing.
Well if he wasn't confused before...
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html