????2?? ??????server??????
????server????
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;
/**
* SumServer.java
*
*
* Created: Thu Nov 06 11:41:52 2003
*
* @author starchu1981
* @version 1.0
*/
public class SumServer {
private ByteBuffer _buffer=ByteBuffer.allocate(8);
private IntBuffer _intBuffer=_buffer.asIntBuffer();
private SocketChannel _clientChannel=null;
private ServerSocketChannel _serverChannel=null;
public void start(){
try{
openChannel();
waitForConnection();
}catch(IOException e){
System.err.println(e.toString());
}
}
private void openChannel()throws IOException{
_serverChannel=ServerSocketChannel.open();
_serverChannel.socket().bind(new InetSocketAddress(10000));
System.out.println("server????????");
}
private void waitForConnection()throws IOException{
while(true){
_clientChannel=_serverChannel.accept();
if(_clientChannel!=null){
System.out.println("?μ?????????");
processRequest();
_clientChannel.close();
}
}
}
private void processRequest()throws IOException{
_buffer.clear();
_clientChannel.read(_buffer);
int result=_intBuffer.get(0)+_intBuffer.get(1);
_buffer.flip();
_buffer.clear();
_intBuffer.put(0??result);
_clientChannel.write(_buffer);
}
public static void main(String [] args){
new SumServer().start();
}
} // SumServer
???????????
package examples.nio;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.channels.SocketChannel;
import java.net.InetSocketAddress;
import java.io.IOException;
/**
* SumClient.java
*
*
* Created: Thu Nov 06 11:26:06 2003
*
* @author starchu1981
* @version 1.0
*/
public class SumClient {
private ByteBuffer _buffer=ByteBuffer.allocate(8);
private IntBuffer _intBuffer;
private SocketChannel _channel;
public SumClient() {
_intBuffer=_buffer.asIntBuffer();
} // SumClient constructor
public int getSum(int first??int second){
int result=0;
try{
_channel=connect();
sendSumRequest(first??second);
result=receiveResponse();
}catch(IOException e){System.err.println(e.toString());
}finally{
if(_channel!=null){
try{
_channel.close();
}catch(IOException e){}
}
}
return result;
}
private SocketChannel connect()throws IOException{
InetSocketAddress socketAddress=
new InetSocketAddress("localhost"??10000);
return SocketChannel.open(socketAddress);
}
private void sendSumRequest(int first??int second)throws IOException{
_buffer.clear();
_intBuffer.put(0??first);
_intBuffer.put(1??second);
_channel.write(_buffer);
System.out.println("?????????? "+first+"+"+second);
}
private int receiveResponse()throws IOException{
_buffer.clear();
_channel.read(_buffer);
return _intBuffer.get(0);
}
public static void main(String [] args){
SumClient sumClient=new SumClient();
System.out.println("??????? :"+sumClient.getSum(100??324));
}
} // SumClient
????3?? ?????????server
??????????openChannel?????????????
????_serverChannel.configureBlocking(false);//???ó?????????
??????дWaitForConnection????????????????£???÷???????
private void waitForConnection()throws IOException{
Selector acceptSelector = SelectorProvider.provider().openSelector();
/*??server?????????selector???????????accept??????????
?????Selector????????????accept???????????????ready??
?????????????????I/O??????*/
SelectionKey acceptKey = ssc.register(acceptSelector??
SelectionKey.OP_ACCEPT);
int keysAdded = 0;
/*select????????????????????????????????*/
while ((keysAdded = acceptSelector.select()) > 0) {
// ???????????????????I/O????????????ready??????
Set readyKeys = acceptSelector.selectedKeys();
Iterator i = readyKeys.iterator();
// ????ready???????????????????
while (i.hasNext()) {
SelectionKey sk = (SelectionKey)i.next();
i.remove();
ServerSocketChannel nextReady =
(ServerSocketChannel)sk.channel();
// ????????????????
_clientSocket = nextReady.accept().socket();
processRequest();
_clientSocket.close();
}
}
}