我正在尝试使用 java 代码进行简单的 ping 操作,我知道 java 不支持 icmp 所以我只是做了一个简单的代码,使用端口 7 发送请求并等待回显响应
Socket t = new Socket("127.0.0.1", 7);
DataInputStream dis = new DataInputStream(t.getInputStream());
PrintStream ps = new PrintStream(t.getOutputStream());
ps.println("Hello");
String str = dis.readUTF();
if (str.equals("Hello"))
System.out.println("Alive!");
else
System.out.println("Dead");
t.close();
但这给了我
Exception in thread "main" java.net.ConnectException: Connection refused
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:579)
at java.net.Socket.connect(Socket.java:528)
at java.net.Socket.<init>(Socket.java:425)
at java.net.Socket.<init>(Socket.java:208)
at MobileCloud.Ping.main(Ping.java:11)
我读到我应该在 /etc/inetd.conf 文件中进行更改,但我没有这样的文件。