非常困惑,我刚刚发布了这个问题但删除了它,因为我犯了很多错误。好吧,好了又来了!我在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&#34;是相关的,因为我不能解释其余的。我不知道它在那里。希望有人帮忙!
好的我只是发现更多因为我写这个因为我不能发布90分钟,但现在当我使用intellij想法并在那里运行它。只是服务器,没有变化,但是当我添加客户端时,我看到了一切!
之前: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个...上帝这很有意思,或者我只是犯了一个非常愚蠢的错误。但我开始质疑这些问题。
答案 0 :(得分:0)
netstat默认省略侦听套接字。所有套接字都有-l选项或-all。 - laune