Results 1 to 1 of 1
- 07-23-2007, 11:48 PM #1
Member
- Join Date
- Jul 2007
- Posts
- 4
- Rep Power
- 0
freezing when use socket.accept() inside of a thread
Hi, I´m 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 don´t know why because that code is inside ot the thread; when the program is "freezing" in that part , I can´t use even the Close button; so here is my code, thanks for your help:
Java Code:public static void main (String args[]) { sslchat xserver = new sslchat(); xserver.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } public sslchat () { public void 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 ThreadServidor extends Thread{ private ServerSocket x1servidor=null; private Socket x1cliente=null; private Socket s; private sslchat toby; private BufferedReader entrada; private PrintWriter salida; private String usuario; private static 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 private void 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 public void 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
Similar Threads
-
If JNI thread call the java object in another thread, it will crash.
By skaterxu in forum Advanced JavaReplies: 0Last Post: 01-28-2008, 07:02 AM -
how to enable jcreator to accept command line arguments?
By Amateur1980 in forum JCreatorReplies: 1Last Post: 11-07-2007, 02:17 PM -
variable to accept a single object
By Rgfirefly24 in forum New To JavaReplies: 1Last Post: 08-06-2007, 04:41 AM -
Accept 2 values ....
By danny000 in forum New To JavaReplies: 1Last Post: 07-31-2007, 02:12 PM


LinkBack URL
About LinkBacks

Bookmarks