2014-02-06 69 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()會永遠等待永不會到達的行終止符。

相關問題