Echoserver.java (non-blocking mode)

Source: Internet
Author: User

Package nonblock;
Import java.io.*;
Import java.nio.*;
Import java.nio.channels.*;
Import java.nio.charset.*;
Import java.net.*;
Import java.util.*;

public class echoserver{
Private Selector Selector = null;
Private Serversocketchannel serversocketchannel = null;
private int port = 8000;
Private Charset Charset=charset.forname ("GBK");

Public Echoserver () throws ioexception{
selector = Selector.open ();
Serversocketchannel= Serversocketchannel.open ();
Serversocketchannel.socket (). Setreuseaddress (True);
Serversocketchannel.configureblocking (FALSE);
Serversocketchannel.socket (). bind (New Inetsocketaddress (port));
SYSTEM.OUT.PRINTLN ("server Start");
}

public void Service () throws ioexception{
Serversocketchannel.register (selector, selectionkey.op_accept);
while (Selector.select () > 0) {
Set Readykeys = Selector.selectedkeys ();
Iterator it = Readykeys.iterator ();
while (It.hasnext ()) {
Selectionkey Key=null;
try{
Key = (Selectionkey) it.next ();
It.remove ();

if (key.isacceptable ()) {
Serversocketchannel SSC = (serversocketchannel) key.channel ();
Socketchannel Socketchannel = (socketchannel) ssc.accept ();
System.out.println ("Received client connection, from:" +
Socketchannel.socket (). Getinetaddress () +
":" + socketchannel.socket (). Getport ());
Socketchannel.configureblocking (FALSE);
Bytebuffer buffer = bytebuffer.allocate (1024);
Socketchannel.register (Selector,
Selectionkey.op_read |
Selectionkey.op_write, buffer);
}
if (key.isreadable ()) {
Receive (key);
}
if (key.iswritable ()) {
Send (key);
}
}catch (IOException e) {
E.printstacktrace ();
try{
if (key!=null) {
Key.cancel ();
Key.channel (). Close ();
}
}catch (Exception ex) {e.printstacktrace ();}
}
}//#while
}//#while
}

public void Send (Selectionkey key) throws ioexception{
Bytebuffer buffer= (Bytebuffer) key.attachment ();
Socketchannel socketchannel= (Socketchannel) Key.channel ();
Buffer.flip (); Set the limit to position and set the position to 0
String data=decode (buffer);
if (Data.indexof ("\ r \ n") ==-1) return;
String outputdata=data.substring (0,data.indexof ("\ n") +1);
System.out.print (Outputdata);
Bytebuffer Outputbuffer=encode ("Echo:" +outputdata);
while (Outputbuffer.hasremaining ())
Socketchannel.write (OutputBuffer);

Bytebuffer Temp=encode (Outputdata);
Buffer.position (Temp.limit ());
Buffer.compact ();

if (Outputdata.equals ("bye\r\n")) {
Key.cancel ();
Socketchannel.close ();
System.out.println ("Close the connection with the customer");
}
}

public void receive (Selectionkey key) throws ioexception{
Bytebuffer buffer= (Bytebuffer) key.attachment ();

Socketchannel socketchannel= (Socketchannel) Key.channel ();
Bytebuffer readbuff= Bytebuffer.allocate (32);
Socketchannel.read (Readbuff);
Readbuff.flip ();

Buffer.limit (Buffer.capacity ());
Buffer.put (Readbuff);
}

Public String decode (Bytebuffer buffer) {//decode
Charbuffer charbuffer= charset.decode (buffer);
return charbuffer.tostring ();
}
Public Bytebuffer encode (String str) {//encoding
return Charset.encode (str);
}

public static void Main (String args[]) throws exception{
Echoserver Server = new Echoserver ();
Server.service ();
}
}





/****************************************************
* Sun Weichen *
* Source: <<java Network programming Fine >> *
* Technical Support website: www.javathinker.org *
***************************************************/

Echoserver.java (non-blocking mode)

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.