Ver Mensaje Individual
  #1 (permalink)  
Antiguo 27/02/2014, 13:25
jcastro3
 
Fecha de Ingreso: marzo-2013
Mensajes: 51
Antigüedad: 11 años, 1 mes
Puntos: 2
Comprobar si dos clientes tienen el mismo nick

Buenas a todos, estoy haciendo un chat en java, y necesito saber como hacer para comprobar si dos clientes que se conectan tienen el mismo nick. El chat se compone de 4 clases:

La clase inicioCliente

Código Java:
Ver original
  1. import com.sun.org.apache.xalan.internal.xsltc.runtime.BasisLibrary;
  2. import java.util.ArrayList;
  3. import java.util.Vector;
  4. import javax.swing.JOptionPane;
  5.  
  6. /**
  7.  *
  8.  * @author Javier
  9.  */
  10. public class InicioCliente {
  11.  
  12.     /**
  13.      * @param args
  14.      */
  15.     public static void main(String[] args) {
  16.         // TODO Auto-generated method stub
  17.  
  18.         //Declaro la ip y el puerto a conectar
  19.         String ipserver = "localhost";
  20.         String puerto = "7777";
  21.  
  22.    
  23.         //Creo una ventana emergente para pedir el nick, despues se instancia
  24.         //la clase cliente y se le pasa la ip, el puerto y el nick
  25.         String nick = JOptionPane.showInputDialog("Ingrese su Nick",null);
  26.        
  27.                 Cliente charla = new Cliente (ipserver,puerto,nick);
  28.             }
  29.         }

La clase Cliente

