简体   繁体   English

如何在使用reader.readLine()和sc.nextLine()时避免阻塞

[英]How to avoid blocking while using reader.readLine() and sc.nextLine()

I am using reader.readLine() and sc.nextLine() to simulate the server and client. 我正在使用reader.readLine()sc.nextLine()来模拟服务器和客户端。 However, after I typed some words in the scanner, the server responded nothing. 但是,在扫描仪中输入了一些单词后,服务器什么也没有响应。 I think the problem is thread blocking, but I can't correct it. 我认为问题是线程阻塞,但我无法纠正它。 Could any one help point out where the sticking point is. 任何人都可以指出问题的症结所在。

Here is the code for server. 这是服务器的代码。

public class Server {

    public static LocalDateTime currentTime() {
        return LocalDateTime.now();
    }
    public static void main(String[] args) throws IOException, InterruptedException {
        ServerSocket ss = new ServerSocket(9091);
        System.out.println("TCP server ready.\n");
        Socket sock = ss.accept();
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(sock.getInputStream(), StandardCharsets.UTF_8))) {
            try (BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(sock.getOutputStream(), StandardCharsets.UTF_8))) {
                String cmd;
                System.out.println("read in");
                while ((cmd = reader.readLine()) != null) {
                    System.out.println("Rcvd: " + cmd);
                    if ("time".equals(cmd)) {
                        writer.write(currentTime() + "\n");
                        writer.flush();
                    } else {
                        writer.write("Sorry?\n");
                        writer.flush();
                    }   
                }    
            }
        }
        sock.close();
        ss.close();

    }

}

The code for client 客户端代码

public class Client {

    public static void main(String[] args) throws IOException, InterruptedException {
        InetAddress addr = InetAddress.getLoopbackAddress();
        try (Socket sock = new Socket(addr, 9091)){
            try (BufferedReader reader = new BufferedReader(
                    new InputStreamReader(sock.getInputStream(), StandardCharsets.UTF_8))){
                try (BufferedWriter writer = new BufferedWriter(
                        new OutputStreamWriter(sock.getOutputStream(), StandardCharsets.UTF_8))){
                    Scanner sc = new Scanner(System.in);
                    String cmd;
                    while (sc.hasNext()) {
                        cmd = sc.nextLine();
                        System.out.println("Scanned: " + cmd);
                        writer.write(cmd);
                        writer.flush();

                        String resp = reader.readLine();
                        System.out.println("Response: " + resp);
                    }

                }
            }
        }

Use this in Client: 在客户端中使用此命令:

writer.write(cmd + "\n");

since the server read lines. 由于服务器读取了行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用“ while(line = reader.readLine()!= null)”时,类型不匹配 - Type mismatch when using “while(line = reader.readLine() != null)” Java Scanner sc.nextLine()的问题; - Problem with Java Scanner sc.nextLine(); 为什么使用sc.nextLine()不是将控制转移到下一行输入的正确方法? - Why using sc.nextLine() isn't the correct way to transfer control to next line of input ? 如何在reader.readLine()期间检测第一行和最后一行? - How to detect first and last line during reader.readLine()? 如何从reader.readLine()返回的String中删除unicode空格 - how to remove unicode spaces from String returned by reader.readLine() 我想在我的组织 java 应用程序中接受经理的全名……我尝试使用 sc.nextLine(); 但它显示 InputMisMatch 错误 - I want to accept full name of manager in my organization java app…i tried using sc.nextLine(); but it is showing InputMisMatch error Java 在 reader.readLine() 从服务器读取数据时出现套接字错误 - Java socket error while reading data from server at reader.readLine() 为什么要使用 Integer.parseInt(sc.nextLine()); 在这个程序中而不是 sc.nextInt()? - Why should I use Integer.parseInt(sc.nextLine()); in this program and not sc.nextInt()? 在 Integer.parseInt(sc.nextLine()) 之后使用 sc.nextInt() 获取输入? - Getting input with sc.nextInt() after Integer.parseInt(sc.nextLine())? 使用ProcessBuilder执行Linux命令并从reader.readLine()获取NULL,尽管行数为102 - Using ProcessBuilder to execute Linux Command and getting NULL from reader.readLine() although line count is 102
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM