2014-02-06 37 views
0

我创建了一个程序,其中服务器向客户端发送文件列表,然后客户端可以请求检查其内容。它正确地发送文件列表,但客户端不会从控制台接收任何输入。 这是服务器程序客户端套接字不接受来自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

回答

1

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

因为你显然不想关闭套接字,所以你可以让服务器发送一个特殊的消息来匹配循环。匹配时,跳出循环。

此外,readLine/nextLine类似的方法吞噬换行符,所以你需要添加一些像@EJP所说的。我在下面编辑你的编码。我测试了它,现在它似乎在工作。

TCPSERVER

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(); 
    } 
    } 
} 

的TcpClient

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); 
     } 
    } 
} 
0

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

相关问题