Clan x86

Technical (Development, Security, etc.) => JavaOp Board => JavaOp Support Archive => Topic started by: Lance on July 07, 2008, 02:16:00 PM

Title: Socket Server Plugin
Post by: Lance on July 07, 2008, 02:16:00 PM
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();
    }

}
Title: Re: Socket Server Plugin
Post by: iago on July 07, 2008, 03:16:36 PM
You'd probably have to run the listener in another thread.
Title: Re: Socket Server Plugin
Post by: Lance on July 07, 2008, 10:29:22 PM
I'll look up Concurrency in java.sun.com tuts then. Thanks :)
Title: Re: Socket Server Plugin
Post by: Joe on July 07, 2008, 11:20:19 PM
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.
Title: Re: Socket Server Plugin
Post by: Chavo on July 08, 2008, 08:34:20 AM
Well if he wasn't confused before...
Title: Re: Socket Server Plugin
Post by: Hdx on July 09, 2008, 09:07:41 PM
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Thread.html