0

I've got a project to design a chat client in java applets....I've got a way around to implement it but that may be bit an inefficient

I've got a client code that communicates with a server program (both in java).Only one client can communicate with server that one time and in case of multiple clients threads are created but the threads can't communicate with each other...
I just wanna ask is there are any standard functions in java that could allow
my server program to interact with 2 client programs other than out.println(outputLine) && n.readLine() that could form i/o pipeline with one client at a time Here's my client code

kkSocket = new Socket("localhost", 8080);
out = new PrintWriter(kkSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: taranis.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: taranis.");
System.exit(1);
}


BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;


while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
if (fromServer.equals("Bye."))
break;


fromUser = stdIn.readLine();
if (fromUser != null) {
System.out.println("Client: " + fromUser);
out.println(fromUser);

Here's my server code

ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(1);
}


Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}


PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
KnockKnockProtocol kkp = new KnockKnockProtocol();


outputLine = kkp.processInput(null);
out.println(outputLine);


while ((inputLine = in.readLine()) != null) {
outputLine = kkp.processInput(inputLine);
out.println(outputLine);
if (outputLine.equals("Bye."))
break;

Edited by Nick Evan: Fixed formatting

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Ezzaral
0

When a client connects, just add the client output stream to a collection (ArrayList should be just fine) which you can then loop through to broadcast the message along to all clients.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.