Question

I am programming client/server game. I am using Eclipse. Client is with GUI by Swing. Until now, I ran server once and then I ran client, right click on class --> run as . Everything was ok. Now i need to run client class twice. I tried when server and client were running, just again rightclick on client -> run as. There was two client processes but only one GUI JFrame.

I want to test it this way, run server, run client, run client so two clients are running and then on first client I want to click on first button ( It's new game ) and on second client I want to click second button ( it's existing game connection ) but for me it's unable to run two GUI for two client processes. I have only first GUI for first client and then only running process of second client ( NO GUI )

Thank you very much,

Marek

EDIT :

SERVER - main

public static void main(String [] argv) throws IOException,ClassNotFoundException {
    ServerListen server = new ServerListen(PORT);
    server.acceptConnection();
}

SERVER - acceptConnection();

    Socket dataSocket = null;
    ObjectInputStream readBuffer = null;
    ObjectOutputStream writeBuffer = null;
    while( true ) {
        try{
            dataSocket = socket.accept();
            System.err.println("!!! Connection !!!");
            writeBuffer = new ObjectOutputStream(dataSocket.getOutputStream());
            writeBuffer.flush();
            readBuffer = new ObjectInputStream((dataSocket.getInputStream()));

            writeBuffer.writeObject("OKlisten");
            writeBuffer.flush();
            System.err.println("Client connected");
            new Handler(dataSocket,readBuffer,writeBuffer).runGame(list_of_games);
        } catch (IOException e) {
            System.err.println(e);
            System.exit(1);
        }
    }
}

This is running only once.

CLIENT - main

public static void main(String [] argv) throws IOException, ClassNotFoundException {
    Client client = new Client(PORT);
    final Basic frame = new Basic(client);
    frame.setVisible(true);
}

P.S - you see the println of !!! Connection !!! and when I do right-click --> run as on client second time there is no second !!! Connection !!! message

THANK YOU VERY MUCH

EDIT 2.

public Client(int PORT)
{
    try{
        this.clientSocket = new Socket(serverName, PORT);
        sendMessage = new ObjectOutputStream(clientSocket.getOutputStream());
        sendMessage.flush();
        getMessage = new ObjectInputStream(clientSocket.getInputStream());
    }

This is part of code, in second client, it froze on getMessage line.

Was it helpful?

Solution

You need to run any long-running bits of code, such as your while (true) loop in a background thread, else you risk tying up the Swing event thread, freezing your application.

For example, your code could look something like:

// implement Runnable so it can be run in a background thread
public class MultiServer2 implements Runnable {
   public static final int PORT_NUMBER = 2222;
   private static final int THREAD_POOL_COUNT = 20;
   private List<MultiClientHandler> clientList = new ArrayList<>();
   private ServerSocket serverSocket;
   private ExecutorService clientExecutor = Executors.newFixedThreadPool(THREAD_POOL_COUNT);

   public MultiServer2() throws IOException {
      serverSocket = new ServerSocket(PORT_NUMBER);
   }

   @Override
   public void run() {
      // do this in the run method so that it runs in a background thread
      while (true) {
         try {
            Socket clientSocket = serverSocket.accept();

            // MultiClient2 also implements Runnable
            MultiClientHandler client = new MultiClientHandler(clientSocket);
            clientList.add(client);

            // and each socket's code needs to be run in its own thread
            clientExecutor.execute(client);
         } catch (IOException e) {
            // TODO notify someone of problem!
            e.printStackTrace();
         }
      }
   }

with a main method that looks something like:

public static void main(String[] args) {
  try {
     MultiServer2 multiServer = new MultiServer2();
     new Thread(multiServer).start();  // this runs the runnable
  } catch (IOException e) {
     e.printStackTrace();
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top