它不会从服务器返回任何内容!
我有 2 个重要的类(客户端和服务器),我将在文本区域中写一些内容,然后通过单击发送按钮,我将调用客户端类的活动方法,然后将该文本发送到我的客户端类,一切都是好的,该文本也会打印在服务器控制台上,但我无法将该文本从服务器回显到客户端,请帮助我,谢谢。
客户端类:(其中的一部分)
os = new PrintWriter(c.getOutputStream(), true);
is = new BufferedReader(new InputStreamReader(c.getInputStream()));
public static void active() {
String teXt = MainClient.getText();
os.println(teXt);
String line = is.readLine();
System.out.println("Text received: " + line);
os.flush();
is.close();
is.close();
c.close();
服务器类:(其中的一部分)
BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter streamOut =new PrintWriter(client.getOutputStream());
boolean done = false;
String line =null;
while (!done ) {
line = streamIn.readLine();
if (line.equalsIgnoreCase("bye")) {
done = true;
} else {
System.out.println(line);
streamOut.println(line);
}
}
streamIn.close();
client.close();
server.close();
I have 2 important classes(client and server) and I will write something in my text area and by clicking on the send button I will call the active method of the client class and I will send that text to my client class,every thing is ok and that text also will be printed on the server console but I can not echo that text from server to client,please help me thanks.
client class:( a part of that)
os = new PrintWriter(c.getOutputStream(), true);
is = new BufferedReader(new InputStreamReader(c.getInputStream()));
public static void active() {
String teXt = MainClient.getText();
os.println(teXt);
String line = is.readLine();
System.out.println("Text received: " + line);
os.flush();
is.close();
is.close();
c.close();
server class:( a part of that)
BufferedReader streamIn = new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter streamOut =new PrintWriter(client.getOutputStream());
boolean done = false;
String line =null;
while (!done ) {
line = streamIn.readLine();
if (line.equalsIgnoreCase("bye")) {
done = true;
} else {
System.out.println(line);
streamOut.println(line);
}
}
streamIn.close();
client.close();
server.close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实际上 Nettogrof 正在走正确的道路,但您还必须刷新服务器端:
或者只是创建 AutoFlush 设置为 true 的 PrintWriter:
一个注意事项:您还应该测试 readLine() 是否返回 null,因为客户端将在不关闭连接的情况下关闭连接发送“再见”。
第二个注意事项:PrintWriter 的实例永远不会抛出 IOExceptions,您应该测试调用
checkError()
的错误,这也会刷新流。actually Nettogrof is going the correct way, but you must also flush the server side:
or just create the PrintWriter with autoFlush set to true:
One note: you should also test if readLine() is returning null since the client will close the connection without sending a "bye".
A second note: instances of PrintWriter never throw IOExceptions, you should test for errors calling
checkError()
, which also flushes the stream.在阅读服务器答案之前,您需要“ os.flush(); ”。
因为根据您的客户端代码,您准备要发送的文本
然后您等待服务器答复:
然后您将文本发送到服务器:
尝试:
You need to " os.flush(); " before reading the server answer.
Because according to your client code, you prepare the text to send with
Then you wait for server answer by :
Then you send your text to the server :
try :
您的服务器代码实现是错误的,由于无限循环,streamIn、client 和streamOut 永远不会关闭。
请参阅 Medopal 提到的文章以获得更多帮助。
Your server code implementation is wrong, streamIn,client and streamOut are never closed because of infinite loop.
Refer article mentioned by medopal for more help.
输入流被读取的频率是多少?从代码来看,似乎有一次读取,可能是在从服务器发送任何内容之前,仅此而已。如果您要使用您所采用的方法,您可能必须对服务器进行更一致的轮询。
像这样的东西:
How frequently is the input stream being read? From the code, it looks like there is a single read, probably before anything has been sent from the server, and that's it. You'll probably have to do more consistent polling of the server if you're going to to use the approach you've taken.
Something like: