A brief description: The use of Java NIO to develop network communication is relatively fast and aspect. Because he can listen to client connections without blocking, the event-based mechanism can be used in Java NiO for non-blocking communication when something new
When registering, we only need to get new events through the event listening mechanism
Simply put, there is a Selector in Java NiO with a core object named Selector in asynchronous I/O. Selector is where you register your interest in various I/O events, and when those events occur, this is the object that tells
The events that occurred to you.
Let's first give an example of:
Package org.web.niotest;
Import java.io.IOException;
Import java.net.InetSocketAddress;
Import Java.net.ServerSocket;
Import Java.nio.ByteBuffer;
Import Java.nio.channels.SelectionKey;
Import Java.nio.channels.Selector;
Import Java.nio.channels.ServerSocketChannel;
Import Java.nio.channels.SocketChannel;
Import Java.util.Iterator;
Import Java.util.Set;
/**
* Java NIO server-side Example
* @author User
*
*/
public class Server {
Server-Side Channel
Serversocketchannel SSC;
public void Start () {
try {
The core object name of asynchronous IO selector has the effect of event listening
Selector is where you register your interest in various IO events and when those events happen, it's the object that tells you what's going on.
Selector Selector=selector.open ();
Open a Serversocketchannel Channel
Serversocketchannel Ssc=serversocketchannel.open ();
Set to asynchronous
Ssc.configureblocking (FALSE);
Binding ports
ServerSocket Ss=ssc.socket ();
Inetsocketaddress address=new inetsocketaddress (5555);
Ss.bind (address);
Registering event Regisiter The first argument is always selector the second one always op_accept here he specifies that we listen to the accept incident.
That is, when there is a new link coming in is the event that happened
Ssc.register (selector,selectionkey.op_accept);
SYSTEM.OUT.PRINTLN ("Port Registration Complete");
while (true) {
Select () This method blocks until there is a registered event that returns the number of events when one or more events are registered to come in.
Selector.select ();
Calling Selectedkeys () returns the collection of event objects
Set<selectionkey> Selectionkeys=selector.selectedkeys ();
Then we iterate over each event
Iterator<selectionkey> Iter=selectionkeys.iterator ();
Bytebuffer Echobuffer=bytebuffer.allocate (20);
Socketchannel SC;
while (Iter.hasnext ()) {
Selectionkey Key=iter.next ();
Determining Event Types
if ((Key.readyops () &selectionkey.op_accept) ==selectionkey.op_accept)
{
Serversocketchannel nssc= (Serversocketchannel) Key.channel ();
Sc=nssc.accept ();
Set to non-blocking
Sc.configureblocking (FALSE);
Sc.register (selector, selectionkey.op_read);
Iter.remove ();
System.out.println ("There is a new link" +sc);
}else if ((Key.readyops () &selectionkey.op_read) ==selectionkey.op_read) {
Sc= (Socketchannel) Key.channel ();
while (true) {
Echobuffer.clear ();
int A=sc.read (echobuffer);
if (a==-1)
Break
if (a>0) {
Byte[] B=echobuffer.array ();
SYSTEM.OUT.PRINTLN ("Receive data:" +new String (b));
Echobuffer.flip ();
Sc.write (Echobuffer);
SYSTEM.OUT.PRINTLN ("Return Data:" +new String (b));
}
}
Sc.close ();
SYSTEM.OUT.PRINTLN ("Connection End");
System.out.println ("=============================");
Iter.remove ();
}
}
}
catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
public static void Main (String args[]) {
New Server (). Start ();
}
}
Client's Code
Package org.web.niotest;
Import java.net.*;
Import java.nio.*;
Import java.nio.channels.*;
public class Client {
public void Start () {
try {
SocketAddress address = new Inetsocketaddress ("localhost", 5555);
Socketchannel client = Socketchannel.open (address);
Client.configureblocking (FALSE);
String a = "Asdasdasdasddffasfas";
Bytebuffer buffer = bytebuffer.allocate (20);
Buffer.put (A.getbytes ());
Buffer.clear ();
int d = client.write (buffer);
SYSTEM.OUT.PRINTLN ("Send data:" + New String (Buffer.array ()));
while (true) {
Buffer.flip ();
int i = client.read (buffer);
if (i > 0) {
Byte[] B = Buffer.array ();
SYSTEM.OUT.PRINTLN ("Receive data:" + new String (b));
Client.close ();
SYSTEM.OUT.PRINTLN ("Connection closed!");
Break
}
}
catch (Exception e) {
E.printstacktrace ();
}
}
public static void Main (String args[]) {
New Client (). Start ();
}
}