Bom pessoal, nao sabendo nem sequer por onde começar
a logica que pensei seria assim: qdo o cliente desconectar , tirar ele da lista de clientes pra qual o serv envia todas mensagens
dado esse sistema abaixo, alguma dica sobre como fazer?
import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
public class Servidor {
public static void main(String[] args) throws IOException {
// inicia o servidor
new Servidor(12345).executa();
}
private int porta;
private boolean encerrar;
private ArrayList<PrintStream> clientes;
public Servidor (int porta) {
this.porta = porta;
this.clientes = new ArrayList<PrintStream>();
}
public void executa () throws IOException {
ServerSocket servidor = new ServerSocket(this.porta);
System.out.println("Porta 12345 aberta!");
while (!encerrar) {
// aceita um cliente
Socket cliente = servidor.accept();
System.out.println("Nova conexão com o cliente " +
cliente.getInetAddress().getHostAddress());
// adiciona saida do cliente à lista
PrintStream ps = new PrintStream(cliente.getOutputStream());
this.clientes.add(ps);
// cria tratador de cliente numa nova thread
TrataCliente tc = new TrataCliente(cliente.getInputStream(), this);
new Thread(tc).start();
}
}
public void distribuiMensagem(String msg) {
// envia msg para todo mundo
for (PrintStream cliente : this.clientes) {
cliente.println(msg);
System.out.println(msg);
}
}
}
import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;
public class Cliente {
public static void main(String[] args) throws UnknownHostException, IOException {
// dispara cliente
String nome = args.length>0?args[0]:"sem nome";
new Cliente("127.0.0.1", 12345, nome).executa();
}
private String host;
private int porta;
private String nome;
public Cliente (String host, int porta, String nome) {
this.host = host;
this.porta = porta;
this.nome = nome;
}
public void executa() throws UnknownHostException, IOException {
Socket cliente = new Socket(this.host, this.porta);
System.out.println("O cliente se conectou ao servidor!");
// thread para receber mensagens do servidor
Recebedor r = new Recebedor(cliente.getInputStream());
new Thread(r).start();
// le msgs do teclado e manda pro servidor
Scanner teclado = new Scanner(System.in);
PrintStream saida = new PrintStream(cliente.getOutputStream());
while (teclado.hasNextLine()) {
saida.println(nome + ": " + teclado.nextLine());
}
saida.close();
teclado.close();
cliente.close();
}
}
import java.io.InputStream;
import java.util.Scanner;
public class TrataCliente implements Runnable {
private InputStream cliente;
private Servidor servidor;
public TrataCliente(InputStream cliente, Servidor servidor) {
this.cliente = cliente;
this.servidor = servidor;
}
public void run() {
// quando chegar uma msg, distribui pra todos
Scanner s = new Scanner(this.cliente);
while (s.hasNextLine()) {
servidor.distribuiMensagem(s.nextLine());
}
s.close();
}
}
import java.io.InputStream;
import java.util.Scanner;
public class Recebedor implements Runnable{
private InputStream servidor;
public Recebedor(InputStream servidor) {
this.servidor = servidor;
}
public void run() {
// recebe msgs do servidor e imprime na tela
Scanner s = new Scanner(this.servidor);
while (s.hasNextLine()) {
System.out.println(s.nextLine());
}
}
}
