我正在尝试制作一个简单的服务器/客户端应用程序来使用套接字。我可以成功地获得服务器和客户端之间的单线通信,例如客户端向服务器发送消息,服务器确认并发送回单条消息。
问题是,当 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);
}}
所以上面的工作正常..现在当我尝试使用 for 循环将 10 行文本打印到服务器,然后从服务器打印 10 行到客户端时,我遇到了问题。例如..这是服务器和客户端..
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");
现在在服务器类中,应该检查发送给它的文本是否有“Finished”命令,如果是这样,打破while循环并开始发送回客户端,我相信BufferedReader为null的唯一方法是如果它是封闭的,所以它是无限循环的。感谢大家的帮助。
while((fromClient = BR.readLine())!=null)
{
if(fromClient.equals("Finished"))
{
break;
}
System.out.println(fromClient);
}