注册 登录
编程论坛 JAVA论坛

readUTF()

rohalloway 发布于 2020-03-30 21:49, 1653 次点击
程序代码:

try {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
            DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream()));

            boolean offon = true;
            
            do
            {

                String reviMsg = dis.readUTF();
                System.out.println("从客户端" + clientNo + "接收到的数据为:" + reviMsg);
               
                if(reviMsg == "end") {
                    System.out.println("offon = false;");
                    offon = false;
                }
            }
            while (offset);
        }



请问,当我在客户端发送 end 时,为什么不能结束循环呢?

打印的reviMsg 明明就是end,谢谢!

我想实现:当检测到客户端发来的数据为end时停止循环
1 回复
#2
rohalloway2020-03-30 22:43
已解决

问题出现在if(reviMsg == "end")

Java中的字符串“内容”比较应使用 reviMsg.equalsIgnoreCase("end")
1