2017-04-18 22 views
0

我在分配任務時偶然發現了一個死區。這是一個簡單的服務器和客戶端程序。 這個程序的細節如下;簡單客戶端服務器Java程序不會進入while循環

  1. 創建一個服務器類EncryptServer,用於偵聽傳入連接。如果有連接,請接受它並創建一個線程EncryptServerSession來處理輸入和輸出流。
  2. 創建一個線程類EncryptServerSession,它接收來自服務器類的輸入和輸出流並對其進行處理。
  3. 創建一個客戶類EncryptClient,它連接到服務器類並從EncryptServerSessionEncryptServer獲取輸出流。

不知何故EncryptClient類的while循環不起作用。我似乎無法進入循環。我的代碼有問題嗎?提前致謝。

EncryptServer

import java.net.*; 
import java.io.*; 

public class EncryptServer 
{  
    public EncryptServer() throws IOException 
    { 
     ServerSocket serverSocket = new ServerSocket(1122); 
     System.out.println("Server started."); 

     while (true) 
     { 
      Socket conSocket = serverSocket.accept(); 
      System.out.println("Client connected from " + 
conSocket.getLocalAddress().getHostName()); 

      Thread session = new 
EncryptServerSession(conSocket.getInputStream(), 
conSocket.getOutputStream()); 
      session.start(); 
    } 
} 
public static void main(String[] args) 
{ 
    try 
    { 
     EncryptServer server = new EncryptServer(); 
    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
    }          
} 

} 

EncryptServerSession

import java.net.*; 
    import java.io.*; 

public class EncryptServerSession extends Thread 
{ 
    BufferedReader in; 
    Writer out; 

public EncryptServerSession(InputStream inStream, OutputStream outStream) 
{ 
    Reader read = new InputStreamReader(inStream); 
    in = new BufferedReader(read); 
    out = new OutputStreamWriter(outStream); 
} 

public void strEncrypt() 
{ 
    try 
    { 
     String message = in.readLine(); 
     out.write(message); 
     out.flush(); 
    } 
    catch (Exception e) 
    { 
    } 
} 

public void run() 
{ 
    try 
    { 
     //System.out.println(in.readLine()); 
     out.write("Please enter the message to be encrypted: "); 
     out.flush(); 

     //strEncrypt(); 

    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
    } 

} 
} 

EncryptClient

import java.net.*; 
import java.io.*; 


public class EncryptClient 
{ 
BufferedReader input, userTerm; 
Writer output; 
String line; 

public EncryptClient() throws IOException 
{ 
    Socket clientSocket = new Socket("localhost", 1122); 
    Reader read = new InputStreamReader(clientSocket.getInputStream()); 
    input = new BufferedReader(read); 

    userTerm = new BufferedReader(new InputStreamReader(System.in)); 
    output = new OutputStreamWriter(clientSocket.getOutputStream());    

    /////////////// Somehow I cannot get into this loop, why? ////////// 
    while (true) 
    { 
     System.out.println("test "); 
     System.out.println(input.readLine()); 
     System.out.println("Enter message to be encrypted: "); 
     output.write(userTerm.readLine());   
     output.flush(); 
    } 
    /////////////// Somehow I cannot get into this loop, why? ////////// 
} 

public static void main(String[] args) 
{ 
    try 
    { 
     EncryptClient client = new EncryptClient(); 
    } 
    catch (Exception e) 
    { 
     System.out.println(e); 
    } 
} 

}

+0

能不能請你通過你的EncryptClient構造,並添加System.out.pritnln( 「測試」);或者類似於每一行的內容(一個接一個)?這將有助於確切知道你的代碼的哪一行被掛起。 –

+0

在運行EncryptClient的主要方法之前,您是否運行過EncryptServer的主要方法?如果不是,可能是這種情況,因爲您應該首先運行服務器,然後運行客戶端。順便說一句,執行後你的控制檯有什麼輸出? –

+0

我曾經在我的學校網絡上有一個問題,本地主機沒有翻譯成127.0.0.1。你可以在127.0.0.1而不是localhost中嘗試硬編碼。我不是網絡專業人士,但值得一試。還要確保在你的客戶端之前啓動你的服務器,因爲錯誤的最可能的罪魁禍首是你的循環之前的套接字和流。 – noobcoder

回答

0

據我所知,你試圖發送消息到服務器,做一些邏輯,然後將其發送回客戶端。上面的代碼似乎在我身邊運行良好。以下是我所做的:

  • 首先運行EncryptServer。我希望這是你所面臨的問題。或者你的防火牆不會讓你在套接字上聽。
  • in EncryptServerSession,您正在閱讀的文章,但您並未撰寫文章。你可以在close這個流或者在完成後寫一個新的行。

    ... 
    
    out.write(message); 
    out.write("\r\n"); // write new line 
    out.flush(); 
    
    ... 
    
    } finally { 
        try { 
         out.close(); 
        } catch (IOException e) { 
        } 
    } 
    

    OR

    ... 
    
    out.write(message); 
    out.write("\r\n"); // write new line 
    out.flush(); 
    
    ... 
    
+0

嗨,是的,它對我來說也運行良好。服務器和客戶端正在工作。我只是無法進入加密客戶端的循環,不知道爲什麼。 –