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!
MadProgrammer Jan 31 '13 at 2:47 2013-01-31 02:47
source share