Java 网络“拒绝连接:连接" [英] Java Networking "Connection Refused: Connect"

查看:65
本文介绍了Java 网络“拒绝连接:连接"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让一个简单的网络测试程序运行但没有结果.

I have been trying to get a simple networking test program to run with no results.

服务器:

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

public class ServerTest {
    public static void main(String[] args) {
    final int PORT_NUMBER = 44827;

    while(true) {
        try {
        //Listen on port
        ServerSocket serverSock = new ServerSocket(PORT_NUMBER);
        System.out.println("Listening...");

        //Get connection
        Socket clientSock = serverSock.accept();
        System.out.println("Connected client");

        //Get input
        BufferedReader br = new BufferedReader(new InputStreamReader(clientSock.getInputStream()));
        System.out.println(br.readLine());

        br.close();
        serverSock.close();
        clientSock.close();
        } catch(Exception e) {
        e.printStackTrace();
        }
    }
    }
}

客户:

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

public class ClientTest {
    public static void main(String[] args) throws IOException {
    final int PORT_NUMBER = 44827;
    final String HOSTNAME = "xx.xx.xx.xx";

    //Attempt to connect
    try {
        Socket sock = new Socket(HOSTNAME, PORT_NUMBER);
            PrintWriter out = new PrintWriter(sock.getOutputStream(), true);
        //Output
        out.println("Test");
        out.flush();

        out.close();
        sock.close();
    } catch(Exception e) {
        e.printStackTrace();
    }
    }
}

当我使用 127.0.0.1 或我的内部 IP 作为主机名时,该程序运行良好.但是每当我切换到我的外部 IP 地址时,它都会抛出一个 java.net.ConnectException: Connection denied: connect 错误.

The program works just fine when I use 127.0.0.1 or my internal IP for the hostname. But whenever I switch to my external IP address, it throws a java.net.ConnectException: Connection refused: connect error.

我特意选择了这样一个不常见的端口,看看这是否是问题所在,但运气不佳.我可以使用 telnet 毫无问题地连接,但是当我尝试使用 canyouseeme.org 访问端口时,它告诉我连接超时.我什至尝试禁用所有防火墙和防病毒软件,包括 Windows 默认防火墙和路由器防火墙,所有端口都转发并启用 DMZ,但它仍然说连接超时.我使用 Comcast 作为我的 ISP,我怀疑他们会阻止这样一个随机端口.

I purposely picked such an uncommon port to see if that was the problem, with no luck. I can connect with no problems using telnet, but when I try to access the port with canyouseeme.org, it tells me the connection timed out. I even tried to disable all firewalls and antivirus including the Windows default ones and the router firewall, with all ports forwarded and DMZ enabled, and it still says that the connection timed out. I use Comcast as my ISP, and I doubt that they block such a random port.

当我使用数据包跟踪器时,它显示了我的计算机发送 SYN 和接收 RST/ACK 的 TCP 流量,因此它看起来像一个标准的阻塞端口,并且没有其他可疑的数据包流量在进行.

When I use a packet tracer, it shows TCP traffic with my computer sending SYN and receiving RST/ACK, so it looks like a standard blocked port, and no other suspicious packet traffic was going on.

我不知道此时发生了什么;我几乎尝试了我知道的每一个技巧.如果有人知道为什么端口可能会被阻塞,或者至少知道某种使程序运行的方法,那将非常有帮助.

I have no idea what is going on at this point; I have pretty much tried every trick I know. If anyone know why the port might be blocked, or at least some way to make the program work, it would be very helpful.

推荐答案

不管怎样,你的代码在我的系统上运行良好.

For what it's worth, your code works fine on my system.

我不想这么说,但这听起来像是防火墙问题(我知道你已经三重检查了)或 Comcast 问题,这比您想象的更有可能.我会测试您的 ISP.

I hate to say it, but it sounds like a firewall issue (which I know you've already triple-checked) or a Comcast issue, which is more possible than you might think. I'd test your ISP.

这篇关于Java 网络“拒绝连接:连接"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