Reputation: 585
My new laptop (Alienware m17x) throws a java.net.SocketException: Invalid argument: connect
when I run the following basic code:
Server.java:
public static void main (String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket (8888);
Socket socket = serverSocket.accept();
}
Client.java:
public static void main (String[] args) throws Exception {
Socket socket = new Socket ("localhost", 8888);
}
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:
What should I do or check for to resolve this issue?
EDIT: route print outputs:
===========================================================================
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
EDIT: 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:
java.net.preferIPv4Stack=true
to the net.properties
file in %JAVA_HOME%\jre\lib
.Is there anything else I can try?
Upvotes: 16
Views: 58179
Reputation: 1
Please check if you installed JDK in the network location. if yes, please change the jdk location in the local drive disk and confirm there is no limit about permission
Upvotes: 0
Reputation: 2385
There is a Windows bug that affects not only Java applications and produces exactly this error in certain constellations where the JRE is located on a network share: https://bugs.openjdk.java.net/browse/JDK-8068568
Solution is either using a JRE installation on a local drive or granting the List Folder permission on all ancestor folders of the JRE exe.
In https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/3076a9cd-57a0-418d-8de1-07adc3b486bb it is suggested to add a DWORD value HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\FltMgr\UseTildeShortcut with content "1", which I didn't have a chance to try. I think I also remember reading a post on a Microsoft support forum which stated that this doesn't happen on network shares whose name is longer than 8 characters, but I can't find the post anymore.
Upvotes: 3
Reputation: 767
Also check if your IDE is using the JRE which you expect.
I had same issue:
java.sql.SQLException: JZ006: Caught IOException: java.net.SocketException: Invalid argument: create
When I checked my run configuration, my default JRE was pointing to 'Project Execution Environment 'JavaSE1.7' which was not I was expecting.
I changed it to option Alternate JRE and set the JAVA 8 as installed jre. which has solved my problem.
Upvotes: 1
Reputation: 66
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.
Upvotes: 5
Reputation: 1543
I have figured out a way to make this a permanent change from here.
What you need to do is add a system variable named JAVA_TOOL_OPTIONS and set this to whatever you want to be passed to java as a commandline argument whenever it starts up, in yours and my case being
-Djava.net.preferIPv4Stack=true
Upvotes: 5
Reputation: 183
Your machine seems to have an IPv6 configuration and is preferred by default in Java so try start your server and client with -Djava.net.preferIPv4Stack=true as JVM args:
java -Djava.net.preferIPv4Stack=true Client.main
Also make sure you didn't deny the application network access when Windows' Firewall asked you for permission. If you did, you can change the settings in the control panel.
Upvotes: 5
Reputation: 35048
Does it work if you tell Java to use the IPv4 stack?
Use the following command line option when starting the server and client.
-Djava.net.preferIPv4Stack=true
See also here
Upvotes: 7
Reputation: 21449
Can you run other applications that connect to the network or localhost?
I have had a similar problem a while ago (only on Win7) and I fixed it by only going to Network connection-> Repain
. The problem came from the fact that my routing table was corrupted, the localhost
route disappeared from it and any application trying to connect to localhost
was crashing with the same error as yours.
Can you run route -print
in a command line and post the output?
EDIT: Sounds like your IP stack is corrupted, you might try this fix from MS
Upvotes: 0