Created
September 21, 2013 01:25
-
-
Save luan-cestari/6646082 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.net.InetSocketAddress; | |
import java.net.StandardSocketOptions; | |
import java.nio.channels.SelectionKey; | |
import java.nio.channels.Selector; | |
import java.nio.channels.SocketChannel; | |
import java.util.Iterator; | |
/* @author Norman Maurer */ | |
public class BugReproducerClient | |
{ | |
private static final int MAX_CONNECTIONS = 40 * 1024; | |
public static void main(String args[]) throws Exception | |
{ | |
InetSocketAddress address = new InetSocketAddress(args[0], Integer.parseInt(args[1])); | |
int connections = 0; | |
Selector selector = Selector.open(); | |
for (;;) | |
{ | |
SocketChannel channel = SocketChannel.open(); | |
channel.configureBlocking(false); | |
channel.setOption(StandardSocketOptions.SO_KEEPALIVE, true); | |
channel.setOption(StandardSocketOptions.SO_REUSEADDR, true); | |
if (!channel.connect(address)) | |
{ | |
channel.register(selector, SelectionKey.OP_CONNECT); | |
selector.select(); | |
Iterator keys = selector.selectedKeys().iterator(); | |
while (keys.hasNext()) | |
{ | |
SelectionKey key = keys.next(); | |
keys.remove(); | |
if (key.isConnectable()) | |
{ | |
key.interestOps((key.interestOps() &~SelectionKey.OP_CONNECT) | SelectionKey.OP_READ); | |
System.out.println(++connections); | |
} | |
} | |
} | |
else | |
{ | |
channel.register(selector, SelectionKey.OP_READ); | |
System.out.println(++connections); | |
} | |
if (MAX_CONNECTIONS == connections) | |
{ | |
Thread.sleep(1000 * 60); | |
break; | |
} | |
else | |
{ | |
if (connections % 500 == 0) | |
{ | |
Thread.sleep(100); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment