When the client connects a Socket appears to be created, but the connect message sent by the server is never received by the client.
Upon pressing the Send button the contents of the tfSend JTextArea are never received by the client
package msgserver; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.*; import java.net.*; import java.io.*; public class MsgServer extends JFrame { // Swing objects private JPanel pnlMain = new JPanel(); private JPanel pnlSettings = new JPanel(); private JPanel pnlSend = new JPanel(); private JLabel lblPort = new JLabel("Port"); private JButton btnStart = new JButton("Start"); private JButton btnSend = new JButton("Send"); private JTextField tfPort = new JTextField("9001"); private JTextField tfSend = new JTextField(); private JTextArea taDisplay = new JTextArea(); // net objects private Socket clientSock; private int port; // Streams private BufferedReader inStream; private PrintWriter outStream; public MsgServer() { super("Message Server"); // configure GUI objects btnStart.setPreferredSize(new Dimension(100, 20)); taDisplay.setEditable(false); // build the GUI pnlSettings.setLayout(new FlowLayout()); pnlSettings.add(lblPort); pnlSettings.add(tfPort); pnlSettings.add(btnStart); pnlSend.setLayout(new BorderLayout()); pnlSend.add(tfSend, BorderLayout.CENTER); pnlSend.add(btnSend, BorderLayout.EAST); pnlMain.setLayout(new BorderLayout()); pnlMain.add(pnlSettings, BorderLayout.NORTH); pnlMain.add(taDisplay, BorderLayout.CENTER); pnlMain.add(pnlSend, BorderLayout.SOUTH); this.setLayout(new BorderLayout()); this.add(pnlMain, BorderLayout.CENTER); // JFrame configuration this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setPreferredSize(new Dimension(640, 480)); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); // Define event handlers // starting the server btnStart.addActionListener(e -> { // assign instance variable values port = Integer.parseInt(tfPort.getText()); taDisplay.append("Server listening for clients...\n"); // create thread to listen for input Thread t = new Thread(new Runnable() { @Override public void run() { // accept client connection try { clientSock = new ServerSocket(port).accept(); } catch (IOException ioe) { ioe.printStackTrace(); } // status update taDisplay.append("Client connected.\n"); taDisplay.append("Creating streams...\n"); // create the IO streams try { inStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); outStream = new PrintWriter(clientSock.getOutputStream()); } catch (IOException ioe) { ioe.printStackTrace(); } // status update taDisplay.append("Streams created.\n"); taDisplay.append("Attempting to send connect message.\n"); // Attempt to send connected message to client outStream.print("You are connected.\n"); outStream.flush(); // create loop to receive input from client try { String line; while ((line = inStream.readLine()) != null) { // if the user quits break the loop if (line.equals("/quit")) { break; } // otherwise append the line to textarea taDisplay.append(line + "\n"); } } catch (IOException ioe) { ioe.printStackTrace(); } } }); // start input thread t.start(); }); // send message to client btnSend.addActionListener(e -> { outStream.print(tfSend.getText()); outStream.flush(); tfSend.setText(""); }); } public static void main(String[] args) { new MsgServer(); // show server window } }
The client Socket appears to successfully connect to the server, but no connection messages are exchanged.
Upon pressing the Send button the contents of the tfSend JTextArea are never received by the server
package msgclient; import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.*; import java.net.*; import java.io.*; public class MsgClient extends JFrame { // Swing objects private JPanel pnlMain = new JPanel(); private JPanel pnlSettings = new JPanel(); private JPanel pnlSend = new JPanel(); private JLabel lblHost = new JLabel("Host"); private JLabel lblPort = new JLabel("Port"); private JButton btnConnect = new JButton("Connect"); private JButton btnSend = new JButton("Send"); private JTextField tfPort = new JTextField("9001"); private JTextField tfHost = new JTextField("127.0.0.1"); private JTextField tfSend = new JTextField(); private JTextArea taDisplay = new JTextArea(); // net objects private Socket clientSock; private int port; private String host; // Streams private BufferedReader inStream; private PrintWriter outStream; public MsgClient() { super("Message Client"); // configure GUI objects btnConnect.setPreferredSize(new Dimension(100, 20)); taDisplay.setEditable(false); // build the GUI pnlSettings.setLayout(new FlowLayout()); pnlSettings.add(lblHost); pnlSettings.add(tfHost); pnlSettings.add(lblPort); pnlSettings.add(tfPort); pnlSettings.add(btnConnect); pnlSend.setLayout(new BorderLayout()); pnlSend.add(tfSend, BorderLayout.CENTER); pnlSend.add(btnSend, BorderLayout.EAST); pnlMain.setLayout(new BorderLayout()); pnlMain.add(pnlSettings, BorderLayout.NORTH); pnlMain.add(taDisplay, BorderLayout.CENTER); pnlMain.add(pnlSend, BorderLayout.SOUTH); this.setLayout(new BorderLayout()); this.add(pnlMain, BorderLayout.CENTER); // JFrame configuration this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setPreferredSize(new Dimension(640, 480)); this.setLocationRelativeTo(null); this.pack(); this.setVisible(true); // connect to server btnConnect.addActionListener(e -> { // define event handlers // assign instance variable values port = Integer.parseInt(tfPort.getText()); try { // connect to server clientSock = new Socket(host, port); // build IO streams inStream = new BufferedReader(new InputStreamReader(clientSock.getInputStream())); outStream = new PrintWriter(clientSock.getOutputStream()); // Attempt to send connected message to client outStream.print("A client connected.\n"); outStream.flush(); // create loop to receive input from client Thread t = new Thread(new Runnable() { @Override public void run() { try { String line; while ((line = inStream.readLine()) != null) { // if the user quits break the loop if (line.equals("/quit")) { break; } // otherwise append the line to the textarea taDisplay.append(line + "\n"); } } catch (IOException ioe) { ioe.printStackTrace(); } } }); // start input thread t.start(); } catch (IOException ioe) { ioe.printStackTrace(); } }); // send message to server btnSend.addActionListener(e -> { outStream.print(tfSend.getText()); outStream.flush(); tfSend.setText(""); }); } public static void main(String[] args) { new MsgClient(); // show client window } }