Multi threaded chat server

后端 未结 1 1406
庸人自扰
庸人自扰 2021-01-16 02:11

I am new to Threads. I am trying to broadcast message from the server to clients.but I cannot. It seems like the server is always listen to a new connection.But I can send m

1条回答
  •  抹茶落季
    2021-01-16 02:47

    1. You should listen for clients in a background thread. Your code is not doing this currently and so your server is in fact locked in an endless while (true) loop.
    2. You should refactor your code so that your methods are not so large and unwieldy.
    3. You should refactor your code so that your main method is very brief, so that it simply winds the watch and starts it running, so to speak.
    4. Your code as formatted is very difficult to read. Please consider editing your post and fixing your indentation style so that it is uniform and consistent. I usually avoid using tabs for indenting (forum software often doesn't play well with tabs) and indent each code block 4 spaces.
    5. You almost never want to have your class extend Thread. Instead, have it implement Runnable (or use Rod_Algonquin's excellent recommendation).
    6. Don't use deprecated methods such as DataInputStream#readLine(...) as this could be dangerous to do.

    For example, your main could be as simple as this...

    public static void main(String[] args) {
       MyServer myServer = new MyServer();
       myServer.getThingsRunning();
    }
    

    Edit
    Note added as a warning: I don't generally work with sockets, serversockets, or create chat programs, and I'm still new at using Executors, but you could structure your code something along these lines...

    public class MultiServer implements Runnable {
       public static final int PORT_NUMBER = 2222;
       private static final int THREAD_POOL_COUNT = 20;
       private List clientList = new ArrayList<>();
       private ServerSocket serverSocket;
       private ExecutorService clientExecutor = Executors.newFixedThreadPool(THREAD_POOL_COUNT);
    
       public MultiServer() throws IOException {
          serverSocket = new ServerSocket(PORT_NUMBER);
       }
    
       @Override
       public void run() {
          // embed your socket acceptance loop in a Runnable's run method
          while (true) {
             try {
                Socket clientSocket = serverSocket.accept();
                MultiClient client = new MultiClient(clientSocket);
                clientList.add(client);
                clientExecutor.execute(client);
             } catch (IOException e) {
                // TODO notify someone of problem!
                e.printStackTrace();
             }
          }
       }
    
       // ..... more methods and such
    
       public static void main(String[] args) {
          try {
             MultiServer multiServer = new MultiServer();
             new Thread(multiServer).start();
          } catch (IOException e) {
             e.printStackTrace();
          }
    
       }
    }
    

    0 讨论(0)
提交回复
热议问题