Note that there are some explanatory texts on larger screens.

plurals
  1. POClient/server Swing program getting stuck when using Threads
    text
    copied!<p>I have a problem with my Java program. I have this codes:</p> <p><strong>Host.java</strong>:</p> <pre><code>public class Host { protected static void start(JFrame window) { ServerSocket server = null; try { server = new ServerSocket(); SocketAddress addr = new InetSocketAddress(hostname, port); server.bind(addr); Socket socket = server.accept(); window.setVisible(false); Thread thread = new Thread(new Incomming(socket.getInputStream())); thread.start(); thread.join(); socket.close(); } catch (UnknownHostException e) { [...] } } </code></pre> <p><strong>Incomming.java</strong>:</p> <pre><code>public class Incomming implements Runnable { private DataInputStream is; public Incomming(InputStream is) { MyFrame frame = new MyFrame(); frame.setVisible(true); frame.pack(); this.is = new DataInputStream(is); } public void run() { try { while(!Thread.currentThread().isInterrupted()) { int n = is.readInt(); if(n == -1) { break; } byte[] b = new byte[n]; is.readFully(b); [...] // working with bytes } System.out.println("Stream closed."); } catch(IOException e) { [...] } } } </code></pre> <p><strong>Client.java</strong> is very similar to Host.java, it uses Incomming.java for socket.getInputStream() too.</p> <p>So the problem is: the client connects to the host, but when it should show on server side and also on client side the MyFrame window, it doesn't load it fully. And the close button of old JFrame windows (on both sides) doesn't do anything. </p> <p>I tried to remove the line with <code>thread.join()</code>, and then the MyFrame window loads completely and close buttons work, but it throws me exception with <code>socket closed</code> message, so the client is no longer connected to the host.</p> <p>How could I fix this problem? Thanks for replies.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload