Mirad estoy constryendo una aplicacion con sockets, que gestiona varios cliente mediante el uso de threads, la cosa esta en que cada vez que se conecta un cliente al servidor, este le abre un thread, y lo deja en ejecucion hasta que el cliente se cierra y le envia un binario, cuando le envia el binario se cumple el bucle y el thread muere, pero ocurre una cosa extraña, mirad el codigo:
ClienteGui.java
Código
package sockets;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author debci
*/
public class ClienteGui extends javax.swing.JFrame {
public static Socket socket;
public static InputStream is;
public static OutputStream os;
public static DataInputStream dis;
public static DataOutputStream dos;
/** Creates new form ClienteGui */
public ClienteGui() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1 = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
textoEnv = new javax.swing.JTextArea();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosed(java.awt.event.WindowEvent evt) {
formWindowClosed(evt);
}
public void windowClosing(java.awt.event.WindowEvent evt) {
formWindowClosing(evt);
}
});
jButton1.setText("Enviar");
jButton1.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
jButton1ActionPerformed(evt);
}
});
textoEnv.setColumns(20);
textoEnv.setRows(5);
jScrollPane1.setViewportView(textoEnv);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(337, Short.MAX_VALUE)
.addComponent(jButton1)
.addContainerGap())
.addGroup(layout.createSequentialGroup()
.addGap(21, 21, 21)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(117, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap(89, Short.MAX_VALUE)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(83, 83, 83)
.addComponent(jButton1)
.addContainerGap())
);
pack();
}// </editor-fold>
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
try {
String cadena = textoEnv.getText();
dos.writeBoolean(true);
dos.writeUTF(cadena);
} catch (IOException ex) {
Logger.getLogger(ClienteGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void formWindowClosed(java.awt.event.WindowEvent evt) {
}
private void formWindowClosing(java.awt.event.WindowEvent evt) {
try {
dos.writeBoolean(false);
} catch (IOException ex) {
Logger.getLogger(ClienteGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
try {
new ClienteGui().setVisible(true);
socket = new Socket("localhost", 5557);
is = socket.getInputStream();
os = socket.getOutputStream();
dis = new DataInputStream(is);
dos = new DataOutputStream(os);
} catch (IOException ex) {
Logger.getLogger(ClienteGui.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTextArea textoEnv;
// End of variables declaration
}
Servidor.java
Código
package sockets;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import threads.ThreadServidor;
/**
*
* @author debci
*/
public class Servidor {
public static void main(String[] args) throws IOException {
Servidor servidor = new Servidor();
servidor.ServidorChat();
}
public void ServidorChat()
{
try
{
ServerSocket socketServidor = new ServerSocket(5557);
while (true)
{
System.out.println("Esperando conexiones...");
Socket cliente = socketServidor.accept();
Runnable nuevoCliente = new ThreadServidor(cliente);
Thread hilo = new Thread(nuevoCliente);
hilo.start();
}
} catch (Exception e)
{
e.printStackTrace();
}
}
}
ThreadServidor.java
Código
package threads;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author debci
*/
public class ThreadServidor extends Thread {
//Socket al que esta conectado el cliente
private Socket socket1;
/** Para lectura de datos en el socket */
private DataInputStream dataInput;
/** Para escritura de datos en el socket */
private DataOutputStream dataOutput;
private boolean acabado = true;
public ThreadServidor(Socket socket) {
try {
socket1 = socket;
dataInput = new DataInputStream(socket.getInputStream());
dataOutput = new DataOutputStream(socket.getOutputStream());
} catch (IOException ex) {
System.err.println("Error de procesamiento de datos: \n" + ex);
}
}
public void run() {
try {
while(acabado){
acabado = dataInput.readBoolean();
String mensajeRecivido = dataInput.readUTF();
System.out.println(mensajeRecivido + "\n");
}
} catch (IOException ex) {
Logger.getLogger(ThreadServidor.class.getName()).log(Level.SEVERE, null, ex);
}
Me explico, la cosa esta en que cuando el cliente, se cierra (formClosing) se envia el booleano al thread que lo esta atendiendo.
Si el booleano se cambia, se rompe el bucle while y se muere el thread.
El problema, y no se de donde viene, es que cuando cierro un cliente y le pido desconexion y que mate al thread, el servidor libera un error:
Código:
26-dic-2009 15:30:55 threads.ThreadServidor run
GRAVE: null
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at threads.ThreadServidor.run(ThreadServidor.java:50)
at java.lang.Thread.run(Thread.java:619)
Me da ese error y luego continua atendiendo clientes, cada vez que cierro un cliente me lo da, y no se porque es, y como comprendereis no quiero darle uso a una aplicacon que tiene un defecto claro.GRAVE: null
java.io.EOFException
at java.io.DataInputStream.readUnsignedShort(DataInputStream.java:323)
at java.io.DataInputStream.readUTF(DataInputStream.java:572)
at java.io.DataInputStream.readUTF(DataInputStream.java:547)
at threads.ThreadServidor.run(ThreadServidor.java:50)
at java.lang.Thread.run(Thread.java:619)
Saludos y gracias a todos.










Autor





En línea



si es verdad es un error tonto. te da ese error por que no estas haciendo la comprobacion del cierre de conexion en el hilo usuario que estas creando en tu caso es ThreadServidor.. 

tambien sirve para mensajes privados solo hay que usar un poco mas la logica
espero te de la idea .asi lo ise en mi Jchat 
