【问题标题】:ServerSocket doesn't appear in Netstat?ServerSocket 没有出现在 Netstat 中?
【发布时间】:2014-12-24 18:25:52
【问题描述】:

很困惑,我刚刚发布了这个问题但因为我犯了很多错误而将其删除。好吧,这里又来了!我在下面的java中有一个服务器程序。当我运行它时,我希望在 netstat 中看到某种存在,但我什么也没看到。以下是一些屏幕截图:

运行服务器前:https://www.dropbox.com/s/upo9ndbzuxbk9j1/Screenshot%202014-12-24%2002.25.49.png?dl=0

运行服务器后(服务器运行在右上角终端,右下角是客户端,左边显然是netstat):https://www.dropbox.com/s/05urjvz3lskvzkc/Screenshot%202014-12-24%2002.28.24.png?dl=0

有点长,但这里是服务器(我用 63400 运行它):

import java.net.*;
import java.io.*;

public class JavaTest2 {
    public static void main(String[] args) throws IOException {

        if (args.length != 1) {
            System.err.println("Usage: java KnockKnockServer <port number>");
            System.exit(1);
        }

        int portNumber = Integer.parseInt(args[0]);

        try (
                ServerSocket serverSocket = new ServerSocket(portNumber);
                Socket clientSocket = serverSocket.accept();
                PrintWriter out =
                        new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(clientSocket.getInputStream()));
        ) {

            String inputLine, outputLine;

            // Initiate conversation with client
            KnockKnockProtocol kkp = new KnockKnockProtocol();
            outputLine = kkp.processInput(null);
            out.println(outputLine);

            while ((inputLine = in.readLine()) != null) {
                outputLine = kkp.processInput(inputLine);
                out.println(outputLine);
                if (outputLine.equals("Bye."))
                    break;
            }
        } catch (IOException e) {
            System.out.println("Exception caught when trying to listen on port "
                    + portNumber + " or listening for a connection");
            System.out.println(e.getMessage());
        }
    }
}

class KnockKnockProtocol {
    private static final int WAITING = 0;
    private static final int SENTKNOCKKNOCK = 1;
    private static final int SENTCLUE = 2;
    private static final int ANOTHER = 3;

    private static final int NUMJOKES = 5;

    private int state = WAITING;
    private int currentJoke = 0;

    private String[] clues = { "Turnip", "Little Old Lady", "Atch", "Who", "Who" };
    private String[] answers = { "Turnip the heat, it's cold in here!",
            "I didn't know you could yodel!",
            "Bless you!",
            "Is there an owl in here?",
            "Is there an echo in here?" };

    public String processInput(String theInput) {
        String theOutput = null;

        if (state == WAITING) {
            theOutput = "Knock! Knock!";
            state = SENTKNOCKKNOCK;
        } else if (state == SENTKNOCKKNOCK) {
            if (theInput.equalsIgnoreCase("Who's there?")) {
                theOutput = clues[currentJoke];
                state = SENTCLUE;
            } else {
                theOutput = "You're supposed to say \"Who's there?\"! " +
                        "Try again. Knock! Knock!";
            }
        } else if (state == SENTCLUE) {
            if (theInput.equalsIgnoreCase(clues[currentJoke] + " who?")) {
                theOutput = answers[currentJoke] + " Want another? (y/n)";
                state = ANOTHER;
            } else {
                theOutput = "You're supposed to say \"" +
                        clues[currentJoke] +
                        " who?\"" +
                        "! Try again. Knock! Knock!";
                state = SENTKNOCKKNOCK;
            }
        } else if (state == ANOTHER) {
            if (theInput.equalsIgnoreCase("y")) {
                theOutput = "Knock! Knock!";
                if (currentJoke == (NUMJOKES - 1))
                    currentJoke = 0;
                else
                    currentJoke++;
                state = SENTKNOCKKNOCK;
            } else {
                theOutput = "Bye.";
                state = WAITING;
            }
        }
        return theOutput;
    }
}

