2012-12-07 53 views
0

你好傢伙我試圖做一個回聲服務器的Java,但它沒有工作..我不知道爲什麼..但它似乎是服務器正在等待客戶端和客戶端正在等待服務器...所以他們不能提供的infromation彼此..Java Echo服務器

這裏是服務器

ServerSocket server = null; 
     try { 
      server = new ServerSocket(3333); 
       System.out.println("Listening on 3333"); 
    } catch (IOException ex) { 
     System.out.println("Error can't connect to 3333"); 
     System.exit(1); 
    } 

     Socket clientSocket = null; 
    try { 
     clientSocket = server.accept(); 
    } catch (IOException ex) { 
     System.out.println("Accept fail"); 
     System.exit(1); 
    } 
    PrintWriter out = null; 
    try { 
     out = new PrintWriter(clientSocket.getOutputStream()); 
    } catch (IOException ex) { 
     Logger.getLogger(JavaApplication20.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    BufferedReader br = null; 
    try { 
     br = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 
    } catch (IOException ex) { 
     Logger.getLogger(JavaApplication20.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in)); 

    String inputLine, outputLine; 
while(!(inputLine=br.readLine()).equals("bye")) 
      { 
       out.print("echo: " + inputLine); 

      } 
    out.close(); 
    br.close(); 
    clientSocket.close(); 
    server.close(); 
    System.out.println("Server Exited"); 

,這裏的代碼 是客戶端

Socket client = null; 
    try { 
     client = new Socket("localhost", 3333); 
     System.out.println("Connected on 3333"); 
    } catch (UnknownHostException ex) { 
     System.out.println("Couldn't connect to the server"); 
     System.exit(1); 
    } catch (IOException ex) { 
     Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex); 
    } 


    PrintWriter out = null; 
    BufferedReader in = null; 
    BufferedReader stdIn = null; 

    try { 
     out = new PrintWriter(client.getOutputStream(), true); 
    } catch (IOException ex) { 
     Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    try { 
     in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
    } catch (IOException ex) { 
     Logger.getLogger(KnockKnockClient.class.getName()).log(Level.SEVERE, null, ex); 
    } 
    stdIn = new BufferedReader(new InputStreamReader(System.in)); 

    String fromServer, fromUser; 


    while((fromUser=stdIn.readLine())!=null) 
     { 
      System.out.println("From user: "+ fromUser); 

      out.print(fromUser); 
      fromServer=in.readLine(); 
      System.out.println(fromServer); 
     } 



    out.close(); 
    stdIn.close(); 
    in.close(); 
    client.close(); 

    System.out.println("client Exited"); 
代碼

任何幫助?

回答

3

你發送來自客戶端的一些字符串("Hello"爲例),而你試圖用readLine()在服務器(反之亦然),以讀取它。 readLine()只有在找到EOL字符或輸入流關閉後纔會返回。

由於客戶端不發送任何EOL字符,服務器無限期地等待,客戶端也因爲它等待來自服務器的回答。

發送"Hello\n",它會工作得更好。

+0

你說什麼它對我有意義..但我怎麼能解決這個問題...我的輸入是你好\ n但我仍然有同樣的問題..我也用out.flush停止readline但同樣的問題.. 有什麼建議麼 ?? –

+0

在您的代碼中添加痕跡或調試它。你是否還向服務器發送回客戶端的字符串添加了'\ n'? –

+0

工作:) ..... –

1

out.print(fromUser);之後在您的客戶端和服務器中使用out.flush()。沖洗將確保它將正確地插入插座。

while((fromUser=stdIn.readLine())!=null) 
    { 
     System.out.println("From user: "+ fromUser); 

     out.print(fromUser); 
     out.flush(); 
     fromServer=in.readLine(); 
     System.out.println(fromServer); 
    } 



out.close(); 
stdIn.close(); 
in.close(); 
client.close(); 

關於刷新,摘自java doc。

Flushes the stream. If the stream has saved any characters from the various write()  methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams. 

    If the intended destination of this stream is an abstraction provided by the underlying operating system, for example a file, then flushing the stream guarantees only that bytes previously written to the stream are passed to the operating system for writing; it does not guarantee that they are actually written to a physical device such as a disk drive. 
+0

我做了out.flush()但我仍然得到相同的問題 例如,如果我的輸入是你好 輸出是從用戶:你好......但它沒有得到從服務器的響應 –

+0

@我在你的服務器上說過使用out.flush()後out.print(「echo:」+ inputLine); – Suranga

+0

是的,我已經做了,但我仍然....不正確..並且它不會回顯數值..並且readline不斷聽取輸入... 客戶端卡在這一行 fromUser = stdIn。 readLine() 和服務器卡在這裏inputLine = br.readLine() –