Author Topic: Socket Server Plugin  (Read 3299 times)

0 Members and 1 Guest are viewing this topic.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Socket Server Plugin
« 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:
Code: [Select]
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();
    }

}
« Last Edit: July 07, 2008, 02:23:53 pm by SarCaSTiC »
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!

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: Socket Server Plugin
« Reply #1 on: July 07, 2008, 03:16:36 pm »
You'd probably have to run the listener in another thread.

Offline Lance

  • Full Member
  • ***
  • Posts: 129
    • View Profile
Re: Socket Server Plugin
« Reply #2 on: July 07, 2008, 10:29:22 pm »
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!

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: Socket Server Plugin
« Reply #3 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.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline Chavo

  • x86
  • Hero Member
  • *****
  • Posts: 2219
  • no u
    • View Profile
    • Chavoland
Re: Socket Server Plugin
« Reply #4 on: July 08, 2008, 08:34:20 am »
Well if he wasn't confused before...

Offline Hdx

  • The Hdx!
  • Full Member
  • ***
  • Posts: 311
  • <3 Java/Cpp/VB/QB
    • View Profile
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.