Antes que nada saludos y gracias de antemano.
Tengo un problema al hacer un chat multi-cliente, cuando ejecuto el servidor y un cliente de la aplicacion todo funciona perfecto el problema viene cuando ejecuto el segundo cliente y envio un mensaje por el chat. Al enviar un mensaje desde el segundo cliente el JTextArea que es donde estan las conversaciones se agrega el mensaje dos veces y el JTextArea del primer cliente no se actualiza y deja de poner enviar mensajes.
Aqui les subire las clases de donde creo que ocurre el problema.
package com.gestion.garage.servidor;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import javax.swing.DefaultListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
private DefaultListModel<String> conversacion;
public HiloDeCliente
(DefaultListModel
<String
> conversacion,
Socket socket
){ this.conversacion = conversacion;
this.socket = socket;
try{
conversacion.addListDataListener(HiloDeCliente.this);
}
e.printStackTrace();
}
}
dataSalida.close();
dataEntrada.close();
}
// No hace nada
String texto
= (String) conversacion.
getElementAt(e.
getIndex0()); try{
dataSalida.writeUTF(texto);
}
ex.printStackTrace();
}
}
//No hace nada
}
public void run() {
try{
while(true){
String texto
= dataEntrada.
readUTF(); synchronized(conversacion){
conversacion.addElement(texto);
System.
out.
println("Hay "+conversacion.
getSize()+" elementos"); }
}
e.printStackTrace();
}
}
}
package com.gestion.garage.InterfazGrafica;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.Socket;
private VentanaAdministrador panel;
public ControlCliente
(Socket socket,VentanaAdministrador panel
){ this.panel = panel;
try{
panel.addActionListener(this);
hilo.start();
}
ex.printStackTrace();
}
}
try
{
dataSalida.writeUTF(panel.getTexto());
{
excepcion.printStackTrace();
}
}
public void run() {
try
{
while (true)
{
String texto
= dataEntrada.
readUTF(); panel.addTexto(texto);
panel.addTexto("\n");
}
{
e.printStackTrace();
}
}
}
package com.gestion.garage.InterfazGrafica;
import java.awt.Font;
public class Login
extends JFrame { private VentanaAdministrador panel;
public Login(){
setTitle("Login");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setSize(354,308);
getContentPane().setLayout(null);
int eleccion
= JOptionPane.
showConfirmDialog(null,
"¿Desea cerrar esta ventana?"); if(eleccion==0){
try {
HiloDeCliente.cerrarFlujos();
e.printStackTrace();
}
}
}
});
txtUsuario.setBounds(98, 55, 143, 20);
getContentPane().add(txtUsuario);
txtUsuario.setColumns(10);
txtPassword.setBounds(98, 132, 143, 20);
getContentPane().add(txtPassword);
lblUsuario.
setFont(new Font("Cambria Math",
Font.
BOLD,
15)); lblUsuario.setBounds(137, 27, 66, 14);
getContentPane().add(lblUsuario);
lblPassword.
setFont(new Font("Cambria Math",
Font.
BOLD,
15)); lblPassword.setBounds(131, 105, 82, 14);
getContentPane().add(lblPassword);
System.
out.
println(ConexionMySql.
getInstancia().
verificarUsuario(txtUsuario.
getText())); if(ConexionMySql.getInstancia().verificarUsuario(txtUsuario.getText())){
try {
new ControlCliente
(new Socket("localhost",
5557),
new VentanaAdministrador
(ConexionMySql.
getInstancia().
obtenerNombre(txtUsuario.
getText()))); e.printStackTrace();
}
Login.this.dispose();
}
else{
JOptionPane.
showMessageDialog(null,
"No hay ningun usuario registrado con estos datos"); }
}
});
btnIniciar.setBounds(124, 181, 89, 23);
getContentPane().add(btnIniciar);
new VentanaRegistro();
Login.this.dispose();
}
});
lblRegistrarse.
setFont(new Font("Cambria Math",
Font.
BOLD,
15)); lblRegistrarse.setBounds(225, 238, 89, 20);
getContentPane().add(lblRegistrarse);
setVisible(true);
}
}
package com.gestion.garage.servidor;
import java.net.ServerSocket;
import java.net.Socket;
import javax.swing.DefaultListModel;
public class ServidorChat {
private DefaultListModel<String> conversacion = new DefaultListModel<String>();
public static void main
(String[] args
){ new ServidorChat();
}
public ServidorChat(){
try{
while(true){
Socket cliente
= socketServidor.
accept(); Runnable nuevoCliente
= new HiloDeCliente
(conversacion,cliente
); hilo.start();
}
e.printStackTrace();
}
}
}
package com.gestion.garage.InterfazGrafica;
import java.awt.Font;
public class VentanaAdministrador
extends JFrame{ private static final int precio = 0;
super("Garage - El Gomerito - ");
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
setSize(764,421);
getContentPane().setLayout(null);
int eleccion
= JOptionPane.
showConfirmDialog(null,
"¿Desea cerrar esta ventana?"); if(eleccion==0){
new Login();
VentanaAdministrador.this.dispose();
}
}
});
tabbedPane.setBounds(0, 0, 748, 394);
getContentPane().add(tabbedPane);
tabbedPane.addTab("Registro", null, pnAlta, null);
pnAlta.setLayout(null);
if(cbTipoVehiculo.getSelectedIndex() == 0){
}
else{
}
}
});
cbTipoVehiculo.setBounds(202, 11, 107, 20);
pnAlta.add(cbTipoVehiculo);
lblTipoVehiculo.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblTipoVehiculo.setBounds(10, 11, 122, 17);
pnAlta.add(lblTipoVehiculo);
lblRuedas.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblRuedas.setBounds(10, 60, 122, 14);
pnAlta.add(lblRuedas);
cbRuedas.setBounds(202, 57, 107, 20);
pnAlta.add(cbRuedas);
cbModelo.setBounds(202, 107, 107, 20);
pnAlta.add(cbModelo);
lblModelo.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblModelo.setBounds(10, 107, 122, 20);
pnAlta.add(lblModelo);
lblPrecio
= new JLabel("0.00"); lblPrecio.
setFont(new Font("Tahoma",
Font.
BOLD | Font.
ITALIC,
32)); lblPrecio.setBounds(202, 297, 92, 29);
pnAlta.add(lblPrecio);
btnRegistrar
= new JButton("Registrar"); btnRegistrar.setEnabled(false);
if(txtMarca.getText().equals("") || txtPropietario.getText().equals("") || txtPlaca.getText().equals("")){
JOptionPane.
showMessageDialog(null,
"Debe llenar todos los campos antes de registrar"); }
else{
ModeloTabla.getInstancia().agregarVehiculo(new Vehiculo(txtMarca.getText(),txtPropietario.getText(),
Integer.
parseInt((String)cbRuedas.
getSelectedItem()),txtPlaca.
getText())); txtPropietario.setText("");
txtMarca.setText("");
txtPlaca.setText("");
}
}
});
btnRegistrar.
setFont(new Font("Tahoma",
Font.
BOLD,
13)); btnRegistrar.setBounds(538, 305, 107, 29);
pnAlta.add(btnRegistrar);
btnCalcular
= new JButton("Calcular"); btnCalcular.
setFont(new Font("Tahoma",
Font.
BOLD,
13)); if(cbTipoVehiculo.getSelectedIndex()==0 && cbRuedas.getSelectedIndex()==-1 && cbModelo.getSelectedIndex()==-1){
JOptionPane.
showMessageDialog(null,
"Debe seleccionar el tipo de vehiculo"); }
else{
btnRegistrar.setEnabled(true);
if(cbTipoVehiculo.getSelectedItem().equals("Coche")){
lblPrecio.
setText(String.
valueOf(400*Integer.
parseInt((String)cbRuedas.
getSelectedItem()))+"$"); }
else{
lblPrecio.
setText(String.
valueOf(400*Integer.
parseInt((String)cbRuedas.
getSelectedItem()))+"$"); }
}
}
});
btnCalcular.setBounds(38, 309, 89, 23);
pnAlta.add(btnCalcular);
txtPlaca.setBounds(202, 147, 107, 20);
pnAlta.add(txtPlaca);
txtPlaca.setColumns(10);
txtPropietario.setBounds(202, 199, 107, 20);
pnAlta.add(txtPropietario);
txtPropietario.setColumns(10);
lblPlaca.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblPlaca.setBounds(10, 153, 122, 14);
pnAlta.add(lblPlaca);
lblPropietario.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblPropietario.setBounds(10, 200, 122, 14);
pnAlta.add(lblPropietario);
lblMarca.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblMarca.setBounds(10, 251, 122, 14);
pnAlta.add(lblMarca);
txtMarca.setBounds(202, 246, 107, 20);
pnAlta.add(txtMarca);
txtMarca.setColumns(10);
lblBienvenido.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblBienvenido.setBounds(378, 14, 116, 17);
pnAlta.add(lblBienvenido);
lblNombreUsuario.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); lblNombreUsuario.setBounds(504, 11, 212, 20);
pnAlta.add(lblNombreUsuario);
tabbedPane.addTab("Chat", null, pnChat, null);
pnChat.setLayout(null);
scrollPane.setBounds(0, 0, 733, 294);
pnChat.add(scrollPane);
scrollPane.setViewportView(txtChat);
txtChat.setEditable(false);
txtMensaje.setBounds(0, 298, 610, 27);
pnChat.add(txtMensaje);
txtMensaje.setColumns(10);
btnEnviar.setBounds(611, 298, 120, 27);
pnChat.add(btnEnviar);
tabbedPane.addTab("Garage", null, pnGarage, null);
pnGarage.setLayout(null);
if(tblVehiculos.getSelectedRow()==-1){
JOptionPane.
showMessageDialog(null,
"Debe seleccionar una columna"); }
else{
ModeloTabla.getInstancia().borrarVehiculo(tblVehiculos.getSelectedRow());
}
}
});
btnDespachar.
setFont(new Font("Tahoma",
Font.
BOLD,
16)); btnDespachar.setBounds(273, 309, 132, 29);
pnGarage.add(btnDespachar);
scrollPane_1.setBounds(10, 29, 708, 212);
pnGarage.add(scrollPane_1);
tblVehiculos.setModel(ModeloTabla.getInstancia());
scrollPane_1.setViewportView(tblVehiculos);
setVisible(true);
}
txtMensaje.addActionListener(accion);
btnEnviar.addActionListener(accion);
}
public void addTexto
(String texto
){ txtChat.append(texto);
}
String texto
= txtMensaje.
getText(); txtMensaje.setText("");
return texto;
}
}
package com.gestion.garage.practicas;
import com.gestion.garage.InterfazGrafica.Login;
public class SistemaGarage {
public static void main
(String[] args
){ new Login();
}
}
Cualquier duda acerca del codigo me avisan, espero sus respuestas :)