Non-blocking TCP time server, Client Data Reading thread, main thread method only processes Client Connection

Source: Internet
Author: User

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 ){

}
}

}

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.