2013-02-04 73 views
0

我正在嘗試製作一個簡單的服務器/客戶端應用程序來讓我的頭部使用套接字。我可以成功地獲得服務器和客戶端之間的單線通信,例如客戶端向服務器發送消息,服務器確認併發回單個消息。從服務器到客戶端沒有什麼返回

問題是,當我嘗試讀取從BufferedReader中有多行時從服務器回到客戶端的傳入答覆。

示例...

服務器和客戶端互相提供一條線。

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

class Server { 
public static void main(String args[])throws IOException 
{ 
    Server myServ = new Server(); 
    myServ.run(); 
}//end main 
public void run() throws IOException 
{ 
ServerSocket serSKT = new ServerSocket(729); 
Socket socket = serSKT.accept(); 

BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
PrintWriter PW = new PrintWriter(socket.getOutputStream()); 

String fromClient = BR.readLine(); 
System.out.println(fromClient); 

if(fromClient !=null) 
{PW.println("Server giving response to client");PW.flush();} 
}}//end class server 

客戶:

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

class Client 
{ 
public static void main(String args[])throws IOException 
{ 
Client myCli = new Client(); 
myCli.run(); 
} 
public void run() throws IOException 
{ 
int port = 729; 

Socket mySkt = new Socket("localhost",port); 

PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true); 

BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream())); 

myPW.println("Message from client to server"); 

String temp=myBR.readLine(); 
System.out.println(temp); 
}} 

因此上述作品fine..now當我嘗試從服務器到使用for循環打印文本到服務器的10線,然後10號線回客戶端,我遇到問題。對於example..Here是服務器和客戶端..

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

class Server { 
public static void main(String args[])throws IOException 
{ 
    Server myServ = new Server(); 
    myServ.run(); 
}//end main 
public void run() throws IOException 
{ 
ServerSocket serSKT = new ServerSocket(729); 
Socket socket = serSKT.accept(); 

BufferedReader BR = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
PrintWriter PW = new PrintWriter(socket.getOutputStream()); 
String fromClient = null; 
while((fromClient = BR.readLine())!=null) 
{ 
System.out.println(fromClient);//this is being printed successfully 
} 

if(fromClient !=null) 
{ 
for(int i=0;i<10;i++){ 
PW.println("Server giving response to client"+i);} 
PW.flush();} 
}}//end class server 

客戶:

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

class Client 
{ 
public static void main(String args[])throws IOException 
{ 
Client myCli = new Client(); 
myCli.run(); 
} 
public void run() throws IOException 
{ 
int port = 729; 

Socket mySkt = new Socket("localhost",port); 

PrintWriter myPW = new PrintWriter(mySkt.getOutputStream(),true); 

BufferedReader myBR = new BufferedReader(new InputStreamReader(mySkt.getInputStream())); 

for(int i =0;i<10;i++) 
myPW.println("Message from client to server"+i); 

String temp=null; 
while((temp=myBR.readLine())!=null){ 
System.out.println(temp);} 
}} 

上面會打印出消息10倍到服務器,服務器將顯示哪些客戶端發送它成功:

while((fromClient = BR.readLine())!=null) 
{ 
System.out.println(fromClient);//this is being printed successfully 
} 

我不明白的是爲什麼客戶端將不會收到消息「服務器給予響應客戶端」,並打印到控制檯:

String temp=null; 
while((temp=myBR.readLine())!=null){ 
System.out.println(temp);} //should be printing "Server giving repsonse to client" 

對不起,這很長,謝謝,但我試圖沒有成功!

****編輯* **

櫃面任何人都遇到這個問題,這是由客戶端類打印的命令到PrintWriter的允許解決服務器知道它已經完成。例如:

for(int i =0;i<10;i++){ 
myPW.println("Message from client to server"+i);} 
myPW.println("Finished"); 

現在在服務器類的形式發送給它應該爲「完成」命令來檢查,如果是的話,打出來的,而循環,並開始發送回客戶端,我相信, BufferedReader的唯一方法是null,如果它是關閉的,所以它是無限循環的。感謝大家的幫助。

while((fromClient = BR.readLine())!=null) 
{ 
if(fromClient.equals("Finished")) 
{ 
break; 
} 
System.out.println(fromClient); 
} 

回答

2

它看起來像你從來沒有退出在服務器中的while()循環,10日線後,服務器等待另一行(和,又一本,等等)。 客戶端必須通知服務器他已經完成發送,例如,通過發送一個特殊的線。 然後您應該退出服務器上的while循環並開始發送。

+0

你是第一個正確回答,謝謝你啊! –

1

我認爲這個問題是:

while((fromClient = BR.readLine())!=null) 
{ 
System.out.println(fromClient);//this is being printed successfully 
} 

BR.readLine()不會返回null直到流結束(如關閉/斷開)。您不應將此用作您的休息條件,它將在readLine()之內等待更多數據出現在數據流中。

+0

非常感謝或此。 –

1

您正在等待客戶端關閉流,即在br.readLine() == null之前發送任何內容。發生這種情況後fromClient會在這種情況下,空發送反正沒事

我建議你嘗試

for (String s; (s = BR.readLine())!=null;) { 
    System.out.println(s); 
    PW.println("Server response to " + s); 
} 
+1

非常感謝...如果他們卡住了,我已經爲其他人更新了問題。 –

相關問題