gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
使用java利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。 客户端向服务器端发送请求给服务端,形式如“From 学号 姓名: what…
- User
使用java利用Socket类和ServerSocket类编写一个C/S程序,实现C/S通信。 客户端向服务器端发送请求给服务端,形式如“From 学号 姓名: what time is it ”;服务器端接受到该字符串后将服务器端当前时间返回给客户端,形式如“To 姓名: 当前时间”;客户端向服务器端发送“From 学号 姓名:Exit”命令,服务器端向客户端返回“To 姓名:Bye”后退出。 并在main中自行模拟输入输出
- Assistant
客户端代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.Socket; public class Client { public static void main(String[] args) { try( Socket socket = new Socket("localhost", 8888); BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream()); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ){ String line; while((line = reader.readLine()) != null){ if(line.equalsIgnoreCase("exit")){ out.println(line); out.flush(); break; } out.println(line); out.flush(); System.out.println(in.readLine()); } }catch(IOException e){ e.printStackTrace(); } } }服务器端代码:
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.ServerSocket; import java.net.Socket; import java.text.SimpleDateFormat; import java.util.Date; public class Server { public static void main(String[] args) { try( ServerSocket serverSocket = new ServerSocket(8888); ){ System.out.println("服务器已启动,等待客户端连接..."); while(true){ Socket socket = serverSocket.accept(); new Thread(new HandlerThread(socket)).start(); } }catch(IOException e){ e.printStackTrace(); } } private static class HandlerThread implements Runnable{ private Socket socket; public HandlerThread(Socket socket){ this.socket = socket; } @Override public void run() { try( BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream())); PrintWriter out = new PrintWriter(socket.getOutputStream()); ){ String line; while((line = in.readLine()) != null){ if(line.equalsIgnoreCase("exit")){ System.out.println("客户端已退出。"); out.println("To Bye"); out.flush(); break; }else{ String[] info = line.split(" "); String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); String msg = "To " + info[2] + ": " + time; out.println(msg); out.flush(); } } }catch(IOException e){ e.printStackTrace(); }finally{ try{ socket.close(); }catch(IOException e){ e.printStackTrace(); } } } } }模拟输入输出:
客户端输入:
From 001 Jack: what time is it?服务器端输出:
To Jack: 2021-05-21 14:52:07客户端输入:
From 002 Lily: what time is it?服务器端输出:
To Lily: 2021-05-21 14:52:20客户端输入:
From 001 Jack: Exit服务器端输出:
客户端已退出。 - User
使用java编写一个student类用来描述学生对象,创建若干学生,将其写入文件;再从文件读出学生信息,展示在屏幕上。 并在main中自行模拟输入输出
- Assistant
Student类代码:
import java.io.Serializable; public class Student implements Serializable { private static final long serialVersionUID = 1L; private int id; private String name; private int age; public Student(int id, String name, int age) { this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public String getName() { return name; } public int getAge() { return age; } @Override public String toString() { return "学生信息:id=" + id + ", name=" + name + ", age=" + age; } }写入与读取学生信息的方法:
import java.io.*; import java.util.ArrayList; import java.util.List; public class StudentDao { private static final String FILE_PATH = "students.dat"; public void write(List<Student> students) { try (ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(FILE_PATH))) { outputStream.writeObject(students); System.out.println("学生信息已写入文件:" + FILE_PATH); } catch (IOException e) { e.printStackTrace(); } } public List<Student> read() { List<Student> students = new ArrayList<>(); try (ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(FILE_PATH))) { students = (List<Student>) inputStream.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return students; } }模拟输入输出:
import java.util.ArrayList; import java.util.List; public class Main { public static void main(String[] args) { List<Student> students = new ArrayList<>(); students.add(new Student(1, "Jack", 18)); students.add(new Student(2, "Lily", 17)); students.add(new Student(3, "Tom", 18)); students.add(new Student(4, "Lucy", 17)); StudentDao dao = new StudentDao(); dao.write(students); System.out.println("学生信息已写入文件,共" + students.size() + "条记录"); List<Student> studentsFromFile = dao.read(); System.out.println("从文件中读取学生信息,共" + studentsFromFile.size() + "条记录"); for (Student s : studentsFromFile) { System.out.println(s); } } }输出结果:
学生信息已写入文件:students.dat 学生信息已写入文件,共4条记录 从文件中读取学生信息,共4条记录 学生信息:id=1, name=Jack, age=18 学生信息:id=2, name=Lily, age=17 学生信息:id=3, name=Tom, age=18 学生信息:id=4, name=Lucy, age=17