我正在嘗試製作一個簡單的服務器/客戶端應用程序來讓我的頭部使用套接字。我可以成功地獲得服務器和客戶端之間的單線通信,例如客戶端向服務器發送消息,服務器確認併發回單個消息。從服務器到客戶端沒有什麼返回
問題是,當我嘗試讀取從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);
}
你是第一個正確回答,謝謝你啊! –