Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - multithreaded server only works for one connection
    text
    copied!<p>I'm writing a multiplayer game and I'm writing my own server for it in Java using sockets. </p> <p>I successfully was able to get a server up and running using the Java tutorials and some googling but when I tried to implement a multithreaded server to handle multiple clients, it's not working quite right. </p> <p>The first client that connects will still work properly, but any other clients that connect will just sit there and do nothing after sending their input to the server. Here is my code:</p> <pre><code>//This class handled Server/client communication for one client. public class GameRoom implements Runnable { public GameRoom(Socket socket) { this.socket = socket; } public void run() { logger.info("Game room has been created."); while(true) { try { in = new BufferedReader(new InputStreamReader(socket.getInputStream())); out = new PrintWriter(socket.getOutputStream()); String clientResponse = in.readLine(); out.println("You wrote " + clientResponse); out.flush(); } catch (IOException e) { logger.severe(e.getMessage()); throw new RuntimeException(); } } } //Will eventually change the string to a GSON object and delegate the appropriate actions based on the gson object type. private String delegateClientInput(String clientInput) { return "I heard you say: " + clientInput + "\n"; } private BufferedReader in; private PrintWriter out; private Socket socket; private static final Logger logger = LogUtil.initializeServerLog(GameRoom.class.toString()); } </code></pre> <hr> <pre><code>/* * This class houses the server socket itself. Handles connecting to multiple clients. */ public class ServerClientThreadPool extends Thread { public static void main(String[] args) { ServerClientThreadPool serverClientThreadPool = new ServerClientThreadPool(); serverClientThreadPool.startServer(); } ServerClientThreadPool() { try { serverListener = new ServerSocket(GAME_ROOM_PORT); } catch (IOException e) { logger.severe(e.getMessage()); throw new RuntimeException(); } } public void startServer() { for(int i = 0; i &lt; MAX_CONNECTIONS; i++) { try { GameRoom gameRoom = new GameRoom(serverListener.accept()); gameRoom.run(); } catch (IOException e) { logger.severe(e.getMessage()); throw new RuntimeException(); } } } private static final int GAME_ROOM_PORT = 18000; private ServerSocket serverListener; private static final int MAX_CONNECTIONS = 100; private static final Logger logger = LogUtil.initializeServerLog(ServerClientThreadPool.class.getName()); } </code></pre> <p>And here's the client's main function, housed in a separate program of course:</p> <pre><code>ClientSocketWrapper clientSocketWrapper = ClientSocketWrapper.createSocketWrapperAndConnectTo("localhost", 18000); /** MAIN ENTRY POINT FOR CLIENT */ while(true) { clientSocketWrapper.updateInputOutputStream(); clientSocketWrapper.writeToOutputStream(executeClientInputProcessing()); updateGameState(clientSocketWrapper.getServerResponse()); } </code></pre> <p>I realize you can't really see what's going on inside those methods, but it's basically just implementing a client side like the Java tutorials do and it works as expected. If you think I need to post the methods running here let me know, but I think the problem is on the server end.</p> <p>BONUS QUESTION: I'm also wondering if I'm on the right track here. The game is fairly complex, but I was just going to use Gson to serialize and deserialize objects and delegate the appropriate actions based on the gson strings that get sent back and forth. After googling server/client architecture, it seems my approach is too simplistic. I'm having a hard time finding good resources to learn more advanced server/client architecture, so any links to great books or tutorials would be a big help.</p> <p>Thanks all!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload