0

I have written a simple multithreaded Server on which the two Clients can send Messages to the Server an the Server a Message to all the Clients at once. But I can't get it to work as intended.

I already tried it by putting a List of all PrintWriters in the Server class and then print the Message through each PrintWriter but this didn't work either.

public class Client {

private static final String IP = "10.59.0.188";

private Socket clientSocket;
private PrintWriter toServer;
private BufferedReader fromServer;
private BufferedReader input;
private String serverMessage;
private String clientMessage;
private String name;

public static void main(String[] args) {
    try {
        new Client();
    } catch (Exception e) {
        System.err.println(e);
    }
}

public Client() throws IOException {
    input = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Name: ");
    name = input.readLine();
    openConnection();
    toServer.println(name);

    while (true) {
        clientMessage = input.readLine();
        toServer.println(clientMessage);
    }

    //closeConnection();
}

private void openConnection() throws IOException{
    clientSocket = new Socket(IP, 6666);
    toServer = new PrintWriter(clientSocket.getOutputStream(), true);
    fromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
}

}

public class Server {

private ServerSocket serverSocket;
private BufferedReader input;
private Vector<ClientProcess> processList;
private int clientCount = 0;
private String serverMessage;

public static void main(String[] args) {
    try {
        new Server();
    } catch (Exception e) {
        System.err.println(e);
    }
}

public Server() throws IOException {
    startServer();

    while (clientCount < 2) {
        waitForNewClient();
    }

    for (ClientProcess clientProcess : processList) {
        clientProcess.start();
        System.out.println("Clientprocess started");
    }        
}

private void startServer() throws IOException {
    processList = new Vector<>();
    input = new BufferedReader(new InputStreamReader(System.in));
    serverSocket = new ServerSocket(6666);
    System.out.println("Server online");
}

private void waitForNewClient() throws IOException {
    System.out.println("Waitin' for new Client...");
    Socket clientSocket = serverSocket.accept();
    ClientProcess clientProcess = new ClientProcess(clientSocket);
    processList.add(clientProcess);
    clientCount++;
}

}

public class ClientProcess extends Thread {

private Socket clientSocket;
private PrintWriter toClient;
private BufferedReader fromClient;
private String clientMessage;
private String serverMessage;
private String name;
private BufferedReader input;

public ClientProcess(Socket clientSocket) {
    this.clientSocket = clientSocket;
    input = new BufferedReader(new InputStreamReader(System.in));
}

@Override
public void run() {
    try {
        openClientConnection();
        name = fromClient.readLine();

        do {
            clientMessage = fromClient.readLine();
            System.out.println(name + ": " + clientMessage);
        } while (true);

        //closeClientConnection();
    } catch (IOException e) {
        System.err.println(e);
    }
}

private void openClientConnection() throws IOException {
    toClient = new PrintWriter(clientSocket.getOutputStream());
    fromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    serverMessage = "ServerMessageTest00";
    toClient.println(serverMessage);
    System.out.println("Client Connection Online");
}

}

1 Answers1

0

In the ClientProcess you create a new PrintWriter.

From the javadoc:

Creates a new PrintWriter, without automatic line flushing, from anexisting OutputStream.

This means your toClient.println(...); is wrote to the server's output buffer but will not be send to the client because you miss the toClient.flush().

Grim
  • 1,938
  • 10
  • 56
  • 123