我創建了一個程序,其中服務器向客戶端發送文件列表,然後客戶端可以請求檢查其內容。它正確地發送文件列表,但客戶端不會從控制檯接收任何輸入。 這是服務器程序客戶端套接字不接受來自cmd的任何輸入
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);
}
}
}
任何人都可以解釋爲什麼客戶不接受任何輸入? Thanx