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

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

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

  1. try {
  2. pw = new PrintWriter(clientSocket.getOutputStream(), true);
  3. br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  4. pw.println(input);
  5. while((output = br.readLine()) != null) {
  6. System.out.println(output);
  7. }
  8. }

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

  1. import java.io.*;
  2. import java.net.*;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. public class ClientGUI {
  6. protected final String host;
  7. protected final int port;
  8. private PrintWriter pw;
  9. private BufferedReader br;
  10. public ClientGUI(String host, int port) {
  11. this.host = host;
  12. this.port = port;
  13. }
  14. public String getHost() {
  15. return host;
  16. }
  17. public int getPort() {
  18. return port;
  19. }
  20. public void setConnection(String input) {
  21. try {
  22. Socket client = new Socket(getHost(), getPort());
  23. sendClientMessage(client, input);
  24. getServerMessage(client);
  25. closeConnection(client);
  26. } catch(UnknownHostException uhe) {
  27. System.err.println("Unknown Host Encountered: " + getHost());
  28. uhe.printStackTrace();
  29. } catch(IOException ioe) {
  30. System.err.println("IOException On: " + getHost());
  31. ioe.printStackTrace();
  32. }
  33. }
  34. public List<String> getServerMessage(Socket clientSocket) throws IOException {
  35. List<String> list = new ArrayList<>();
  36. BufferedReader br = null;
  37. try {
  38. br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
  39. String output;
  40. while((output = br.readLine()) != null) {
  41. list.add(output);
  42. }
  43. } catch(IOException ioe) {
  44. System.err.println("IOException: " + ioe.getMessage());
  45. } finally {
  46. if(br != null) {
  47. br.close();
  48. }
  49. }
  50. return list;
  51. }
  52. public void sendClientMessage(Socket clientSocket, String input) throws IOException {
  53. PrintWriter pw = null;
  54. try {
  55. pw = new PrintWriter(clientSocket.getOutputStream(), true);
  56. pw.println(input);
  57. } catch(IOException ioe) {
  58. System.err.println("IOException Encountered: " + ioe.getMessage());
  59. } finally {
  60. if(pw != null) {
  61. pw.flush();
  62. pw.close();
  63. }
  64. }
  65. }
  66. public void closeConnection(Socket clientSocket) throws IOException {
  67. if(clientSocket != null) {
  68. clientSocket.close();
  69. }
  70. }
  71. public static void main(String[] args) {
  72. ClientGUI gui = new ClientGUI(<The host here>, <The port here>);
  73. gui.setConnection(<Some GUI output here>);
  74. }
  75. }

暂无答案!

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

相关问题