News:

Help! We're trapped in the computer, and the computer is trapped in 2008! Someone call the time police!

Main Menu

Socket Server Plugin

Started by Lance, July 07, 2008, 02:16:00 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Lance

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();
    }

}
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

iago

You'd probably have to run the listener in another thread.

Lance

I'll look up Concurrency in java.sun.com tuts then. Thanks :)
Quote from: Joe
[23:55:31] [william@enterprise ~/Documents/Programming/javaop2]$ svn commit -m 'Tried to fix StayConnected. Again.'
Committed revision 63.
StayConnected strikes back!

Joe

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.
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


Chavo

Well if he wasn't confused before...

Hdx

http://img140.exs.cx/img140/6720/hdxnew6lb.gif
09/08/05 - Clan SBs @ USEast
[19:59:04.000] <DeadHelp> We don't like customers.
[19:59:05.922] <DeadHelp> They're assholes
[19:59:08.094] <DeadHelp> And they're never right.