请各位高手帮忙看看客户端关了readLine()为什么不报错.

wensshq wensshq 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
import java.io.IOException;
import java.io.*;
import java.net.*;
public class ChatServer {

public static void main(String[] args) {

ServerSocket ss = null;
BufferedReader br = null;
Socket s = null;
boolean started = false;
try {
ss = new ServerSocket(8888);
} catch (BindException e) {
System.out.println("The port is already in use, please replace the port!");
System.out.println("The program has been in operation, please close and re run the program!");
System.exit(0);
} catch (IOException e) {
e.printStackTrace();
}

try {
started = true;
while(started) {
boolean bConnected = false;
s = ss.accept();
bConnected = true;
System.out.println("a Client connected!");
br = new BufferedReader(new InputStreamReader(s.getInputStream()));
while(bConnected) {
String str = br.readLine();
System.out.println(str);

}
System.out.println("a Client connected!");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) br.close();
if(s != null) s.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Client Closed!");
}
}
}

请帮忙看下代码中标红的地方.readLine()与readUTF()这个方法都是阻滞式的,为什么把客户端关了之后,就一直输出null, 而不是进入阻滞,或者是抛出异常.
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
wensshq
Java SE 62,621 成员 307,251 内容