*** TCP Servidor *** import java.net.*; import java.io.*; public class TCPServidor extends Object implements Runnable { protected ServerSocket servidorSocket; protected Thread escutaPorta; protected Lista lista; protected int count; public TCPServidor(int porta) throws IOException { servidorSocket = new ServerSocket ( porta ); lista = new Lista(); } public void run() { while ( true ) { try { Socket novoCliente = servidorSocket.accept(); TCPConexao conexao = new TCPConexao ( novoCliente, lista, Integer.toString (count), this); lista.adicionar ( conexao ); incCount(); } catch(Exception exception) { exception.printStackTrace(); } } } public synchronized void incCount() { count++; } public synchronized void decCount() { count--; } public void start() { escutaPorta = new Thread ( this ); escutaPorta.start(); } public void stop() { escutaPorta.stop(); escutaPorta = null; System.gc(); } } *** TCP Conexão *** import java.net.*; import java.io.*; class TCPConexao extends Object { protected Socket clientSocket; protected DataInputStream entrada; protected DataOutputStream saida; protected Escutador escutador; protected String ipDest; protected int portDest; public Lista lista; protected String nome, alias; protected Thread temp; protected TCPServidor servidor; public TCPConexao ( Socket clientSocket, Lista lista, String nome, TCPServidor servidor ) { try { this.alias = null; this.servidor = servidor; this.nome = nome; this.lista = lista; this.clientSocket = clientSocket; entrada = new DataInputStream ( clientSocket.getInputStream() ); saida = new DataOutputStream ( clientSocket.getOutputStream() ); escutador = new Escutador (this); temp = new Thread ( escutador ); temp.start(); ipDest = clientSocket.getInetAddress().toString(); portDest = clientSocket.getPort(); System.out.println("Conexao estabelecida com " + ipDest + " em " + Integer.toString ( portDest ) ); } catch (Exception exception) { exception.printStackTrace(); } } public DataInputStream getInputStream() { return this.entrada; } public void incCount () { servidor.incCount(); } public void decCount() { servidor.decCount (); } public String getNome () { return this.nome; } public String getIpDest() { return this.ipDest; } public void setAlias ( String alias ) { this.alias = alias; } public String getAlias() { return this.alias; } public void stop() { temp.stop(); temp=null; System.gc(); } public void mandaMsg ( String conteudo ) { try { saida.writeInt ( conteudo.length() ); saida.writeBytes ( conteudo ); } catch ( Exception e ) { e.printStackTrace(); } } public void imprimirMsg(String msg) { System.out.println("Mensagem do servidor: " + msg); } } *** Lista *** import java.util.*; import java.io.*; class Lista extends Thread { private Object[] clientes; public Lista () { clientes = new Object [ 20 ]; } public synchronized void adicionar ( TCPConexao conexao ) { Integer temp = new Integer ( conexao.getNome() ); clientes[temp.intValue()] = conexao; } public synchronized void remover ( TCPConexao conexao ) { Integer temp = new Integer (conexao.getNome()); clientes[temp.intValue()] = null; } public synchronized void mandarMsg ( String mensagem, String alias ) { for(int i=1; i<=clientes.length; i++) { TCPConexao conexao = (TCPConexao)clientes[i-1]; if((conexao!=null)&&(conexao.getAlias() != alias)) conexao.mandaMsg ( alias + ": " + mensagem ); } //Enumeration clientesAtivos = clientes.elements(); /*try { while ( clientesAtivos.hasMoreElements() ) { TCPConexao conexao = (TCPConexao)clientesAtivos.nextElement(); if ( conexao.getAlias() != alias ) conexao.mandaMsg ( alias + ": " + mensagem ); } } catch ( Exception exception ) { //exception.printStackTrace(); }*/ } } *** Iniciar Servidor *** import java.io.*; public class IniciarServidor { public IniciarServidor(){} public static void main(String[] args) { try { int porta=8085; String portaStr=System.getProperty("porta"); if (portaStr!=null) { try { porta=Integer.parseInt(portaStr); } catch (Exception exception) { exception.printStackTrace(); } } TCPServidor tcpServidor = new TCPServidor ( porta ); tcpServidor.start(); System.out.println("Servidor escutando..."); } catch (Exception ignore) { } } } *** Escutador *** import java.io.*; public class Escutador extends Object implements Runnable { protected DataInputStream entrada; protected TCPConexao tcpConexao; public Escutador ( TCPConexao conexao ) { this.tcpConexao = conexao; this.entrada = tcpConexao.getInputStream(); } public void run() { try { int tamanho = entrada.readInt(); byte[] msg = new byte[tamanho]; entrada.readFully ( msg ); String mensagem = new String ( msg ); tcpConexao.setAlias ( mensagem ); } catch ( Exception exception ) { exception.printStackTrace(); } while(true) { try { int tamanho = entrada.readInt(); byte[] msg = new byte[tamanho]; entrada.readFully ( msg ); String mensagem = new String ( msg ); tcpConexao.lista.mandarMsg ( mensagem, tcpConexao.getAlias() ); } catch(Exception exception) { System.out.println( tcpConexao.getIpDest() + " desconectado deste servidor..." ); tcpConexao.decCount(); tcpConexao.lista.remover (tcpConexao); tcpConexao.stop(); } } } } *** Escuta Cliente *** import java.io.*; public class EscutaCliente extends Object implements Runnable { protected DataInputStream entrada; protected echoClient cliente; public EscutaCliente ( echoClient cliente ) { this.cliente = cliente; this.entrada = cliente.getInputStream(); } public void run() { while(true) { try { int tamanho = entrada.readInt(); byte[] msg = new byte[tamanho]; entrada.readFully ( msg ); String mensagem = new String ( msg ); System.out.println(mensagem); } catch(Exception exception) { System.out.println("cliente desconectado..."); cliente.stop(); System.exit(0); } } } } *** Echo Cliente *** import java.net.*; import java.io.*; public class echoClient { private Socket theSocket; private DataInputStream theInputStream, userInput; private DataOutputStream theOutputStream; private EscutaCliente escutaCliente; private Thread temp; public echoClient ( String hostname ) { try { theSocket = new Socket ( hostname, 8085 ); theInputStream = new DataInputStream ( theSocket.getInputStream() ); theOutputStream = new DataOutputStream ( theSocket.getOutputStream() ); escutaCliente = new EscutaCliente ( this ); temp = new Thread ( escutaCliente ); temp.start(); } catch ( Exception exception ) { exception.printStackTrace(); } } public void mandaMsg ( String conteudo ) { try { theOutputStream.writeInt ( conteudo.length() ); theOutputStream.writeBytes(conteudo); } catch(Exception e) { e.printStackTrace(); } } public void imprimirMsg(String msg) { System.out.println("Mensagem do servidor: " + msg); } public DataInputStream getInputStream() { return this.theInputStream; } public void stop() { temp.stop(); temp=null; System.gc(); } public static void main(String[] args) { String theLine, hostname; DataInputStream userInput = new DataInputStream ( System.in ); if (args.length > 0) { hostname = args[0]; } else { hostname = "localhost"; } echoClient cliente = new echoClient ( hostname ); try { System.out.print("Qual seu nome? "); theLine = userInput.readLine(); cliente.mandaMsg ( theLine ); while (true) { theLine = userInput.readLine(); if (theLine.equals(".")) throw new Exception(); cliente.mandaMsg ( theLine ); } } catch (Exception e) { System.out.println("cliente desconectado..."); cliente.stop(); System.exit(0); } } }