现在,当我运行客户端时,我可以在最顶部看到 netstat 的存在,两个套接字作为我的服务器和客户端在同一台计算机上运行。但仍然没有服务器套接字:(

截图:https://www.dropbox.com/s/bnrbyk41mjob5bg/Screenshot%202014-12-24%2002.25.53.png?dl=0

客户端代码(使用 127.0.0.1 64300 运行):

import java.io.*;
import java.net.*;

public class JavaTest {
    public static void main(String[] args) throws IOException {

        if (args.length != 2) {
            System.err.println(
                    "Usage: java KnockKnockClient <host name> <port number>");
            System.exit(1);
        }

        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);

        try (
                Socket kkSocket = new Socket(hostName, portNumber);
                PrintWriter out = new PrintWriter(kkSocket.getOutputStream(), true);
                BufferedReader in = new BufferedReader(
                        new InputStreamReader(kkSocket.getInputStream()));
        ) {
            BufferedReader stdIn =
                    new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;

            while ((fromServer = in.readLine()) != null) {
                System.out.println("Server: " + fromServer);
                if (fromServer.equals("Bye."))
                    break;

                fromUser = stdIn.readLine();
                if (fromUser != null) {
                    System.out.println("Client: " + fromUser);
                    out.println(fromUser);
                }
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " +
                    hostName);
            System.exit(1);
        }
    }
}

提前感谢所有花时间解决问题的人!

我认为它可能只是 netstat 但我仍然想知道为什么当我运行 lsof -i :64300 当只有服务器正在运行时,它显示了我的 java 进程。当我使用我看到的所有内容 3 运行它时(如您在此屏幕截图的左侧终端中看到的 https://www.dropbox.com/s/1focc9dmhkidtan/Screenshot%202014-12-24%2002.52.22.png?dl=0)。在它向我显示我“认为”相关的内容后,我取消了 netstat,因为我无法解释其余部分。我不知道它是否在那里。希望有人帮忙!

好的,我只是在写这篇文章时发现更多,因为我不能发布 90 分钟,但是现在当我使用 intellij idea 并在那里运行它时。只是服务器,没有任何变化,但是当我添加客户端时,我看到了一切!

在任何事情之前:https://www.dropbox.com/s/akupewmprcld0b3/Screenshot%202014-12-24%2002.59.05.png?dl=0

服务器:https://www.dropbox.com/s/uvxmz0jvjozbkyy/Screenshot%202014-12-24%2002.59.34.png?dl=0

客户端+服务器:https://www.dropbox.com/s/bssa16s1n3adwdq/Screenshot%202014-12-24%2003.00.00.png?dl=0

发生了什么事....我还看到 6 个本地主机而不是 3 个...上帝这很有趣,或者我只是犯了一个非常愚蠢的错误。但我开始怀疑这些问题。

【问题讨论】:

  • netstat 默认忽略监听套接字。所有套接字都有一个 -l 选项或 -all 选项。
  • 我不是netstat 专家,但我注意到您的 netstat 列表标题为“活动 Internet 连接”。也许只是监听的服务器还没有“活跃”?
  • 看起来我把它合二为一了。第一条评论指出了另一种解决方案:确保您阅读了有关您使用的工具的文档,以便了解它们的实际工作原理。
  • 果然你是对的。当我用 -all 运行它时,我看到了它,但 netstat -l 我没有。谢谢
  • 您没有将服务器套接字绑定到特定的 IP 地址,因此它显示为“*”。同样,netstat 文档中几乎肯定会提到这一点。

标签: java sockets server serversocket netstat


【解决方案1】:

netstat 默认忽略监听套接字。所有套接字都有一个 -l 选项或 -all 选项。 – 劳恩

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 1970-01-01
    • 2016-01-04
    • 2014-07-17
    • 1970-01-01
    • 1970-01-01
    • 2017-03-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多