Networking Problem

Discussion in 'Plugin Development' started by GhostHack, Jul 1, 2014.

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

    GhostHack

    I'm Creating a plugin to connect to a chat system i made. How ever i ran into a problem,

    if i use the while loop like so

    while(clientconected){
    String message = in.readLine();
    Bukkit.broadcastMessage(message);
    }

    The Bukkit Server And Clients will crash, but i cant get the threaded version to connect to my Socket Server running on a different system any suggestions would be greatly appreciated.
     
  2. Offline

    xize

    GhostHack

    never use a boolean in that way in a while loop unless you can limit the loop with the boolean.

    so while(true) causes to loop endlessly and only stops when it turns to false in this case it never stops thats probably causing the crash.

    Could I may ask what you wanted to make?
     
  3. Offline

    fireblast709

    xize he is trying to connect his chat service to the MC chat, so the infinite loop is necessary to keep it broadcasting.

    GhostHack you need to create a Thread, and move all the socket code into that Thread.
     
  4. Offline

    GhostHack

    i stated if i used that way it would create and endless loop, and on request im creating a bukkit plugin to go along with the irc styled chat program and server i've been creating

    however the threaded plugin client listener was not connecting to non threaded server
     
  5. Offline

    fireblast709

    GhostHack throw in the attempt of threaded chat
     
  6. Offline

    GhostHack

    fireblast709
    i tried that how ever the socket server which prints out all connection attempts with the ips listed never showed that the threaded version of the plugin client attempted to connect
     
  7. Offline

    electro4fun

    I am just thinking here, but instead of trying to look for the data server side, maybe make a packet event with ProtocolLib so when the chat system sends the message the server reads it instead of the server constantly waiting for input. You said you tried that threaded, I would assume you tried it Async otherwise it would crash the server. I am not sure if Async events need to 'finish' or if scheduling too many (one for each connected player) would cause the server to crash.
     
  8. Offline

    fireblast709

    GhostHack either you managed to create the socket incorrectly (server or clientside), or there are other issues on the network (i.e. portforwarding could cause issues with NAT)
     
  9. Offline

    GhostHack

    fireblast709 , @elcetro4fun
    there are no issues with the socket server or connection code since the unthreaded plugin version can connect and print out the client messages,
    and yes i used Async for both threaded version and non threaded the only changes between the working and non working version is the thread
    if someone can help me create a working thread that will work it would be greatly appreciated
     
  10. Offline

    fireblast709

    GhostHack still, paste your threaded version. I doubt the Thread causes it to break xD.
     
  11. Offline

    GhostHack

    Cleaner Post http://pastebin.com/dMQWs8hR

    well then that didnt turn out like i hoped

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  12. Offline

    fireblast709

    GhostHack
    • I suggest you start off by creating a new class that extends Thread.
    • Then add in all the methods you have defined in the main class (like stopChat() and startChat())
    • in that new class, you create a constructor with the hostname and port. Then simply create a new instance of the new class with the hostname and port you already have in.
    • Initialize the Socket in the new class
    • Instead of directly writing messages to the outputstream, rather have a synchronized List where you push the messages to and have the socket 'consume' them (and send them back to the server)
    • Remove all static keywords, no need for them :p
    • Code:java
      1. there are [ syntax=java][/ syntax] blocks (without spaces) for formatting code btw
     
  13. Offline

    GhostHack

    i forgot about the blocks, and how do you suggest i handle the list?
     
  14. Offline

    fireblast709

    GhostHack
    Code:java
    1. Collections.synchronizedList(new ArrayList<String>());
    and a simple pushing method that calls .add(message) (don't forget to synchronize!)

    The 'consumer' would simply while(!list.isEmpty()) remove the 0th element (again synchronized!)
     
Thread Status:
Not open for further replies.

Share This Page