SwingWorker Java kernel is running but not received or message sent

A few days ago I tried to create a client server or client server as an experiment to find out about a socket using a stream, but then someone told me that I should use swingWorker. I did some research on how to use it and implemented it as a practice, but it still doesn't work. the swingWorker thread doesn't look like it works even if I get the connection and used .excute (). If you guys can help determine where I am doing wrong, that would be great. The SwingWorker class is in the startSever () and startClient () methods.

private void startServer() { SwingWorker <Void, String> runningServer = new SwingWorker<Void, String>(){ protected Void doInBackground() { try { listeningSocket = new ServerSocket(port); System.out.println("waiting for connection"); connection = listeningSocket.accept(); connected = true; System.out.println("Connected"); String incomeMessage =null; while(connected){ inStream = connection.getInputStream(); inDataStream = new DataInputStream(inStream); if (myMessage !=null){ outStream = connection.getOutputStream(); outDataStream = new DataOutputStream(outStream); outDataStream.writeUTF(myMessage); } if((incomeMessage = inDataStream.readUTF())!=null){ clientMessage = incomeMessage; publish(clientMessage); incomeMessage =null; } } } catch (IOException e) { clientMessage = "Connection Lost"; } return null; } runningServer.execute(); } 
+2
java concurrency swingworker
Jan 31 '13 at 2:10
source share
3 answers

Here is a VERY basic example.

In principle, since the program requires asynchronous communication (i.e. you should be able to read from and write to the socket at the same time), you need to unload each stream into a separate stream.

The management process in this example, well, does not exist. Actually, you should have some kind of “connection” manager that could cleanly close the output and input streams, so that, for example, when the user entered “bye”, the output stream could tell the connection manager that the connection should be terminated. It will then tell the input stream to stop reading any new message and complete ...

Client

 public class Client { public static void main(String[] args) { try { Socket master = new Socket("localhost", 8900); new Thread(new InputHandler(master)).start(); new Thread(new OuputHandler(master)).start(); } catch (Exception ex) { ex.printStackTrace(); } } public static class InputHandler implements Runnable { private Socket socket; public InputHandler(Socket socket) { this.socket = socket; } @Override public void run() { boolean commune = true; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (commune) { String text = reader.readLine(); System.out.println("\n<server> " + text); if (text.toLowerCase().equals("bye")) { commune = false; } } } catch (Exception exp) { exp.printStackTrace(); } finally { try { reader.close(); } catch (Exception e) { } try { socket.close(); } catch (Exception e) { } } } } public static class OuputHandler implements Runnable { private Socket socket; public OuputHandler(Socket socket) { this.socket = socket; } @Override public void run() { boolean commune = true; BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); Scanner scanner = new Scanner(System.in); while (commune) { System.out.print("> "); String text = scanner.nextLine(); writer.write(text); writer.newLine(); writer.flush(); if (text.equalsIgnoreCase("bye")) { commune = false; } } } catch (Exception exp) { exp.printStackTrace(); } finally { try { writer.close(); } catch (Exception e) { } try { socket.close(); } catch (Exception e) { } } } } } 

Server

 public class Server { public static void main(String[] args) { try { ServerSocket master = new ServerSocket(8900); Socket socket = master.accept(); new Thread(new InputHandler(socket)).start(); new Thread(new OuputHandler(socket)).start(); } catch (Exception ex) { ex.printStackTrace(); } } public static class InputHandler implements Runnable { private Socket socket; public InputHandler(Socket socket) { this.socket = socket; } @Override public void run() { boolean commune = true; BufferedReader reader = null; try { reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); while (commune) { String text = reader.readLine(); System.out.println("\n<client> " + text); if (text.toLowerCase().equals("bye")) { commune = false; } } } catch (Exception exp) { exp.printStackTrace(); } finally { try { reader.close(); } catch (Exception e) { } try { socket.close(); } catch (Exception e) { } } } } public static class OuputHandler implements Runnable { private Socket socket; public OuputHandler(Socket socket) { this.socket = socket; } @Override public void run() { boolean commune = true; BufferedWriter writer = null; try { writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); Scanner scanner = new Scanner(System.in); while (commune) { System.out.print("> "); String text = scanner.next(); writer.write(text); writer.newLine(); writer.flush(); if (text.equalsIgnoreCase("bye")) { commune = false; } } } catch (Exception exp) { exp.printStackTrace(); } finally { try { writer.close(); } catch (Exception e) { } try { socket.close(); } catch (Exception e) { } } } } } 

Update (whine)

As long as I have the source code in front of me ...

Here very, very rarely need to do textMessage.addKeyListener(this)

Since you are using a JTextField , you should use an ActionListener . There are a number of important reasons for this, but for you the main thing is that the “accept” action depends on Look and Feel. While most systems use Enter as the take action, this is not a guarantee.

See How to write an action listener for more information.

Given the overall complexity of what you are trying to do, +1 for an overall good try!

+6
Jan 31 '13 at 2:47
source share

Using this, the following changes work with one telnet client.

 private PrintWriter out; ... public void keyPressed(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) { myMessage = friendLabel + textMessage.getText(); if (out != null) { out.println(myMessage); } ... } ... protected Void doInBackground() { try { listeningSocket = new ServerSocket(port); System.out.println("Waiting for connection"); connection = listeningSocket.accept(); connected = true; System.out.println("Connected"); Scanner in = new Scanner(connection.getInputStream()); out = new PrintWriter(connection.getOutputStream(), true); publish("Connected"); while (true) { publish(in.nextLine()); } } catch (IOException e) { clientMessage = "Connection Lost"; try { connection.close(); System.out.println("Closed"); } catch (IOException e1) { e1.printStackTrace(); connected = false; } } return null; } 
+5
Jan 31 '13 at 2:49 on
source share

I see that your server port is 8900 and your port is 8900. I'm not sure if it matters if the server and the client are running on the same machine ...

-one
Jan 31 '13 at 2:36
source share



All Articles