kamilo 7 Share Posted May 10, 2020 So basically i've been working with Java sockets, and i'm currently stuck at a problem I've re-searched ways to handle multiple clients on one server; i.e. having multiple slave bots and one mule bot So here's my code for the Server.java class which is initialized on my main script, as new Server(port)... private ServerSocket server; private final int PORT; private volatile boolean isRunning = true; private ArrayList<ServerThread> serverThreads = new ArrayList<>(); public Server(int PORT) { this.PORT = PORT; } @Override public void run() { while (isRunning) { try { server = new ServerSocket(PORT); ServerThread thread = new ServerThread(server.accept()); serverThreads.add(thread); thread.start(); MethodProvider.log("size: " + serverThreads.size()); } catch (IOException e) { e.printStackTrace(); } } } As you can see I'm adding multiple connections into an arraylist of Threads (ServerThreads) private Socket socket; private ObjectInputStream input; private ObjectOutputStream output; private PlayerState state; private volatile boolean isRunning = true; public ServerThread(Socket socket) { this.socket = socket; } public void run() { while (isRunning) { try { input = new ObjectInputStream(socket.getInputStream()); output = new ObjectOutputStream(socket.getOutputStream()); // Read state state = (PlayerState)input.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } } So, all that is done; every connection is being added on to a que( i think) The problem i have is how the fuck do I reach the ServerThread code in another class, I already initialized the Server on my main and i don't wanna put more code there i would like to handle the code in a separate task class; i.e. so i can read the location, world. then send back the boolean yes(ready for trade) and then when all is set and done send a final status complelettion boolean which indicates that the transaction is complete Don't worry all this info is just a PlayerState class that i create and it contains all this info please let me know how i can access these new 'threads' i created in the main, and write to them. if possible handle in separate classes; one class to handle the world and hop, another class to set the go message for trade, and another class to setthe completeion boolean that it's done. Hopefully its possible Link to comment Share on other sites More sharing options...
kamilo 7 Author Share Posted May 10, 2020 also i dont get why it doesn't close spoilers and simply shoves it under another... Link to comment Share on other sites More sharing options...
Articron 738 Share Posted May 10, 2020 Have a look at my Server class from this thread: https://github.com/articron/IO-JSON-based-networking/blob/master/src/io/server/IOServer.java#L52 tl;dr I would write a wrapper for the connection and have the client send a UID at the start of the connection. You set it on your server-sided connection as it accepts the connection. That way you can loop through your list. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.