Java 网络“连接被拒绝:连接”
我一直在尝试运行一个简单的网络测试程序,但没有结果。
服务器:
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 returned: connect
错误。
我特意选择了一个不常见的端口来看看是否是问题所在,但没有成功。 我可以使用 telnet 毫无问题地进行连接,但是当我尝试使用 canyouseeme.org 访问该端口时,它告诉我连接超时。 我什至尝试禁用所有防火墙和防病毒软件,包括 Windows 默认防火墙和路由器防火墙,并转发所有端口并启用 DMZ,但它仍然显示连接超时。我使用 Comcast 作为我的 ISP,我怀疑他们是否会阻止这样的随机端口。
当我使用数据包跟踪器时,它显示我的计算机发送 SYN 并接收 RST/ACK 的 TCP 流量,因此它看起来像一个标准的阻塞端口,并且没有其他可疑数据包流量发生。
我不知道此时发生了什么;我几乎尝试了我所知道的所有技巧。如果有人知道为什么端口可能被阻止,或者至少知道某种使程序运行的方法,那将会非常有帮助。
I have been trying to get a simple networking test program to run with no results.
Server:
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();
}
}
}
}
Client:
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();
}
}
}
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.
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这些问题出现在以下情况下:
客户端和服务器,其中一个或两个不在网络中。
服务器未运行。
服务器正在运行,但未侦听端口,客户端正在尝试连接。
主机-端口组合不允许使用防火墙。
主机端口组合不正确。
连接字符串中的协议不正确。
如何解决问题:
首先,您 ping 目标服务器。如果 ping 正常,
那么客户端和服务器都在网络中。
尝试使用 telnet 连接到服务器主机和端口。如果你是
能够连接,那么您在客户端代码中犯了一些错误。
These problem comes under the following situations:
Client and Server, either or both of them are not in network.
Server is not running.
Server is running but not listening on port, client is trying to connect.
Firewall is not permitted for host-port combination.
Host Port combination is incorrect.
Incorrect protocol in Connecting String.
How to solve the problem:
First you ping destination server. If that is pinging properly,
then the client and server are both in network.
Try connected to server host and port using telnet. If you are
able to connect with it, then you're making some mistakes in the client code.
无论如何,您的代码在我的系统上运行良好。
我不想这么说,但这听起来像是防火墙问题(我知道您已经进行了三次检查)或 康卡斯特问题,这比您想象的更有可能。我会测试您的 ISP。
For what it's worth, your code works fine on my system.
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.
服务器套接字可能仅绑定到本地主机地址。您可以使用构造函数的 3 参数形式将其绑定到特定 IP 地址。
Likely the server socket is only being bound to the localhost address. You can bind it to a specific IP address using the 3-argument form of the constructor.
我假设您正在使用路由器连接到互联网。您应该进行端口转发以让公众访问您的内部网络。看看 如何让 Java 套接字与公共 IP?
我还写了一篇关于端口转发的博客文章,您可能想看看:) http://happycoders.wordpress.com/2010/10/03/how-to-setup-a-web-server-by -yourself/
但我仍然无法通过公共 IP 访问它,现在正在努力......
I assume you are using a Router to connect to Internet. You should do Port Forwarding to let public access your internal network. Have a look at How do you get Java sockets working with public IPs?
I have also written a blog post about Port forwarding, you might wanna have a look :) http://happycoders.wordpress.com/2010/10/03/how-to-setup-a-web-server-by-yourself/
But I still couldn't get this accessed over public IP, working on it now...
我遇到了同样的问题,因为有时客户端在服务器之前启动,当他尝试建立连接时,它找不到正在运行的服务器。
我的第一个(不太优雅)解决方案是使用 sleep 方法让客户端停止一段时间:
我在客户端连接之前使用此代码,在您的示例中,就在 Socket sock = new Socket(HOSTNAME, PORT_NUMBER) 之前;
我的第二个解决方案基于这个 答案。基本上我在客户端类中创建了一个方法,该方法尝试连接到服务器,如果连接失败,它会等待两秒钟再重试。
这是我的方法:
正如您所看到的,该方法尝试创建套接字十次,然后返回套接字的空值,因此请小心并检查结果。
你的代码应该变成:
这个解决方案帮助了我,我希望它也能帮助你。 ;-)
I had the same problem because sometimes the client started before server and, when he tried to set up the connection, it couldn't find a running server.
My first (not so elegant) solution was to stop the client for a while using the sleep method:
I use this code just before the client connection, in your example, just before
Socket sock = new Socket(HOSTNAME, PORT_NUMBER);
My second solution was based on this answer. Basically I created a method in the client class, this method tries to connect to the server and, if the connection fails, it waits two seconds before retry.
This is my method:
As you can see this method tries to create a socket for ten times, then returns a null value for socket, so be carefull and check the result.
Your code should become:
This solution helped me, I hope it helps you as well. ;-)