Search code examples
javaserversocket

Accepting multiple connections from clients on a serversocket


I just started using Sockets, and for my current project I need to be able to control my program from a client, however if my project-partner wants use his client at the same time, the server doesn't send him the "You are connected" message as shown in the connection class. So I assume the server doesn't accept mutiple clients at the same time. I have tried using a Thread of the class Connection, but that also doesn't send the message "You are connected" to the second Client. What am I doing wrong here?

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

public class Listener extends Thread{

private ServerSocket server;
private int PORT;
public boolean running;

public Listener(int port){
    try{
        this.PORT = port;
        this.server = new ServerSocket(PORT,10);
    }catch (IOException e) {
        System.out.println("Could not create serverSocket...");
    }
}

@Override
public void run() {
    this.running = true;
    try{
        waitForConnection();
    }catch(IOException e){
        System.out.println("Could not accept connection request..");
        run();
    }


}
public void dispose(){
    try{
        System.out.println("DISPOSE");
        running = false;
        server.close();
    } catch (IOException i) {
        System.out.println("Could not close ServerSocket");
    }
}
private void waitForConnection() throws IOException{
    while(running){
        System.out.println("Waiting for connection");
        Socket client = server.accept();
        Runnable connection = new Connection(client);
        new Thread(connection).start();
    }
}       
}

This is the Thread I'm using to have multiple users connect at the same time:

 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
 import java.net.Socket;

public class Connection extends Thread {
     Socket connection;
     private ObjectOutputStream output;
     private ObjectInputStream input;
     private boolean running;

     public Connection(Socket connect){
          this.connection = connect;
          try {
              setupStreams();
              whileListening();
          } catch (IOException e) {
                System.out.println("could not connect to: "+ connection.getInetAddress().getHostName());
    }
}

public void dispose(){
    try{
        output.close();
        input.close();
        connection.close();
        running = false;
    }catch(IOException ioException){
        ioException.printStackTrace();
    }
}   

private void whileListening(){
    String message = "You are connected! ";
    sendMessage(message);
    do{
        try{
            message = (String) input.readObject();
            checkMessage(message);
        }catch(ClassNotFoundException classNotFoundException){
            sendMessage("tf did you send? ");
        }catch (IOException e) {
            dispose();
            run();
        }
    }while(!message.equals("Client - END") && running == true);
}

private void setupStreams() throws IOException{
    output = new ObjectOutputStream(connection.getOutputStream());
    output.flush();
    input = new ObjectInputStream(connection.getInputStream());
}

private  void sendMessage(String message){
    try {
        output.writeObject("Server - " + message+"\n");
        output.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

private void checkMessage(String text){
        //check the message
}
}     

EDIT: Addittional information

Before the first client connects, the server console says "Waiting for connection", then when the first client connects, the client console says "You are connected" and, when a second client connects, the console is black, when I close the first client, the second client console says "You are connected" and the server console says "Waiting for connection", then if I close the second client aswell, the server console says "Waiting for connection" again.


Solution

  • In your constructor of the public class Connection extends Thread you do this whileListening()stuff, so your constructor never ends, you need to override the run() function and do that there

    @Override
    public void run() {
        while(true) {
            try {
                whileListening();
            } catch(Exception e) {}
        }
    }
    

    like so, it should do the trick.