双11狂欢大促

盲盒赢 定制羽绒服

开启惊喜盲盒,抽取你的专属好礼

— 惊喜奖品抢先看 —

请教一个java socket的问题

testpwd testpwd 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
有这样一个例子:


public class EchoServer
{
public static void main(String[] args)
{
try
{
ServerSocket s = new ServerSocket(8189);
Socket incoming = s.accept();
try
{
InputStream inStream = incoming.getInputStream();
OutputStream outStream = incoming.getOutputStream();

Scanner in = new Scanner(inStream);
PrintWriter out = new PrintWriter(outStream, true);

out.println("hello! enter BYE to exit.");

boolean done = false;

while(!done && in.hasNextLine())
{
String line = in.nextLine();
out.println("Echo:" + line);
if(line.trim().equals("BYE"))
done = true;
}
}
finally
{ incoming.close();}
}
catch(IOException e)
{
}

}
}


程序运行后,用telnet 127.0.0.1 8189连接服务器,然后输入
Hello Server
服务器返回
Echo:Hello Server

然后再输入BYE
服务器返回
Echo:BYE

有个问题是:循环的条件是 while(!done && in.hasNextLine())
当第一次输入Hello Server之后,in.hasNextLine()应该为false了啊?为什么没有退出循环呢?
...全文
评论 7
收藏 已收藏
分享
返回顶部
testpwd
Java SE 62,621 成员 307,251 内容