I've been trying this for a while, and I want multiple clients to recieve multiple inputs simultaneously.
There is one problem, I want the server to print "Hi" to all clients if one client says 'print2all Hi'.
I know how to process it to print it, just to print to ALL clients is the problem.
Here's what I have so far.
Server
try{
try{
server = new ServerSocket(25565);
} catch (Exception e){
e.printStackTrace();
}
while (isListening){
new SocketThread(server.accept()).start();
}
server.close();
} catch (Exception e){
e.printStackTrace();
}
SocketThread
try {
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String inputLine, outputLine;
Processor kkp = new Processor();
out.println("Hi!");
while ((inputLine = in.readLine()) != null) {
outputLine = kkp.Proccess(inputLine,this.socket);
out.println(outputLine);
}
out.close();
in.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
}
Client
Processor p = new Processor();
socket = new Socket("localhost",25565);
out = new PrintWriter(socket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
out.println("print2all Hi")
socket.close();
First you need to keep track of all connected clients:
Having such list if one client receives
"print2all Hi"
it simply iterates over allclients
and sends message to each of them. To do this you'll most likely have to expose some method onSocketThread
that will access client socket. This means you'll have to changeout
variable to field.Alternative approach is to keep a list of client sockets. But this breaks encapsulation badly. Also you might run into nasty IO/thread-safety issues if sockets are exposed directly. Better hide them behind some API (like
SocketThread
method) and do the synchronization properly inside.A full implementation of what you are looking.
Server
Client