Java New IO "_selector

Source: Internet
Author: User

Dateserver.java


Import java.net.InetSocketAddress;
Import Java.net.ServerSocket;
Import Java.util.Set;
Import Java.util.Iterator;
Import Java.util.Date;
Import Java.nio.channels.ServerSocketChannel;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.SocketChannel;
Import Java.nio.channels.Selector;
Import Java.nio.channels.SelectionKey;
public class dateserver{
public static void Main (String args[]) throws Exception {
int ports[] = {8000,8001,8002,8003,8005,8006}; Represents five listening ports
Selector Selector = Selector.open (); Find selector through the open () method
for (int i=0;i<ports.length;i++) {
Serversocketchannel initser = null;
Initser = Serversocketchannel.open (); Open the channel for the server
Initser.configureblocking (FALSE); Server configured for non-blocking
ServerSocket Initsock = Initser.socket ();
Inetsocketaddress address = null;
Address = new Inetsocketaddress (ports[i]); Instantiate a binding address
Initsock.bind (address); Binding for service
Initser.register (selector,selectionkey.op_accept); Waiting for connection
SYSTEM.OUT.PRINTLN ("server running," on "+ ports[i] +" Port listening. ") ;
}
To receive all generated keys and to determine whether to get the client's output through the connection
int keysadd = 0;
while ((Keysadd=selector.select ()) >0) {//Select a set of keys and the corresponding channel is ready
set<selectionkey> Selectedkeys = Selector.selectedkeys ();//Remove All generated keys
Iterator<selectionkey> iter = Selectedkeys.iterator ();
while (Iter.hasnext ()) {
Selectionkey key = Iter.next (); Remove each key
if (key.isacceptable ()) {
Serversocketchannel Server = (Serversocketchannel) key.channel ();
Socketchannel client = Server.accept (); Receive new connections
Client.configureblocking (false);//configured for non-blocking
Bytebuffer outbuf = bytebuffer.allocatedirect (1024); //
Outbuf.put ("Current time is:" + new Date ()). GetBytes ()); Set content to Buffer
Outbuf.flip ();
Client.write (OUTBUF); Output content
Client.close (); Shut down
}
}
Selectedkeys.clear (); Clear all the keys
}

}
}

Java New IO "_selector

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.