1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
| import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.util.Iterator;
public final class ChannelManager
{
Selector selector = null;
ServerSocketChannel sChannel1;
ServerSocketChannel sChannel2;
public ChannelManager() throws IOException
{
try
{
// Create the selector
selector = Selector.open();
// Create two non-blocking sockets. This method is implemented in
// Creating a Non-Blocking Socket.
sChannel1 = createServerSocketChannel("127.0.0.1", 31110);
sChannel2 = createServerSocketChannel("127.0.0.1", 32110);
// Register the channel with selector, listening for all events
sChannel1.register(selector, SelectionKey.OP_ACCEPT);
sChannel2.register(selector, SelectionKey.OP_ACCEPT);
}
catch (IOException e)
{
System.out.println("I'll be crazy "+e);
}
// Wait for events
while (selector.select() > 0)
{
// Get list of selection keys with pending events
Iterator it = selector.selectedKeys().iterator();
// Process each key at a time
while (it.hasNext())
{
// Get the selection key
SelectionKey selKey = (SelectionKey) it.next();
// Remove it from the list to indicate that it is being processed
it.remove();
try
{
processSelectionKey(selKey);
}
catch (IOException e)
{
System.out.println("exception" + e);
selKey.cancel();
}
}
}
System.out.println("END");
}
public ServerSocketChannel createServerSocketChannel(String hostName, int port) throws IOException
{
// Create a non-blocking server socket channel
InetSocketAddress address = new InetSocketAddress(hostName, port);
ServerSocketChannel sChannel = ServerSocketChannel.open();
sChannel.configureBlocking(false);
sChannel.socket().bind(address);
System.out.println("Channel from: " + hostName + " port: " + port + " isOpen :"+ sChannel.isOpen());
return sChannel;
}
public void processSelectionKey(SelectionKey selKey) throws IOException
{
// Since the ready operations are cumulative,
// need to check readiness for each operation
if (selKey.isValid() && selKey.isConnectable())
{
// Get channel with connection request
SocketChannel sChannel = (SocketChannel) selKey.channel();
System.out.println("valid et connectable:" +sChannel.toString());
boolean success = sChannel.finishConnect();
if (!success)
{
// An error occurred; handle it
// Unregister the channel with this selector
selKey.cancel();
}
}
if (selKey.isValid() && selKey.isReadable())
{
// Get channel with bytes to read
SocketChannel sChannel = (SocketChannel) selKey.channel();
if (sChannel.equals(sChannel1))
{
System.out.println(" channel 1 state : " + sChannel1.isOpen());
}
else if (sChannel.equals(sChannel2))
{
System.out.println("channel 2 state : " + sChannel2.isOpen());
}
else
{
System.out.println("ni 1 ni 2");
}
}
if (selKey.isValid() && selKey.isWritable())
{
// Get channel that's ready for more bytes
SocketChannel sChannel = (SocketChannel) selKey.channel();
// See Writing to a SocketChannel
}
}
public static void main(String[] args) throws IOException
{
ChannelManager channel = new ChannelManager();
}
} |
Partager