它不会从服务器返回任何内容!

发布于 2024-08-18 08:54:07 字数 1180 浏览 1 评论 0原文

我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

桜花祭 2024-08-25 08:54:07

实际上 Nettogrof 正在走正确的道路,但您还必须刷新服务器端:

        line = streamIn.readLine();
        if (line.equalsIgnoreCase("bye")) {
            done = true;
        } else {
            System.out.println(line);
            streamOut.println(line);
            streamOut.flush();    // or ...checkError();
        }

或者只是创建 AutoFlush 设置为 true 的 PrintWriter:

    PrintWriter streamOut = new PrintWriter(client.getOutputStream(), true);

一个注意事项:您还应该测试 readLine() 是否返回 null,因为客户端将在不关闭连接的情况下关闭连接发送“再见”。

第二个注意事项:PrintWriter 的实例永远不会抛出 IOExceptions,您应该测试调用 checkError() 的错误,这也会刷新流。

actually Nettogrof is going the correct way, but you must also flush the server side:

        line = streamIn.readLine();
        if (line.equalsIgnoreCase("bye")) {
            done = true;
        } else {
            System.out.println(line);
            streamOut.println(line);
            streamOut.flush();    // or ...checkError();
        }

or just create the PrintWriter with autoFlush set to true:

    PrintWriter streamOut = new PrintWriter(client.getOutputStream(), 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.

夜灵血窟げ 2024-08-25 08:54:07

在阅读服务器答案之前,您需要“ os.flush(); ”。

因为根据您的客户端代码,您准备要发送的文本

 String teXt = MainClient.getText();

 os.println(teXt);

然后您等待服务器答复:

String line = is.readLine();
System.out.println("Text received: " + line);

然后您将文本发送到服务器:

os.flush();

尝试:

String teXt = MainClient.getText();

os.println(teXt);
os.flush();
String line = is.readLine();
System.out.println("Text received: " + line);

You need to " os.flush(); " before reading the server answer.

Because according to your client code, you prepare the text to send with

 String teXt = MainClient.getText();

 os.println(teXt);

Then you wait for server answer by :

String line = is.readLine();
System.out.println("Text received: " + line);

Then you send your text to the server :

os.flush();

try :

String teXt = MainClient.getText();

os.println(teXt);
os.flush();
String line = is.readLine();
System.out.println("Text received: " + line);
空宴 2024-08-25 08:54:07

您的服务器代码实现是错误的,由于无限循环,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.

A君 2024-08-25 08:54:07

输入流被读取的频率是多少?从代码来看,似乎有一次读取,可能是在从服务器发送任何内容之前,仅此而已。如果您要使用您所采用的方法,您可能必须对服务器进行更一致的轮询。

像这样的东西:

while (line = is.readLine() != null ) {
    System.out.println("Text received: " + line);
}

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:

while (line = is.readLine() != null ) {
    System.out.println("Text received: " + line);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文