无法连接到端口7
问题描述:
运行下面的代码给予例外:无法连接到端口7
d:\书籍\教程\网络\插座\实例>的Java EchoClient 拒绝连接:连接
这实际上是从java教程的代码(从oracle) 我甚至尝试从防火墙打开icmp eco请求,任何人都可以解释为什么这是哈特?我正在使用Windows 7 32位
import java.io.*;
import java.net.*;
public class EchoClient {
public static void main(String[] args) throws Exception {
Socket echoSocket = null;
PrintWriter out = null;
BufferedReader in = null;
try {
echoSocket = new Socket("localhost", 7);
out = new PrintWriter(echoSocket.getOutputStream(), true);
in = new BufferedReader(new InputStreamReader(
echoSocket.getInputStream()));
} catch (Exception e) {
System.err.println(e.getMessage());
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(
new InputStreamReader(System.in));
String userInput;
while ((userInput = stdIn.readLine()) != null) {
out.println(userInput);
System.out.println("echo: " + in.readLine());
}
out.close();
in.close();
stdIn.close();
echoSocket.close();
}
}
答
无论如何,在Unix机器上,您必须拥有超级用户权限(大致等同于Windows上的管理员权限)才能打开小于1024的端口号用于侦听。您可能会遇到Windows上的等效问题。另一方面,仔细观察代码表明您正试图将其作为客户端连接到客户端,而不需要提升特权。
也许麻烦的是没有服务(守护进程)监听端口 - 所以没有什么可以响应你的回应请求?
嘿,我读了端口7是回声端口的地方,我甚至尝试添加防火墙例外,但它仍然不工作 – lol
我修改了我的答案 - 也许麻烦的是,你没有配置回声服务。 –
k如何配置呢? – lol