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

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

问题描述

我一直试图让一个简单的网络测试程序无法运行。

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:连接被拒绝:连接错误。

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.

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

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.

我讨厌说出来,但这听起来像是防火墙问题(我知道你已经知道了)已经三重检查)或康卡斯特问题,这比你想象的更有可能。我测试你的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 Networking“拒绝连接:连接”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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