gpt4 book ai didi

java - 从服务器到客户端没有任何返回

转载 作者:行者123 更新时间:2023-12-01 18:54:17 24 4
gpt4 key购买 nike

我正在尝试制作一个简单的服务器/客户端应用程序来了解如何使用套接字。我可以成功地在服务器和客户端之间进行单线通信,例如客户端向服务器发送消息,服务器确认并发送回单条消息。

问题是当 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");

现在,在服务器类中,应该检查发送给它的文本是否有“完成”命令,如果是这样,请跳出 while 循环并开始发送回客户端,我相信 BufferedReader 的唯一方法是null 是如果它是封闭的,那么它是无限循环的。感谢大家的帮助。

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

最佳答案

看起来你永远不会退出服务器中的 while() 循环,在第 10 行之后,服务器等待另一行(以及另一行,另一行,等等)。客户端必须通知服务器他已完成发送,例如通过发送一条特殊线路。然后您应该退出服务器上的 while 循环并开始发送。

关于java - 从服务器到客户端没有任何返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14687182/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com