java.net.SocketException: Invalid argument: connect
当我运行以下基本代码时,我的新笔记本电脑 (Alienware m17x) 会抛出错误:
服务器.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);
}
每次我运行 Client.java(在启动 Server.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=true
到.net.properties
%JAVA_HOME%\jre\lib
还有什么我可以尝试的吗?