Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-24-2007, 01:48 AM
Member
 
Join Date: Jul 2007
Posts: 4
tamayo is on a distinguished road
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:

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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
If JNI thread call the java object in another thread, it will crash. skaterxu Advanced Java 0 01-28-2008 09:02 AM
Creating a Thread (extending Java Thread Class) JavaForums Java Blogs 0 12-19-2007 11:31 AM
how to enable jcreator to accept command line arguments? Amateur1980 JCreator 1 11-07-2007 04:17 PM
variable to accept a single object Rgfirefly24 New To Java 1 08-06-2007 06:41 AM
Accept 2 values .... danny000 New To Java 1 07-31-2007 04:12 PM


All times are GMT +3. The time now is 04:55 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org