Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<ol> <li>Thread#join will block until the thread dies. This is preventing you from closing your window, as you are blocking the Event Dispatching Thread. See <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow">Concurrency in Swing</a> for more details.</li> <li>You <code>accept</code> an incoming socket, start a new <code>Thread</code> to process that socket and then promptly close the socket (most likely before the thread has had a chance to even start reading from it). Instead, close the socket inside the thread, once it has competed processing the stream</li> </ol> <p><strong>Updated</strong></p> <p>Swing is a single threaded framework. This means that all interactions with the UI (creation, modification) MUST be executed within the context of the Event Dispatching Thread. Any operation that blocks this thread will stop the EDT from processing events, including repaint, mouse and keyboard events.</p> <p>Instead of passing the socket's input put stream to the thread, you should pass the socket. This passes responsibility for the management of the socket to that thread, freeing up you current thread.</p> <p>Then in your <code>Incomming</code> class, you should grab a reference to the input stream of the socket, perform what ever actions you need to and the, finally, close both the input put stream and socket when you are done.</p> <pre><code>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); // Pass the socket to the thread to allow it to perform the work Thread thread = new Thread(new Incomming(socket)); thread.start(); } catch (IOException ex) { //...// } } public class Incomming implements Runnable { private final Socket socket; public Incomming(Socket socket) { //?? What's this for, this is VERY wrong // UI Interaction should ONLY occur within the context of the EDT MyFrame frame = new MyFrame(); frame.setVisible(true); frame.pack(); this.socket = socket; } public void run() { if (socket != null) { DataInputStream is = null; try { is = new DataInputStream(socket.getInputStream()); while (!Thread.currentThread().isInterrupted()) { int n = is.readInt(); if (n == -1) { break; } byte[] b = new byte[n]; is.readFully(b); //...// } System.out.println("Stream closed."); } catch (IOException e) { } finally { // Finally clean up... try { is.close(); } catch (Exception e) { } try { socket.close(); } catch (Exception e) { } } } } } </code></pre> <p>You <strong>MUST</strong> read <a href="http://docs.oracle.com/javase/tutorial/uiswing/concurrency/" rel="nofollow"><strong>Concurrency in Swing</strong></a></p> <p>If you intend to update the UI while processing the socket, you will most likely want to use a <code>SwingWorker</code> instead of a <code>Thread</code>. This provides additional functionality to make it easier to sync updates back to the Event Dispatching Thread</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