You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java Socket服务端else if分支不运行 客户端按次打印输入失败

错误原因
  • 语法错误:服务端else if (str != null))多了一个闭合右括号,会直接触发编译异常,是分支无法执行的基础原因。
  • 客户端读取逻辑缺陷:服务端在数字分支下会调用多次writeUTF()返回数据(1次提示信息 + 对应次数的文本内容),但客户端仅调用1次readUTF(),只能拿到第一条返回结果,剩余返回内容未被接收打印。
  • 异常被静默吞噬:数字提取逻辑的try-catch块没有任何处理逻辑,当输入内容不含数字时,提取到的数字串为空,Integer.parseInt会抛出NumberFormatException,异常被吞后服务端不会给客户端返回对应响应,也不会输出错误日志,无法定位问题。
  • 正则表达式冗余:提取纯文本的正则[^a-z,^A-Z]中多余了逗号,正确写法应为[^a-zA-Z]。
修正代码

MyServer.java

import java.io.*;
import java.net.*;

public class MyServer {
    public static void main(String[] args){
        try{
            ServerSocket ss=new ServerSocket(5001);
            Socket s=ss.accept();
            DataInputStream dis=new DataInputStream(s.getInputStream());
            String str=(String)dis.readUTF();
            System.out.println("message= "+str);

            DataOutputStream dout=new DataOutputStream(s.getOutputStream());
            if ("Hi".equals(str)){
                dout.writeUTF("How are you?");
            } else if ("Bye".equals(str)){
                dout.writeUTF("Thankyou! Have a Good day!");
            } else if (str != null){
                try {
                    String numbers = str.replaceAll("[^0-9]", "");
                    // 先判断数字串是否为空,避免解析异常
                    if(numbers.isEmpty()){
                        dout.writeUTF("输入内容未包含有效数字");
                    }else {
                        int number = Integer.parseInt(numbers);
                        dout.writeUTF("The line is being printed");
                        String text = str.replaceAll("[^a-zA-Z]", "");
                        for (int i = 0; i < number; i++) {
                            dout.writeUTF(text);
                        }
                    }
                } catch (Exception e) {
                    // 异常时返回错误提示,同时打印日志方便排查
                    dout.writeUTF("输入格式错误:"+e.getMessage());
                    e.printStackTrace();
                }
            } else {
                dout.writeUTF("Sorry!");
            }
            // 新增结束标识,方便客户端判断读取终止
            dout.writeUTF("END");
            dout.flush();
            dout.close();
            s.close();
            ss.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

MyClient.java

import java.io.*;
import java.net.*;
import java.util.*;

public class MyClient {
    public static void main(String[] args) {
        try{
            Scanner sc = new Scanner(System.in);
            Socket s=new Socket("localhost",5001);
            DataOutputStream dout=new DataOutputStream(s.getOutputStream());
            String str1= sc.nextLine();
            dout.writeUTF(str1);
            dout.flush();

            DataInputStream dis=new DataInputStream(s.getInputStream());
            String str;
            // 循环读取所有返回内容,直到遇到结束标识停止
            while (!(str = dis.readUTF()).equals("END")) {
                System.out.println("message= " + str);
            }

            dout.close();
            dis.close();
            s.close();
        } catch(Exception e) {
            System.out.println(e);
        }
    }
}

内容的提问来源于stack exchange,提问作者amnaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 19:54:06