如何将服务器响应从java客户端套接字存储到arraylist或某种结构

64jmpszr  于 2021-08-25  发布在  Java
关注(0)|答案(0)|浏览(219)

我正在处理一个家庭作业问题,要求我将java gui的输出发送到类服务器,并在gui中填充服务器的响应。我能够通过gui将输出从客户端发送到服务器,但是,我只能从控制台而不是gui中打印的服务器获得响应。
我的方法是使用sendclientmessage方法,其中客户端获取gui输出并发送到服务器,使用getservermessage方法,其中服务器对客户端的响应存储在arraylist中并返回arraylist。然而,我不断收到ioexception,说套接字已关闭;当我结合下面两种方法时,我可以向服务器发送消息并从服务器获得响应。

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);
        }
    }

请注意,事物的服务器端是在需要防火墙访问的类web服务器中实现的。因此,我在主方法中调用的类对象和方法中放置了一些占位符参数。我需要帮助将服务器的响应存储在arraylist或其他数据结构中,然后我将能够在gui中填充该响应。我已经包含了客户端类的代码。

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>);
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题