java.net.SocketException: Invalid argument: connect

2022-09-03 13:36:07

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

服务器.java:

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

客户.java:

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

每次我运行客户端.java(启动服务器.java后)我都会收到此套接字异常。下面是异常的完整跟踪:

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)

我已经尝试了一些事情,但没有运气,这个例外总是抛出。以下是我的结论:

  • 更改端口号不会更改任何内容。
  • 这不是网络的问题。我在学校和家里都有例外。
  • 我已经重新安装了JVM和JDK,但没有运气。
  • 这只发生在一台计算机上。我已经在桌面上运行了相同的代码,并且没有任何异常。我在两者都使用Windows 7。
  • 它未被防火墙阻止。我已关闭防火墙,但仍有此问题。

我应该做什么或检查来解决此问题?

编辑:路线打印输出:

===========================================================================
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应用程序时都指定系统变量。

以下是我再次尝试而没有运气的东西:

  • 按照以下步骤将我的计算机首选项设置为首选 IPv4
  • 将该行添加到 文件中。java.net.preferIPv4Stack=truenet.properties%JAVA_HOME%\jre\lib

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


答案 1

如果您告诉Java使用IPv4堆栈,它是否有效?

启动服务器和客户端时,请使用以下命令行选项。

-Djava.net.preferIPv4Stack=true

另请参阅此处


答案 2

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


推荐