Import java.net. inetsocketaddress;
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;
Import com. tz. uitl. Common;
/**
* Non-blocking TCP Time Server
*
* @ Author ljz
*
*/
Public class niotcptimeserver extends thread {
Private int listenports = 8485;
Private serversocketchannel serversocket = NULL;
Private selector acceptselector = NULL;
Private selector readselector = NULL;
Public niotcptimeserver (INT p ){
Listenport = P;
Try {
Acceptselector = selector. open ();
Readselector = selector. open ();
Serversocket = serversocketchannel. open ();
Serversocket. configureblocking (false );
Serversocket. socket (). BIND (New inetsocketaddress (listenport ));
Serversocket. Register (acceptselector, selectionkey. op_accept );
System. Out. println ("the server is started successfully. The listening port [" + listenport + "]");
New readthread (). Start ();
} Catch (exception e ){
System. Out. println ("failed to start the server! ");
E. printstacktrace ();
}
}
/**
* The main thread method only processes client connections <br>
* Register the Connection Client Event Response as a read response <br>
*/
Public void run (){
While (true ){
Try {
If (acceptselector. Select (100)> 0 ){
Set keys = acceptselector. selectedkeys ();
For (iterator I = keys. iterator (); I. hasnext ();){
Selectionkey key = (selectionkey) I. Next ();
I. Remove ();
Serversocketchannel readychannel = (serversocketchannel) Key
. Channel ();
Socketchannel incomingchannel = readychannel. Accept ();
System. Out. println ("client ["
+ Incomingchannel. socket ()
. Getremotesocketaddress ()
+ "] Establish a connection with the server .");
Incomingchannel. configureblocking (false );
Incomingchannel. Register (readselector,
Selectionkey. op_read, new stringbuffer ());
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
}
/**
* Client Data Reading thread
*
* @ Author ljz
*
*/
Private class readthread extends thread {
Public void run (){
Int keysready =-1;
While (true ){
Try {
/**
* Check the customer data event. The timeout value is 1 second.
*/
Keysready = readselector. Select (100 );
If (keysready> 0 ){
Set readykeys = readselector. selectedkeys ();
For (iterator I = readykeys. iterator (); I. hasnext ();){
Selectionkey key = (selectionkey) I. Next ();
Socketchannel incomingchannel = (socketchannel) Key
. Channel ();
Readdata (incomingchannel );
I. Remove ();
}
}
} Catch (exception e ){
E. printstacktrace ();
}
}
}
Private void readdata (socketchannel channel ){
Try {
Bytebuffer readbuf = bytebuffer. Allocate (1024 );
If (Channel. Read (readbuf)> 0 ){
Readbuf. Flip ();
System. Out. println ("receive client ["
+ Channel. socket (). getremotesocketaddress ()
+ "] DATA [" + common. Decode (readbuf) + "].");
}
} Catch (exception e ){
Try {
Channel. Close ();
System. Out. println ("close a terminal .");
} Catch (exception ex ){
}
}
}
}
/**
* @ Param ARGs
*/
Public static void main (string [] ARGs ){
Try {
New niotcptimeserver (8485). Start ();
} Catch (exception e ){
}
}
}