java服务器(简单示例)仅显示发送的第一个字符串

发布于 2025-01-03 08:51:40 字数 1607 浏览 2 评论 0原文

您好,我想通过在一个非常简单的 java 服务器上测试我的客户端应用程序是否正常工作。我的客户几乎每秒都会发送一个字符串。我的想法是,服务器一旦收到该字符串,就会在 eclipse 控制台中显示该字符串。但问题是简单的java服务器只打印出第一个字符串。下面是我从 site 获得的简单 java 服务器

  package serverjava;

/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class serverjava {
    private static int port = 1234; /* port the server listens on */

    public static void main (String[] args) throws IOException {
        ServerSocket server = null;
        try {
            server = new ServerSocket(port); /* start listening on the port */
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port);
            System.err.println(e);
            System.exit(1);
        }

        Socket client = null;
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.err.println(e);
            System.exit(1);
        }

        /* obtain an input stream to the client */
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));

        String msg;
        /* loop reading lines from the client and display them */
        while ((msg = in.readLine()) != null) {
            System.out.println("Client says: " + msg);
        }
    }
}

java 服务器是否能够每次启动仅显示字符串的一个实例?如果是,我怎样才能让它在控制台中打印或显示每秒发送的所有字符串?

Hi i want to test if my client app is working by testing it on a very simple java server. My client will send a string almost every second. My idea is that the server will display the string in the eclipse console once it receives the string. But the problem is that the simple java server only prints out the first string. Below is the simple java server i got from the site

  package serverjava;

/* ChatServer.java */
import java.net.ServerSocket;
import java.net.Socket;

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

public class serverjava {
    private static int port = 1234; /* port the server listens on */

    public static void main (String[] args) throws IOException {
        ServerSocket server = null;
        try {
            server = new ServerSocket(port); /* start listening on the port */
        } catch (IOException e) {
            System.err.println("Could not listen on port: " + port);
            System.err.println(e);
            System.exit(1);
        }

        Socket client = null;
        try {
            client = server.accept();
        } catch (IOException e) {
            System.err.println("Accept failed.");
            System.err.println(e);
            System.exit(1);
        }

        /* obtain an input stream to the client */
        BufferedReader in = new BufferedReader(new InputStreamReader(
                    client.getInputStream()));

        String msg;
        /* loop reading lines from the client and display them */
        while ((msg = in.readLine()) != null) {
            System.out.println("Client says: " + msg);
        }
    }
}

Is the java server capable of displaying only an instance of a string per launch? If yes, how can I make it to print or show in the console all the strings that are being sent every second?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

白馒头 2025-01-10 08:51:40

您遇到的问题是您只读取缓冲区一次。服务器等待客户端行,当它读取它们时,它会完成执行,因为“msg”为空。您必须有一个每秒、每分钟或每小时读取一次的进程......这意味着使用线程

小心!不要使用类似的 while(true) 结构,因为机器将超载。

The problem you have is that you read the buffer only one time. The server waits for client lines and when it reads them, then it finish the execution because 'msg' is null. You must have a process that reads every second or minute, or hour... That means using THREADS.

Be careful! Don't use a while(true) structure of something like that, because the machine will be overloaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文