using socket.accept() inside of a thread is always freezing

Hi, Im writing an app with sockets and one thread, this thread will manage the process when the users want to connect, so, I put the socket.accept() inside of a thread but the program is freezing in this part and I really dont know why because that code is inside ot the thread; when the program is "freezing" in that part , I cant use even the Close button; so here is my code, thanks for your help:

publicstaticvoid main (String args[])

{

sslchat xserver =new sslchat();

xserver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public sslchat (){

publicvoid iniciar(int puerta){

try{

int jaco=puerta;

while(true){

try{

new ThreadServidor(jaco,this).start();

}// fin de try

catch(IOException e1){

System.exit(0);

/*

try{

//x1cliente.close();

}//fin de try

catch(IOException e2)

{

}//fin de catch

*/

}//fin de catch

}//fin del while(true)

}//fin de try

catch(Exception e){

JOptionPane.showMessageDialog(null,"Error al iniciar Comunicacion");

System.exit(0);

}//fin de catch

}//fin de iniciar

}

class ThreadServidorextends Thread{

private ServerSocket x1servidor=null;

private Socket x1cliente=null;

private Socket s;

private sslchat toby;

private BufferedReader entrada;

private PrintWriter salida;

private String usuario;

privatestatic Vector clientesActivos=new Vector();

public ThreadServidor(int jacomen, sslchat jr)throws IOException{

toby=jr;

x1servidor=new ServerSocket(jacomen);

JOptionPane.showMessageDialog(null,"antes del accept");

x1cliente=x1servidor.accept();

JOptionPane.showMessageDialog(null,"despues del accept");

s=x1cliente;

salida=new PrintWriter(s.getOutputStream(),true);

entrada=new BufferedReader(new InputStreamReader(s.getInputStream()));

usuario=s.getInetAddress().getHostName()+";"+s.getPort();

clientesActivos.addElement(this);

/* quita esta rutina

try{

String historial="C:"+File.separatorChar+"historial.txt";

PrintWriter salidaArchivo=new PrintWriter(new BufferedWriter(new FileWriter(historial,true)));

salidaArchivo.println("Conexion desde la direccion: "+s.getInetAddress().getHostName()+" por el puerto "+s.getPort()+" en la fecha "+new Date());

salidaArchivo.close();

}//fin de try

catch(IOException e2){

//JOptionPane.showMessageDialog(null,"Fallo en el archivo de historial", JOptionPane.WARNING_MESSAGE);

JOptionPane.showMessageDialog(null,"Fallo en el archivo de historial");

}//fin de catch

*/

}//fin de la funcion thread throws ioexception

privatevoid escribir(String textoUsuario){

for(int i=0;i<clientesActivos.size();i++){

toby.txtConversacion.append("Enviando a: "+i+textoUsuario);

((ThreadServidor)(clientesActivos.elementAt(i))).salida.println(textoUsuario);

}//fin del for

}//fin de escribir

publicvoid run(){

String textoUsuario;

try{

while((textoUsuario=entrada.readLine())!=null)

escribir(usuario+"> "+textoUsuario);

}//fin del try

catch (Exception e1){

if(e1.getMessage().equals("Conexion terminada por el extremo"))

{

try{

clientesActivos.removeElement(this);

s.close();

}

catch (Exception e9){

JOptionPane.showMessageDialog(null,"Error inesperado");

}

}//fin if

}//fin de catch

/*

try{

clientesActivos.removeElement(this);

String historial="C:"+File.separatorChar+"historial.txt";

PrintWriter salidaArchivo=new PrintWriter(new BufferedWriter(new FileWriter(historial,true)));

salidaArchivo.println("Desconexion desde la direccion: "+s.getInetAddress().getHostName()+" por el puerto "+s.getPort());

salidaArchivo.close();

}//fin de try

catch(IOException e2){

JOptionPane.showMessageDialog(null,"Fallo en el historial");

}//fin de catch

finally{

clientesActivos.removeElement(this);

}

try{

s.close();

}//fin de try

catch(Exception e3){

//JOptionPane.showMessageDialog(null, "No se ha podido cerrar el socket",JOptionPane.WARNING_MESSAGE);

JOptionPane.showMessageDialog(null,"No se ha podido cerrar el socket");

}//fin de catch

}//fin de if

}//fin de catch

*/

}//fin del run

}//fin de clase ppal clientThread

[8026 byte] By [viejotrena] at [2007-11-27 11:28:23]
# 1

Because of how you've mixed your GUI code and your socket code together and because of all the commented out stuff, I'm not even going to look at it.

If you're having trouble with socket.accept freezing, then the code you post should not have one single line of GUI stuff in it.

jverda at 2007-7-29 16:21:49 > top of Java-index,Java Essentials,Java Programming...
# 2

You need to throw all this code away and start over.

ServerSocket.accept blocks and waits for incoming connections to accept them.

I don't believe you want to use ServerSocket at all.

cotton.ma at 2007-7-29 16:21:49 > top of Java-index,Java Essentials,Java Programming...
# 3

And then you seem to have a while(true) loop that would (if it worked) keep trying to bind ServerSockets to the same port.

No, I am sorry but this is all rubbish.

Throw it all away. Read the networking tutorial (found here http://java.sun.com/docs/books/tutorial/networking/index.html) and the threading tutorial (found here http://java.sun.com/docs/books/tutorial/essential/concurrency/index.html), figure out what you really want to do and then try again.

cotton.ma at 2007-7-29 16:21:49 > top of Java-index,Java Essentials,Java Programming...