#Java CLI system not allowing me to open multiple clients on Eclipse IDE
23 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @orchid cloak! Please use
/closeor theClose Postbutton 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.
you entered hi but the program expected an int
How do I open multiple clients?
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
what is the exact functionality you are looking for? multiple client on the console doesn't seem possible
each client has their own console, the server has it's own console
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?
yeah, just rearrange your logic flow
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.
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());
}
}
}
}`