I am creating a client/server program with sockets. Each time a new client connects it creates a new instance of the newClient class which handles the connection and creates a new PrintStream for that client. I then want to output the same data to each of the PrintStreams for the clients. How do I do this?
public void startServer(){
Thread serverstart = new Thread(){
public void run() {
try {
serversocket = new ServerSocket(socket);
while(true){
Socket skt = serversocket.accept();
new Thread(new newClient(skt)).start();
}
} catch (IOException e){
e.printStackTrace();
}
}
};serverstart.start();
}
class newClient implements Runnable {
private Socket socket;
public newClient(Socket skt){
this.socket = skt;
}
@Override
public void run(){
try {
PrintStream output = new PrintStream(socket.getOutputStream());
} catch(IOException ioe){
ioe.printStackTrace();
}
}
}
Here is the method that sends the data:
public void sendData(){
if(preview == 1)
tallydata = "1";
else if(preview == 2)
tallydata = "2";
else if(preview == 3)
tallydata = "3";
else if(preview == 4)
tallydata = "4";
else if(preview == 5)
tallydata = "5";
else if(preview == 6)
tallydata = "6";
if(program == 1)
tallydata = tallydata + " 1";
if(program == 2)
tallydata = tallydata + " 2";
if(program == 3)
tallydata = tallydata + " 3";
if(program == 4)
tallydata = tallydata + " 4";
if(program == 5)
tallydata = tallydata + " 5";
if(program == 6)
tallydata = tallydata + " 6";
output.print(tallydata);
try{
serversocket.close();
socketname.close();
} catch(IOException e){
e.printStackTrace();
}
Thanks in advance!