Skip to content

Instantly share code, notes, and snippets.

@luan-cestari
Last active December 23, 2015 13:59
Show Gist options
  • Save luan-cestari/6646102 to your computer and use it in GitHub Desktop.
Save luan-cestari/6646102 to your computer and use it in GitHub Desktop.
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<SelectionKey> 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