【问题标题】:Client socket doesn't take any input from cmd客户端套接字不接受来自 cmd 的任何输入
【发布时间】:2014-03-02 17:24:18
【问题描述】:

我创建了一个程序,其中服务器向客户端发送文件列表,然后客户端可以请求检查内容。它正确发送文件列表,但客户端不接受来自控制台的任何输入。 这是服务器程序

import java.util.*;
import java.io.*;
import java.net.*;
class TCPServer{
    public static void main(String args[]) throws Exception{
    ServerSocket server = new ServerSocket(4888);
    while(true){
    Socket client = server.accept();
    System.out.println(client);
    DataOutputStream out = new DataOutputStream(client.getOutputStream());
    File path = new File("C://testjava");
    String[] files = path.list();
    String send = "";
    for(String file:files){
        send = send + file + "\n";
    }
    out.writeBytes(send);
    BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream    ()));
    String search_file = in.readLine();
    String searching = "";
    for(String file:files){
        if (file.equals(search_file)){
        searching = search_file;
        }
    }
if(searching.equals("")){
        out.writeBytes("Requested file does not exist");
        client.close();
    }
    Scanner file = new Scanner(new FileReader(searching));
    while(file.hasNextLine()){
        out.writeBytes(file.nextLine());
    }
    client.close();
    }
    }
}

这是客户端程序

import java.util.*;
import java.io.*;
import java.net.*;
class TCPClient{
    public static void main(String args[]) throws Exception{
        Socket client = new Socket("localhost",4888);
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        String  display = ""; 
        while ((display = in.readLine()) != null) {
            System.out.println(display);
        }
        System.out.println("\nChoose a file");
        Scanner src = new Scanner(System.in);
        String ask_file = src.nextLine();
        DataOutputStream out = new DataOutputStream(client.getOutputStream());
        out.writeBytes(ask_file);
        display = ""; 
        while ((display = in.readLine()) != null) {
            System.out.println(display);
        }
    }
}

谁能解释为什么客户端不接受任何输入? 谢谢

【问题讨论】:

    标签: java sockets io


    【解决方案1】:

    在客户端,in.readLine() 阻塞,直到 Socket 关闭。

    由于您显然还不想关闭套接字,您可以让服务器发送一条特殊消息以在循环中匹配 for。匹配时,跳出循环。

    此外,类似 readLine/nextLine 的方法会占用换行符,因此您需要像 @EJP 所说的那样添加一些。我在下面编辑了您的代码。我测试了它,它现在似乎可以工作了。

    TCP服务器

    import java.util.*;
    import java.io.*;
    import java.net.*;
    class TCPServer{
        public static void main(String args[]) throws Exception{
        ServerSocket server = new ServerSocket(4888);
        while(true){
        Socket client = server.accept();
        System.out.println(client);
        DataOutputStream out = new DataOutputStream(client.getOutputStream());
        File path = new File("C://Users/Brian/Desktop");
        String[] files = path.list();
        String send = "";
        for(String file:files){
            send = send + file + "\n";
        }
        send = send + "END\n"; // ADD SOMETHING LIKE THIS ------------------------------>
        out.writeBytes(send);
        BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream    ()));
        String search_file = in.readLine();
        String searching = "";
        for(String file:files){
            if (file.equals(search_file)){
            searching = search_file;
            }
        }
    if(searching.equals("")){
            out.writeBytes("Requested file does not exist");
            client.close();
        }
        Scanner file = new Scanner(new FileReader(searching));
        while(file.hasNextLine()){
            out.writeBytes(file.nextLine() + "\n"); // ADD A NEWLINE HERE ------------------>
        }
        client.close();
        }
        }
    }
    

    TCP客户端

    import java.util.*;
    import java.io.*;
    import java.net.*;
    class TCPClient{
        public static void main(String args[]) throws Exception{
            Socket client = new Socket("localhost",4888);
            BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
            String  display = ""; 
            // ADD A TEST FOR "END" HERE --------------------------------------------->
            while ((display = in.readLine()) != null && !display.equals("END")) {
                System.out.println(display);
            }
            System.out.println("\nChoose a file");
            Scanner src = new Scanner(System.in);
            String ask_file = src.nextLine() + "\n"; // ADD A NEWLINE HERE ----------->
            DataOutputStream out = new DataOutputStream(client.getOutputStream());
            out.writeBytes(ask_file);
            display = ""; 
            while ((display = in.readLine()) != null) {
                System.out.println(display);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      你在读行,但不是在写行。您需要在使用writeBytes() 编写的字符串中添加\n。否则readLine() 将永远阻塞等待永远不会到达的行终止符。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-15
        • 2020-07-18
        • 2010-10-26
        • 2023-03-10
        • 2016-07-29
        • 1970-01-01
        • 1970-01-01
        • 2016-10-20
        相关资源
        最近更新 更多