14

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:

  • Changing the port numbers changes nothing.
  • It's not a problem with the network. I have gotten the exception at school and at home.
  • I've reinstalled the JVM and the JDK but no luck.
  • This only occurs on one machine. I have run the same code on my desktop and I have not gotten any exceptions. I am using Windows 7 on both.
  • It is not blocked by a firewall. I have turned my firewalls off and I still have this issue.

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:

  • 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%\jre\lib.

Is there anything else I can try?

Community
  • 1
  • 1
abourg28
  • 565
  • 2
  • 5
  • 10

8 Answers8

6

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

beny23
  • 32,077
  • 3
  • 78
  • 83
5

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.

David
  • 66
  • 1
  • 1
4

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.

PhilippeLM
  • 163
  • 1
  • 1
  • 9
  • It works! I do not get the error when I use the option `-Djava.net.preferIPv4Stack=true`. Is there a way I can change my machine's configuration to prefer IPv4 by default? – abourg28 Dec 03 '11 at 17:43
  • It's not really a machine preference; the spec says it should be that way. I find that Java is a bit brain dead when IPv6 is concerned (or perhaps only the libraries I used) everything should fallback to IPv4 but you'll need to do it yourself in this case (hint: **connect** and **bind** to 127.0.0.1, not "localhost"). It does mean your machine is not configured correctly for IPv6, however. – PhilippeLM Dec 05 '11 at 01:50
  • Ok thank you. It is a bit cumbersome to fallback myself. Would there be a way to reconfigure IPv6 correctly? Should I contact my vendor? – abourg28 Dec 05 '11 at 16:19
3

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.

Bernhard Stadler
  • 916
  • 8
  • 19
  • Thanks, this fixed my problem trying to execute an HTTP request with JMeter - I had installed my JRE on my desktop, which was on a network share and not local to my machine! – TheTinyMan Aug 16 '18 at 15:09
1

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.

Techflash
  • 501
  • 6
  • 10
1

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

Community
  • 1
  • 1
Matt Habel
  • 1,353
  • 2
  • 13
  • 34
0

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

0

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

GETah
  • 19,549
  • 6
  • 51
  • 95
  • By `Network Connection -> Repain` do you mean repair my network connection? I have done that and I still have the problem. I have added the output to `route print` to my post. – abourg28 Nov 21 '11 at 19:26
  • The localhost loopback seems to be there. Did you try to bind to 192.168.0.191 instead? – GETah Nov 21 '11 at 20:35
  • I still have the exception with 192.168.0.191 – abourg28 Nov 21 '11 at 21:50
  • Just edited my answer, check out the edit and let me know the results – GETah Nov 21 '11 at 22:02
  • I have tried the fix with no luck. I still get the exception. Does it help to know I am able to telnet to the server? – abourg28 Nov 21 '11 at 23:11
  • Yes sure, any application that can connect to your server will be of good help. Also, have you tried to connect to a different port on the server? – GETah Nov 21 '11 at 23:14
  • Also, which Java RT are you running? – GETah Nov 21 '11 at 23:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5215/discussion-between-abourg28-and-getah) – abourg28 Nov 21 '11 at 23:32