0

I am working on a homework problem where I am asked to send the output from a Java GUI to the class server and populate the response from the server in the GUI. I am able to send the output from the client to the server through the GUI, yet, I can only get the response from the server printed in the console instead of the GUI.

My approach was to have a sendClientMessage method where the client grabs the GUI output and sends to the server and a getServerMessage method where the server's response to the client is stored in an ArrayList and returns the ArrayList. However, I keep getting the IOException saying socket is closed; I have no problem sending to the server and getting response from it when I combine the 2 methods like below.

try {
        pw = new PrintWriter(clientSocket.getOutputStream(), true);
        br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
        pw.println(input);
        
        while((output = br.readLine()) != null) {
            System.out.println(output);
        }
    } 

Please note that the server side of things are implemented in a class web server which requires firewall access. Therefore, I put some place holder parameters in the class object and method invoked in the main method. I need help with storing the server's response in an ArrayList or some other data structure, then I will be able to populate that in the GUI. I have included the code for the Client class.

import java.io.*;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class ClientGUI {    
    protected final String host;
    protected final int port;
    private PrintWriter pw;
    private BufferedReader br;
    
    public ClientGUI(String host, int port) {
        this.host = host;
        this.port = port;
    }
    
    public String getHost() {
        return host;
    }
    
    public int getPort() {
        return port;
    }
    
    public void setConnection(String input) {
        try {
            Socket client = new Socket(getHost(), getPort());
            sendClientMessage(client, input);
            getServerMessage(client);
            closeConnection(client);
        } catch(UnknownHostException uhe) {
            System.err.println("Unknown Host Encountered: " + getHost());
            uhe.printStackTrace();
        } catch(IOException ioe) {
            System.err.println("IOException On: " + getHost());
            ioe.printStackTrace();
        }
    }
    
    public List<String> getServerMessage(Socket clientSocket) throws IOException {
        List<String> list = new ArrayList<>();
        BufferedReader br = null;
        
        try {
            br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            String output;
            
            while((output = br.readLine()) != null) {
                list.add(output);
            }
        } catch(IOException ioe) {
            System.err.println("IOException: " + ioe.getMessage());
        } finally {
            if(br != null) {
                br.close();
            }
        }
        return list;
    }
    
    public void sendClientMessage(Socket clientSocket, String input) throws IOException {
        PrintWriter pw = null;
        
        try {
            pw = new PrintWriter(clientSocket.getOutputStream(), true);
            pw.println(input);
            
        } catch(IOException ioe) {
            System.err.println("IOException Encountered: " + ioe.getMessage());
        } finally {
            if(pw != null) {
                pw.flush();
                pw.close();
            }
        }
    }
    
    public void closeConnection(Socket clientSocket) throws IOException {
        if(clientSocket != null) {
            clientSocket.close();
        }
    }
    
    public static void main(String[] args) {
        ClientGUI gui = new ClientGUI(<The host here>, <The port here>);
        gui.setConnection(<Some GUI output here>);
    }
}
  • [Does closing the BufferedReader/PrintWriter close the socket connection?](https://stackoverflow.com/q/484925) – 001 Jul 06 '21 at 18:52
  • Thank you! I can separate the actions to write to and read from the server into 2 methods after closing the socket, PrinterWriter and BufferedReader at the connection. – anAlgorithmADay Jul 07 '21 at 06:53

0 Answers0