Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package servidor;
  2.  
  3. import java.io.EOFException;
  4. import java.io.IOException;
  5. import java.io.ObjectInputStream;
  6. import java.net.Socket;
  7. import java.net.SocketException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10.  
  11. public class ThreadRecibe implements Runnable {
  12.     private final PrincipalChat main;
  13.     private String mensaje;
  14.     private ObjectInputStream entrada;
  15.     private Socket cliente;
  16.    
  17.    
  18.    //Inicializar chatServer y configurar GUI
  19.    public ThreadRecibe(Socket cliente, PrincipalChat main){
  20.        this.cliente = cliente;
  21.        this.main = main;
  22.    }  
  23.  
  24.     public void mostrarMensaje(String mensaje) {
  25.         main.areaTexto.append(mensaje);
  26.     }
  27.    
  28.     public void run() {
  29.         try {
  30.             entrada = new ObjectInputStream(cliente.getInputStream());
  31.         } catch (IOException ex) {
  32.             Logger.getLogger(ThreadRecibe.class.getName()).log(Level.SEVERE, null, ex);
  33.         }
  34.         do { //procesa los mensajes enviados dsd el servidor
  35.             try {//leer el mensaje y mostrarlo
  36.                 mensaje = (String) entrada.readObject(); //leer nuevo mensaje
  37.                 main.mostrarMensaje(mensaje);
  38.             } //fin try
  39.             catch (SocketException ex) {
  40.             }
  41.             catch (EOFException eofException) {
  42.                 main.mostrarMensaje("Fin de la conexion");
  43.                 break;
  44.             } //fin catch
  45.             catch (IOException ex) {
  46.                 Logger.getLogger(ThreadRecibe.class.getName()).log(Level.SEVERE, null, ex);
  47.             } catch (ClassNotFoundException classNotFoundException) {
  48.                 main.mostrarMensaje("Objeto desconocido");
  49.             } //fin catch              
  50.  
  51.         } while (!mensaje.equals("Servidor>>> TERMINATE")); //Ejecuta hasta que el server escriba TERMINATE
  52.  
  53.         try {
  54.             entrada.close(); //cierra input Stream
  55.             cliente.close(); //cieraa Socket
  56.         } //Fin try
  57.         catch (IOException ioException) {
  58.             ioException.printStackTrace();
  59.         } //fin catch
  60.  
  61.         main.mostrarMensaje("Fin de la conexion");
  62.         System.exit(0);
  63.     }
  64. }