Skip to content

Instantly share code, notes, and snippets.

@luan-cestari
Last active December 23, 2015 13:59
Show Gist options
  • Save luan-cestari/6646095 to your computer and use it in GitHub Desktop.
Save luan-cestari/6646095 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.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
/*@author Norman Maurer */
public class BugReproducer
{
public static void main(String args[]) throws Exception
{
int connections = 0;
Selector selector = Selector.open();
final ServerSocketChannel channel = ServerSocketChannel.open();
channel.configureBlocking(false);
channel.register(selector, SelectionKey.OP_ACCEPT, channel);
channel.bind(new InetSocketAddress("0.0.0.0", Integer.parseInt(args[0])));
for (;;)
{
selector.select();
Iterator<SelectionKey> keys = selector.selectedKeys().iterator();
while (keys.hasNext())
{
SelectionKey key = keys.next();
keys.remove();
if (key.isAcceptable())
{
ServerSocketChannel ch = (ServerSocketChannel) key.attachment();
for (;;)
{
SocketChannel sch = ch.accept();
if (sch == null)
{
break;
}
sch.setOption(StandardSocketOptions.SO_KEEPALIVE, true);
sch.setOption(StandardSocketOptions.SO_REUSEADDR, true);
sch.configureBlocking(false);
sch.register(selector, SelectionKey.OP_READ);
System.out.println(++connections);
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment