我有两个类服务器和客户端。我在 Intelliji 上同时运行服务器和客户端。我能够将数据写入服务器上的 Json 文件,但是在读取数据时,我无法读取它。当我尝试读取数据时,我的应用程序没有响应。我是套接字编程的新手,请帮助我。这是客户端的代码import java.io.*;import java.net.Socket;public class Client { public String readDataFromServer(Socket socket) throws IOException { InputStreamReader inputStreamReader = new InputStreamReader(socket.getInputStream()); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); return bufferedReader.readLine(); } public void writeDataToServer(String obj) throws IOException { Socket socket = new Socket("localhost", 1299); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(socket.getOutputStream()); PrintWriter printWriter = new PrintWriter(outputStreamWriter); printWriter.write(obj); printWriter.flush(); printWriter.close(); }}这是服务器端的代码import java.io.*;import java.net.ServerSocket;import java.net.Socket;public class Server { static void writeJson (String str) throws IOException { FileWriter pw = null; try { pw = new FileWriter("MYJSON.json", true); } catch (FileNotFoundException e1) { e1.printStackTrace(); } pw.write(str + '\n'); pw.flush(); try { } catch (Exception E) { E.printStackTrace(); } pw.close(); }
添加回答
举报
0/150
提交
取消