I have the java app with a custom security policy and when I tried to open 100 udp sockets I've got exception:
java.net.SocketException: maximum number of DatagramSockets reached
Test app:
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.util.ArrayList;
import java.util.List;
public class Test {
static int basePortNum = 40000;
public static void main(String[] args) {
List<DatagramSocket> socks = new ArrayList<DatagramSocket>();
try{
for(int i = 0; i<100; i++){
socks.add(new DatagramSocket(basePortNum+i, Inet4Address.getByName("127.0.0.1")) );
System.out.println(i);
}
}catch(Exception e){
e.printStackTrace();
}finally{
for(java.net.DatagramSocket soc: socks){
soc.close();
}
}
}
}
If I launch it without security manager it creates all 100 sockets:
0
1
...
98
99
But if I add this line to VM arguments
-Djava.security.manager
It fails even with default security policy:
0
1
...
23
24
java.net.SocketException: maximum number of DatagramSockets reached
at sun.net.ResourceManager.beforeUdpCreate(ResourceManager.java:53)
at java.net.PlainDatagramSocketImpl.create(PlainDatagramSocketImpl.java:74)
at java.net.DatagramSocket.createImpl(DatagramSocket.java:318)
at java.net.DatagramSocket.<init>(DatagramSocket.java:209)
at java.net.DatagramSocket.<init>(DatagramSocket.java:262)
at Test.main(Test.java:16)
How can I change socket limit while using security manager?
UPD: it was reproduced on windows 8.1 x64, windows 7 x64 and windows xp sp3 x32, windows 7 x32. On all systems JDK 1.6.45 x32 was used.
UPD2: on windows xp sp3 x32 reproduced with JDK7 too, but can't reproduce this with JDK 1.6.27.
UPD3: debugging JDK classes found this:
// Compiled from ResourceManager.java (version 1.5 : 49.0, super bit)
public class sun.net.ResourceManager {
// Field descriptor #14 I
private static final int DEFAULT_MAX_SOCKETS = 25;
In open JDK implementation the default socket limit is 1024 http://www.docjar.com/html/api/sun/net/ResourceManager.java.html
That VM argument helped:
-Dsun.net.maxDatagramSockets=101