#Java CLI system not allowing me to open multiple clients on Eclipse IDE

23 messages · Page 1 of 1 (latest)

orchid cloak
#

I have created a CLI system that should allow multiple users to communicate with one another using the client, but I am unable to run multiple clients on the console. I really need help to fix this.

solemn flareBOT
#

This post has been reserved for your question.

Hey @orchid cloak! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

high sigil
#

you entered hi but the program expected an int

orchid cloak
#

How do I open multiple clients?

high sigil
#

using multiple threads

#

create one thread per client

orchid cloak
#

How would I do that?

#

package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    try (Socket socket = new Socket("localhost", 1000)) {
        PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
        Scanner scanner = new Scanner(System.in);
        String msg;
        String clientName = "unknown";
        ClientRunnable clientRun = new ClientRunnable(socket);
        new Thread(clientRun).start();
        do {
            if (clientName.equals("unknown")) {
                System.out.print("Enter your name: ");
                msg = scanner.nextLine();
                clientName = msg;
                output.println(msg);
                if (msg.equals("exit")) {
                    break;
                }
            } 
            else {
                msg = scanner.nextLine();
                if (msg.equals("exit")) {
                    output.println("exit");
                    break;
                }
                System.out.println("1. Broadcast");
                System.out.println("2. Private");
                int type = scanner.nextInt();
                scanner.nextLine();
                if(type == 1){
                    output.println(clientName+": " + msg+"$b");
                }
                else if(type == 2){
                    System.out.print("Enter Recevier ID: ");
                    String ID = scanner.next();
                    output.println(clientName+": " + msg+"$"+ID+"$p");
                }
            }
        } while (!msg.equals("exit"));

    } catch (Exception e) {
        System.out.println("Error [Client]: " + e.getMessage());
        e.printStackTrace();
    }
}

}

#

This is the code for the Main java file

#

Sorry I'm new to Java, and I've tried to google all of this but can't seem to find any solutions

bright latch
#

each client has their own console, the server has it's own console

orchid cloak
#

I think I figured it out

#

But the functionality of my code is weird, I have to write out the message before I choose whether I want to broadcast it or pm it

#

Can I change that to be the other way around?

solid saffron
#

yeah, just rearrange your logic flow

orchid cloak
#

done thanks

solemn flareBOT
# orchid cloak done thanks

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.

orchid cloak
#

i need help with this part

#

`package client;

import java.io.PrintWriter;
import java.net.Socket;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
    try (Socket socket = new Socket("localhost", 1000)) {
        PrintWriter output = new PrintWriter(socket.getOutputStream(), true);
        Scanner scanner = new Scanner(System.in);
        String msg = null;
        String clientName = "unknown";
        ClientRunnable clientRun = new ClientRunnable(socket, output);
        new Thread(clientRun).start();
        boolean broadcasting = true;
        while (true) {
            if (clientName.equals("unknown")) {
                System.out.print("Enter your name: ");
                msg = scanner.nextLine();
                clientName = msg;
                output.println(msg);
                if (msg.equals("exit")) {
                    break;
                }
            } else {
                if (broadcasting) {
                    System.out.println("1. Broadcast");
                    System.out.println("2. Private");
                    System.out.println("3. Exit");
                    int type = scanner.nextInt();
                    scanner.nextLine();
                    if (type == 2) {
                        System.out.print("Enter Receiver ID: ");
                        String ID = scanner.next();
                        scanner.nextLine();
                        System.out.print("Enter message: ");
                        msg = scanner.nextLine();
                        if (msg.equals("exit")) {
                            output.println("exit");
                            break;
                        }
                        output.println(clientName + ": " + msg + "$" + ID + "$p");
                        broadcasting = true;
                    } else if (type == 3) {
                        output.println("exit");
                        break;
                    } else {
                        broadcasting = false;
                    }
                }
                System.out.print("Enter message: ");
                msg = scanner.nextLine();
                if (msg.equals("exit")) {
                    output.println("exit");
                    break;
                }
                output.println(clientName + ": " + msg + "$b");
                broadcasting = true;
            }
        }
    } catch (Exception e) {
        System.out.println("Error [Client]: " + e.getMessage());
        e.printStackTrace();
    }
}

}
`

#

i am getting an error with this line
ClientRunnable clientRun = new ClientRunnable(socket, output);

#

this is my clientrunnable code

`package client;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.HashSet;

public class ClientRunnable implements Runnable {
private Socket clientSocket;
private HashSet<String> names;
private BufferedReader input;

public ClientRunnable(Socket clientSocket, HashSet<String> names) {
    this.clientSocket = clientSocket;
    this.names = names;
}

public void broadcastNewUser(String name) {
    for (String user : names) {
        if (!user.equals(name)) {
            try {
                PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
                writer.println(user + " has joined the chat");
            } catch (Exception e) {
                System.out.println("Error broadcasting new user: " + e.getMessage());
            }
        }
    }
}

public void broadcastUserLeft(String name) {
    names.remove(name);
    for (String user : names) {
        try {
            PrintWriter writer = new PrintWriter(clientSocket.getOutputStream(), true);
            writer.println(name + " has left the chat");
        } catch (Exception e) {
            System.out.println("Error broadcasting user left: " + e.getMessage());
        }
    }
}

@Override
public void run() {
    try {
        input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        String line;
        String name = null;
        while ((line = input.readLine()) != null) {
            if (name == null) {
                name = line;
                names.add(name);
                broadcastNewUser(name);
            } else {
                if (line.startsWith("exit")) {
                    broadcastUserLeft(name);
                    break;
                } else {
                    System.out.println(line);
                }
            }
        }
    } catch (Exception e) {
        System.out.println("Error [ClientRunnable]: " + e.getMessage());
    } finally {
        try {
            clientSocket.close();
        } catch (Exception e) {
            System.out.println("Error closing client socket: " + e.getMessage());
        }
    }
}

}`