优化下面代码package Y; import java.io.*; import java.net.*; import java.time.*; import java.util.*; public class Shiyan5Server { public static void main(String[] args) { ServerSocket server = null; Sy5ServerThread thread; Socket you = null; while(true) { try { server = new ServerSocket(2010); } catch (IOException e) { System.out.println("正在监听"); // TODO: handle exception } try { System.out.println("等待客户呼叫"); you = server.accept(); System.out.println("客户的地址"+you.getInetAddress()); } catch (Exception e) { // TODO: handle exception System.out.println("正在等待客户"); } if (you != null) { new Sy5ServerThread(you).start(); } } } } class Sy5ServerThread extends Thread{ Socket socket; DataOutputStream out = null; DataInputStream in = null; String s = null; Sy5ServerThread(Socket t) { socket = t; try { out = new DataOutputStream(socket.getOutputStream()); in = new DataInputStream(socket.getInputStream()); } catch (Exception e) { // TODO: handle exception } } public void run() { while(true) { try { String S1 = "几点了?"; String S2 = "今天是几号?"; String S3 = "今天是星期几?"; String st = in.readUTF(); if (st.equals(S1)) { LocalTime time = LocalTime.now(); String Time = String.format("%tR", time); out.writeUTF(Time); } else if(st.equals(S2)) { LocalDate date = LocalDate.now(); String Time = String.format("%tm月%td日", date,date); out.writeUTF(Time); }else if (st.equals(S3)) { LocalDate week = LocalDate.now(); String Time = String.format("%tA", week); out.writeUTF(Time); }else { System.out.println("服务结束"); break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }} } }
时间: 2024-01-08 09:03:18 浏览: 79
java代码-import java.util.Scanner;
5星 · 资源好评率100%
以下是代码的优化:
1. 将ServerSocket的创建放在while循环的外部,否则每次循环都会重新创建一个ServerSocket对象,浪费资源。
2. 在catch语句中添加具体的异常处理代码,避免出现异常时程序无法正常执行。
3. 在Sy5ServerThread类的构造函数中,将out和in的初始化放在try语句块中,避免出现异常时无法正确初始化。
4. 将字符串常量定义为静态常量,避免在运行时重复创建对象,浪费资源。
5. 在while循环中添加try-catch语句,避免出现异常时程序中断。
改进后的代码如下:
```
package Y;
import java.io.*;
import java.net.*;
import java.time.*;
import java.util.*;
public class Shiyan5Server {
private static final String S1 = "几点了?";
private static final String S2 = "今天是几号?";
private static final String S3 = "今天是星期几?";
public static void main(String[] args) {
ServerSocket server = null;
Socket you = null;
try {
server = new ServerSocket(2010);
System.out.println("正在监听");
} catch (IOException e) {
e.printStackTrace();
}
while (true) {
try {
System.out.println("等待客户呼叫");
you = server.accept();
System.out.println("客户的地址" + you.getInetAddress());
new Sy5ServerThread(you).start();
} catch (Exception e) {
System.out.println("正在等待客户");
e.printStackTrace();
}
}
}
}
class Sy5ServerThread extends Thread {
private static final String S1 = "几点了?";
private static final String S2 = "今天是几号?";
private static final String S3 = "今天是星期几?";
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
Sy5ServerThread(Socket t) {
socket = t;
try {
out = new DataOutputStream(socket.getOutputStream());
in = new DataInputStream(socket.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
}
public void run() {
while (true) {
try {
String st = in.readUTF();
if (st.equals(S1)) {
LocalTime time = LocalTime.now();
String Time = String.format("%tR", time);
out.writeUTF(Time);
} else if (st.equals(S2)) {
LocalDate date = LocalDate.now();
String Time = String.format("%tm月%td日", date, date);
out.writeUTF(Time);
} else if (st.equals(S3)) {
LocalDate week = LocalDate.now();
String Time = String.format("%tA", week);
out.writeUTF(Time);
} else {
System.out.println("服务结束");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
```
阅读全文