以下代码存在bug,请找出: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 (IOException e) { e.printStackTrace(); // TODO: handle exception } } public void run() { while(true) { try { String S1 = "几点了?"; String S2 = "今天是几号?"; String S3 = "今天是星期几?"; String S4 = "再见"; 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 if(st.equals(S4)) { System.out.println("结束服务"); break; } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
时间: 2024-01-15 16:02:44 浏览: 66
skypinacolada.github.io
在while(true)循环中,每次循环都会创建一个新的ServerSocket对象,这会导致已经被占用的端口无法再次使用,并抛出BindException异常。正确的做法是将ServerSocket的创建放在while循环外面,只创建一次即可。修改后的代码如下:
```
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;
try {
server = new ServerSocket(2010);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
while (true) {
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 (IOException e) {
e.printStackTrace();
// TODO: handle exception
}
}
public void run() {
while (true) {
try {
String S1 = "几点了?";
String S2 = "今天是几号?";
String S3 = "今天是星期几?";
String S4 = "再见";
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 if (st.equals(S4)) {
System.out.println("结束服务");
break;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
```
阅读全文