Socket blocks server commands

Discussion in 'Plugin Development' started by MrFrozen, Jul 18, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    MrFrozen

    Hey Bukkit,

    I used socks into my plugin to test stuff out but in the begin I have my first problem and that is the command line in the console doesnt work anymore. How to fix this?

    Core.java
    Code:
    public class Core extends JavaPlugin {
        int port = 8223;
        ServerSocket listenSock = null;
        Socket sock = null;
       
        public void onEnable () {
            BukkitScheduler scheduler = Bukkit.getServer().getScheduler();
            scheduler.scheduleSyncDelayedTask(this, new Runnable() {
                @Override
                public void run() {
                    try {
                        listenSock = new ServerSocket(port);
                        sock = listenSock.accept();
                        BufferedReader br = new BufferedReader(new InputStreamReader(sock.getInputStream()));
                        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
                        String line = "";
                        while ((line = br.readLine()) != null) {
                            getServer().getConsoleSender().sendMessage("Received: " + line);
                            bw.write("Java said: " + line + "\n");
                            bw.flush();
                        }
                        bw.close();
                        br.close();
                        sock.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }, 20L);
        }
       
        public void onDisable () {
           
        }
    }
     
  2. @MrFrozen
    I think it's because you're running the server on the minecraft server's main thread, which makes your whole server freeze. Run it Async instead of Sync
     
  3. Offline

    MrFrozen

    How :$
     
  4. .runAsyncDelayedTask
     
  5. Offline

    MrFrozen

    Thanks!

    Only after 1 received socks connection from the web it wont receive anymore
     
  6. I'm not so good at socket/network stuff but I think you only listen 1 time to an incoming thing
     
  7. @MrFrozen
    Make a separate class for this. Create a boolean in the class called something like "running" and set it to true. Run the task 1 time asynchronously, but put the stuff you want to do inside a while(running) {} block. When your plugin disables, set the running field to false, and your task should stop running.
     
  8. Offline

    MrFrozen

    Sorry but I dont really get it how to...
     
  9. Offline

    mythbusterma

    @MrFrozen

    You should probably start with something simpler before you try to do network I/O.
     
  10. Offline

    MrFrozen

    Like what xD I did everything everything I could that I wanted to and nou I like to go into networking.

    I did from simple string manipulation to modifiening packets to custom file readers and classes.
     
  11. Offline

    mythbusterma

    @MrFrozen

    Well, you don't seem to have a very firm grasp of multithreading...
     
  12. Offline

    MrFrozen

    Kinda true Threading / Networking is something what I wanna learn
     
  13. Offline

    mythbusterma

    @MrFrozen

    Well you should probably create your own thread and use it to monitor for network input, then schedule a task on the main thread every time you have input.
     
  14. Offline

    MrFrozen

    Thanks for this, this will help me furthur in my journey Threads / Networking and personally I learn from this the most! Im gonna figure it out!
     
Thread Status:
Not open for further replies.

Share This Page