java.net.SocketException:无效的参数:连接

问题描述:

我的新笔记本电脑 (Alienware m17x) 在运行以下基本代码时抛出 java.net.SocketException: Invalid argument: connect:

My new laptop (Alienware m17x) throws a java.net.SocketException: Invalid argument: connect when I run the following basic code:

Server.java:

Server.java:

public static void main (String[] args) throws Exception {
        
    ServerSocket serverSocket = new ServerSocket (8888);
    Socket socket = serverSocket.accept();
}

Client.java:

Client.java:

public static void main (String[] args) throws Exception {
    Socket socket = new Socket ("localhost", 8888);
}

每次我运行 Client.java(在启动 Server.java 之后)我都会收到这个套接字异常.这是异常的完整跟踪:

Every time I run Client.java (after starting Server.java) I get this socket exception. Here's the full trace of the exception:

Exception in thread "main" java.net.SocketException: Invalid argument: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at Client.main(Client.java:5)

我已经尝试了一些方法,但不幸的是总是抛出异常.这是我得出的结论:

I've tried a few things, but no luck the exception is always thrown. Here's what I have concluded:

  • 更改端口号没有任何改变.
  • 这不是网络问题.我在学校和家里都有例外.
  • 我已经重新安装了 JVM 和 JDK,但没有成功.
  • 这只发生在一台机器上.我在桌面上运行了相同的代码,但没有出现任何异常.我在两者上都使用 Windows 7.
  • 它没有被防火墙阻止.我已关闭防火墙,但仍然存在此问题.

我应该怎么做或检查以解决此问题?

What should I do or check for to resolve this issue?

路由打印输出:

===========================================================================
Interface List
 15...e4 d5 3d 08 cb 83 ......Killer Wireless-N 1103 Network Adapter
 13...d0 df 9a b5 73 dc ......Bluetooth Device (Personal Area Network)
 11...d4 be d9 00 10 65 ......Atheros AR8151 PCI-E Gigabit Ethernet Controller (NDIS 6.20)
  1...........................Software Loopback Interface 1
 17...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface
 16...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #2
 18...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter
 19...00 00 00 00 00 00 00 e0 Microsoft ISATAP Adapter #3
===========================================================================

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0      192.168.0.1    192.168.0.191     25
        127.0.0.0        255.0.0.0         On-link         127.0.0.1    306
        127.0.0.1  255.255.255.255         On-link         127.0.0.1    306
  127.255.255.255  255.255.255.255         On-link         127.0.0.1    306
      192.168.0.0    255.255.255.0         On-link     192.168.0.191    281
    192.168.0.191  255.255.255.255         On-link     192.168.0.191    281
    192.168.0.255  255.255.255.255         On-link     192.168.0.191    281
        224.0.0.0        240.0.0.0         On-link         127.0.0.1    306
        224.0.0.0        240.0.0.0         On-link     192.168.0.191    281
  255.255.255.255  255.255.255.255         On-link         127.0.0.1    306
  255.255.255.255  255.255.255.255         On-link     192.168.0.191    281
===========================================================================
Persistent Routes:
  None

IPv6 Route Table
===========================================================================
Active Routes:
 If Metric Network Destination      Gateway
 17     58 ::/0                     On-link
  1    306 ::1/128                  On-link
 17     58 2001::/32                On-link
 17    306 2001:0:4137:9e76:28f3:2721:524f:e2ef/128
                                    On-link
 15    281 fe80::/64                On-link
 17    306 fe80::/64                On-link
 17    306 fe80::28f3:2721:524f:e2ef/128
                                    On-link
 15    281 fe80::68c1:bc79:fefa:88a2/128
                                    On-link
  1    306 ff00::/8                 On-link
 17    306 ff00::/8                 On-link
 15    281 ff00::/8                 On-link
===========================================================================
Persistent Routes:
  None

正如@PhilippeLM 和@beny23 让我意识到的那样,将 java 系统变量 java.net.preferIPv4Stack 设置为 true 解决了我的问题.但是我想要一个永久的修复.我不想每次运行java应用程序时都指定系统变量.

As @PhilippeLM and @beny23 made me realize, setting the java system variable java.net.preferIPv4Stack to true solves my problem. However I want a permanent fix. I don't want to specify the system variable every time I run a java application.

这是我再次尝试但没有成功的方法:

Here's what I've tried with no luck once again:

  • 按照这些步骤
  • 将我的机器首选项设置为首选 IPv4
  • java.net.preferIPv4Stack=true 行添加到 %JAVA_HOME%jrelib 中的 net.properties 文件中.
  • Setting my machine preferences to prefer IPv4 by following these steps
  • Adding the line java.net.preferIPv4Stack=true to the net.properties file in %JAVA_HOME%jrelib.

还有什么我可以尝试的吗?

Is there anything else I can try?

我有一个 Alienware m17x R3,我发现我在使用 Java 时遇到了完全相同的错误,尽管我在使用 Minecraft 时遇到了它.安装较旧的 Java6u33 解决了该问题,但我发现即使等待几个月后,Java7 的较新更新仍然无法正常工作.因此,我最终访问了戴尔网站并为我的 Alienware 笔记本电脑下载了最新的网络驱动程序(因为 Alienware 现在归戴尔所有)并立即解决了问题.

I have an Alienware m17x R3 and I found that I had the exact same error with Java, though I encountered it with Minecraft. Installing older Java6u33 fixed the issue but I found that newer updates of Java7 still weren't working even after waiting several months. So I eventually went to the Dell website and downloaded the most recent network drivers for my Alienware laptop (since Alienware is now owned by Dell) and that fixed the problem right away.