-1

I have been working on a basic proof of concept project for a long time and have failed over and over; it is becoming very demoralizing. I have gone over many examples online and created probably a dozen projects trying to figure this out using different implementations.

The goal: create a simple message server and message client (just a single direct connection between two hosts) and be able to send text back and forth.

I have two projects, one for the server and one for the client. I have created these as the simplest example I can think of so that my errors might be easier to spot.

Below are examples of testing steps, the expected results, the actual results, and the code for each application (the server and the client).

The server application

The message server test steps:

  • Run the application
  • Press Start button
  • Type text in the tfSend JTextField and press the Send button

The expected result:

  • TextArea indicates the server is waiting for connections
  • The ServerSocket listens for connections
  • ServerSocket accepts a connection and creates a client Socket
  • The client Socket input stream is assigned to a BufferedReader
  • The client Socket output stream is assigned to a PrintWriter
  • The server should send a connect message to the client using the PrintWriter “You are connected.” The message should display in the taDisplay JTextArea
  • The server should receive the connect message “A client connected.” From the client and display in the taDisplay JTextArea
  • The Send button should send the contents of the tfSend JTextField to the client

Actual Results:

  • 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 application

The message client test steps (the server must be running):

  • Press the Connect button
  • Type text in the tfSend JTextField and press the Send button.

The expected result:

  • Client Socket connects to the server
  • Server accepts the connection
  • The client Socket input stream is assigned to a BufferedReader
  • The client Socket output stream is assigned to a PrintWriter
  • Client sends connection message to server “A client connected.” The message should display in the taDisplay JTextArea
  • Client should receive the connection message “You are connected.” From the server and display in the taDisplay JTextArea
  • The send button should send the contents of the tfSend JTextField to the server

Actual Results:

  • 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 } } 

I have tried different implementations of threads, I have tried not using threads, and even using the NetBeans debugger I have not been able to discern where the problem is. According to all of my research and the countless examples I’ve seen it should work, but it does not. If anyone can show me what I am doing wrong I would be over the moon with joy. I have been trying to figure this out for months on and off.

2
  • If this is an exact duplicate please provide a link to the question it duplicates. edit I see the link, I will now review[/edit] Commented Feb 23, 2018 at 19:59
  • The issue was two parts: I needed the flush() method on the output stream as Khal_Drogo stated and as the duplicate question link explained I also needed to add an end of line character "\n" for the readLine() method to work. Thank you for the help! Commented Feb 23, 2018 at 20:07

1 Answer 1

-1

Have you tried flushing your buffer after you send the message? Try using the .Flush() method after you send your message.

Sign up to request clarification or add additional context in comments.

3 Comments

We have progress! Thank you, the flush() method got the connection messages sent, but the Send button still does not function correctly. I also added the flush() method to the Send button event handler, but it did not fix it. I will update the code with the flush() method. If you have any ideas on the Send button issue I would appreciate that as well. Thank you!
The Send button issue is because the readLine() method must have a line ending character \n in order to be read. Thank you for your help! It won't let me upvote because I am new and below 15 rep, but I did click the upvote button.
I'm glad that I was able to help!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.