Java Sockets: One Server and Multiple Clients

16,271

Solution 1

Incoming connections are only handled by the line listener.accept();. But after you got a client connected, you're stuck in the while loop. You need to create a new Thread (or Runnable executed on an ExecutorService if you expect high load), and start it, then immediately accept the next connection.

Solution 2

In a nutshell, this is what is going wrong.

  1. You are using exactly ONE thread as the server.
  2. Blocking this thread when you call listener.accept()

This is what you need to do:

Create two classes 1: Server - Similar to what you have now, but instead of doing the actual work of acting as an echo server, it just spawns a new Thread which starts listening on a NEW PORT (which you can select randomly), and sends the client the address for this new port. The client will then get the new port number and would try to connect to the server on the new port. 2: The Echo thread - This starts a new listener on the port passed, and does the job of echoing to whoever is listening.

OR:

You start a UDP server rather than a TCP server, and all this will not matter then, but that is out of the purview of this specific question.

Share:
16,271

Related videos on Youtube

projectdelphai
Author by

projectdelphai

Updated on September 14, 2022

Comments

  • projectdelphai
    projectdelphai about 1 year

    So I created a basic client-server program in java. It starts out like this:

    1. Client connects to Server
    2. Server asks for Client's name
    3. Client responds with name
    4. Server greets Client
    5. After this, Client speaks and the Server repeats the words back

    I got this to work without too much trouble using this tutorial. The problem comes whenever I try to introduce multiple clients. I thought that it would work because I'm using multiple threads, however, the second clients just hangs until the first client quits and then it starts it work (the server does accept input from the second client, but it doesn't respond with anything until the first client quits).

    Here is the code I'm using:

    import java.net.*;
    import java.io.*;
    
    public class Server extends Thread {
      private ServerSocket listener;
    
      public Server(int port) throws IOException {
        listener = new ServerSocket(port);
      }
    
      public void run() {
        while(true) {
          try {
            Socket server = listener.accept();
            DataOutputStream out = new DataOutputStream(server.getOutputStream());
            out.writeUTF("What is your name?");
            DataInputStream in = new DataInputStream(server.getInputStream());
            String user_name = in.readUTF();
            out.writeUTF("Hello "+user_name);
            while(true) {
              String client_message = in.readUTF();
              out.writeUTF(client_message);
            }
          }
          catch(IOException e) {
            e.printStackTrace();
          }
        }
      }
    
      public static void main(String[] args) {
        int port = 6006;
        try {
          Thread t = new Server(port);
          t.start();
        } catch(IOException e) {
          e.printStackTrace();
        }
      }
    }
    

    Can someone explain what I'm doing wrong?

    I have looked at the using Runnable instead of Extends Thread, but I ran into even more problems there, so I want to try and work with this first.

  • dst
    dst about 10 years
    @AlexeiKaigorodov indeed easy to misunderstand that, edited that word now.