Código Java:
Ver original
  1. import java.awt.event.ActionEvent;
  2. import java.awt.event.ActionListener;
  3. import java.awt.event.WindowEvent;
  4. import java.awt.event.WindowListener;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8. import java.io.PrintWriter;
  9. import java.net.Socket;
  10. import java.net.UnknownHostException;
  11. import javax.swing.JButton;
  12. import javax.swing.JFrame;
  13. import javax.swing.JScrollPane;
  14. import javax.swing.JTextArea;
  15. import javax.swing.JTextField;
  16.  
  17. public class Cliente extends JFrame implements ActionListener,WindowListener{
  18.  
  19.  
  20.     private static final long serialVersionUID = 1L;
  21.  
  22.     //Parte grafica, declaro los elementos a usar
  23.    
  24.     JTextField JTEXTO = null; //Campo de escribir mensajes
  25.     JButton JBOTON = null; //Boton de enviar
  26.  
  27.     JTextArea JTAREA = null; //TextArea donde aparecen los mensajes
  28.     JScrollPane scrollpane1 = null; //Panel con scroll que contiene al textArea
  29.    
  30.     //Declaramos ip puerto y nick
  31.     String ipserver;
  32.     String puerto;
  33.     String nick;
  34.  
  35.     //Declaramos el Socket cliente y los canales  de escritura y lectura
  36.    
  37.     Socket sock_c;//Socket cliente
  38.     PrintWriter canal_w;    //Canal de escritura
  39.     BufferedReader canal_r; //Canal de lectura
  40.     //---
  41.  
  42.  
  43.     //Creamos el constructor de la clase
  44.     public Cliente(String ipserver, String puerto, String nick) {
  45.  
  46.         this.ipserver = ipserver;
  47.         this.puerto = puerto;
  48.         this.nick = nick;
  49.        
  50.         //Creamos la interfaz
  51.         crearInterfaz();
  52.  
  53.         try {
  54.             //Se instancian el socket y los buffer
  55.             sock_c = new Socket(this.ipserver,Integer.parseInt(this.puerto));
  56.             canal_w = new PrintWriter(sock_c.getOutputStream(),true);
  57.             canal_r = new BufferedReader(new InputStreamReader(sock_c.getInputStream()));
  58.  
  59.             canal_w.println(nick);
  60.  
  61.             while(true){
  62.                 String linea = canal_r.readLine();
  63.                 //Si se escribe FIN se termina el programa (Ejercicio 1 de toda esta practica)
  64.                 if(linea.equals("FIN")){
  65.                     System.exit(0);
  66.                 }else{
  67.                     //Se añaden los mensajes al textArea
  68.                     JTAREA.append(linea + "\n");
  69.                 }
  70.             }
  71.  
  72.         } catch (NumberFormatException e) {
  73.             // TODO Auto-generated catch block
  74.             e.printStackTrace();
  75.         } catch (UnknownHostException e) {
  76.             // TODO Auto-generated catch block
  77.             e.printStackTrace();
  78.         } catch (IOException e) {
  79.             // TODO Auto-generated catch block
  80.             e.printStackTrace();
  81.         }
  82.  
  83.     }//Fin constructor
  84.  
  85.     private void crearInterfaz(){
  86.         //Ventana principal
  87.         this.addWindowListener(this);
  88.         this.setLayout(null);
  89.         this.setSize(450, 450);
  90.         this.setResizable(false);//Se establece el tamaño de la ventana
  91.         this.setTitle("Chat - " + this.nick);
  92.  
  93.         //TextArea
  94.         JTAREA = new JTextArea(); //TextArea donde aparecen los mensajes
  95.         JTAREA.setBounds(10, 10, 370, 370); //Se da un tamaño
  96.         JTAREA.setEditable(false); //Se establece que no se puede editar
  97.         this.add(JTAREA);
  98.  
  99.         //Panel con scroll donde esta el textArea donde aparecen los mensajes
  100.         scrollpane1 = new JScrollPane(JTAREA);
  101.         scrollpane1.setBounds(10, 10, 370, 370);//Se da un tamaño
  102.         scrollpane1.getVerticalScrollBar().setValue(scrollpane1.getVerticalScrollBar().getMaximum());
  103.         this.add(scrollpane1);
  104.  
  105.         //Caja de texto donde escribimos el mensaje
  106.         JTEXTO = new JTextField();
  107.         JTEXTO.setBounds(10, 390, 260, 20); //Se da un tamaño
  108.         this.add(JTEXTO);
  109.  
  110.         //Boton enviar
  111.         JBOTON= new JButton();
  112.         JBOTON.setBounds(280, 390, 100, 20); //Se da un tamaño
  113.         JBOTON.setText("Enviar"); //Se establece el texto del boton
  114.         JBOTON.addActionListener(this);
  115.         this.add(JBOTON);
  116.  
  117.         this.setVisible(true);
  118.     }
  119.  
  120.  @Override
  121.     public void windowActivated(WindowEvent arg0) {
  122.         // TODO Auto-generated method stub
  123.  
  124.     }
  125.  
  126.     @Override
  127.     public void windowClosed(WindowEvent arg0) {
  128.         // TODO Auto-generated method stub
  129.  
  130.     }
  131.  
  132.     @Override
  133.     public void windowClosing(WindowEvent arg0) {
  134.         // TODO Auto-generated method stub
  135.         canal_w.println("FIN");
  136.     }
  137.  
  138.     @Override
  139.     public void windowDeactivated(WindowEvent arg0) {
  140.         // TODO Auto-generated method stub
  141.  
  142.     }
  143.  
  144.     @Override
  145.     public void windowDeiconified(WindowEvent arg0) {
  146.         // TODO Auto-generated method stub
  147.  
  148.     }
  149.  
  150.     @Override
  151.     public void windowIconified(WindowEvent arg0) {
  152.         // TODO Auto-generated method stub
  153.  
  154.     }
  155.  
  156.     @Override
  157.     public void windowOpened(WindowEvent arg0) {
  158.         // TODO Auto-generated method stub
  159.  
  160.     }
  161.  
  162.     @Override
  163.     public void actionPerformed(ActionEvent arg0) {
  164.         // TODO Auto-generated method stub
  165.  
  166.         Object control = arg0.getSource();
  167.  
  168.         if(control instanceof JButton){
  169.             canal_w.println(JTEXTO.getText());
  170.             JTEXTO.setText("");
  171.         }
  172.  
  173.     }
  174.  
  175. }