Java distributed development TCP / IP NIO non-blocking Socket ((based on the message mode to achieve communication between systems))

From: https://www.iteye.com/blog/mars914-1238353

In java, TCP / IP + NIO inter-system communication can be realized based on the related classes of Channel and Selector in java.nio.channels.

 

Used for inter-system communication. SocketChannel and ServerSocketChannel are used. SocketChannel is used to establish connections, monitor events and read and write operations. ServerSocketChannel is used to monitor ports and monitor connection events. You can use the Selector to obtain whether there are events to be processed.

 

Server java code:

Java code   Favorite Code
  1. package com.java.distributed.message.tcpip;  
  2.   
  3. import java.io.IOException;  
  4. import java.net.InetSocketAddress;  
  5. import java.net.ServerSocket;  
  6. import java.nio.ByteBuffer;  
  7. import java.nio.channels.SelectionKey;  
  8. import java.nio.channels.Selector;  
  9. import java.nio.channels.ServerSocketChannel;  
  10. import java.nio.channels.SocketChannel;  
  11. import java.nio.charset.Charset;  
  12.   
  13. public class NIOServer {  
  14.   
  15.     /** 
  16.      * @param args 
  17.      * @throws IOException  
  18.      */  
  19.     public static void main(String[] args) throws IOException {  
  20.         int port =7889;  
  21.         // Open the selector  
  22.         Selector selector=Selector.open();  
  23.         // Open the server socket channel  
  24.         ServerSocketChannel ssc=ServerSocketChannel.open();  
  25.         // Retrieve the server socket associated with this channel  
  26.         ServerSocket serverSocket=ssc.socket();  
  27.         // Bind ServerSocket to a specific address (IP address and port number)  
  28.         serverSocket.bind(new InetSocketAddress(port));  
  29.         System.out.println("server listen on port:"+port);  
  30.           
  31.         // Adjust the blocking mode of the channel  
  32.         ssc.configureBlocking(false);  
  33.         // Register this channel with the given selector and return a selection key. SelectionKey.OP_ACCEPT--Operation set bit for socket accept operation     
  34.         ssc.register(selector, SelectionKey.OP_ACCEPT);  
  35.           
  36.         while(true){  
  37.             // timeout: positive, then block at most timeout ms while waiting for a channel to be ready; if zero, block indefinitely; must be non-negative  
  38.             int nKeys=selector.select(1000);  
  39.             if(nKeys>0){  
  40.                   
  41.                 for(SelectionKey key:selector.selectedKeys()){  
  42.                     / * Test whether the channel of this key is ready to accept new socket connections-- 
  43.                      * If the channel of this key does not support socket accept operation, this method always returns false 
  44.                      * */  
  45.                     if(key.isAcceptable()){  
  46.                         ServerSocketChannel server=(ServerSocketChannel) key.channel();  
  47.                         SocketChannel sc=server.accept();  
  48.                           
  49.                         if(sc==null){  
  50.                             continue;  
  51.                         }  
  52.                         sc.configureBlocking(false);  
  53.                         sc.register(selector, SelectionKey.OP_READ);  
  54.                     }else if(key.isReadable()){  
  55.                         // Allocate a new byte buffer  
  56.                         ByteBuffer buffer=ByteBuffer.allocate(1024);  
  57.                         SocketChannel sc=(SocketChannel) key.channel();  
  58.                         int readBytes=0;  
  59.                         String message=null;  
  60.                         try{  
  61.                             int ret;  
  62.                             try{  
  63.                                 while((ret=sc.read(buffer))>0){  
  64.                                     readBytes + = ret;  
  65.                                 }  
  66.                                   
  67.                             }catch(Exception e ){  
  68.                                 readBytes=0;  
  69.                                 //ignore  
  70.                             }finally{  
  71.                                 // Reverse this buffer. First set a limit on the current position, then set the position to zero  
  72.                                 buffer.flip();  
  73.                             }  
  74.                               
  75.                             if(readBytes>0){  
  76.                                 message=Charset.forName("UTF-8").decode(buffer).toString();  
  77.                                 buffer=null;  
  78.                             }  
  79.                         }finally{  
  80.                             if(buffer!=null)  
  81.                                 buffer.clear();  
  82.                         }  
  83.                           
  84.                         if(readBytes>0){  
  85.                             System.out.println("message from client:"+message);  
  86.                             if("quit".equalsIgnoreCase(message.trim())){  
  87.                                 sc.close();  
  88.                                 selector.close();  
  89.                                 System.out.println("Server has been shutdown!");  
  90.                                 System.exit(0);  
  91.                             }  
  92.                             String outMessage="server response:"+message;  
  93.                             sc.write(Charset.forName("UTF-8").encode(outMessage));  
  94.                         }  
  95.                           
  96.                     }  
  97.                 }  
  98.                 selector.selectedKeys().clear();  
  99.             }  
  100.           
  101.         }  
  102.     }  
  103. }  

 

 

Client java code:

Java code   Favorite Code
  1. package com.java.distributed.message.tcpip;  
  2.   
  3. import java.io.BufferedReader;  
  4. import java.io.IOException;  
  5. import java.io.InputStreamReader;  
  6. import java.net.InetSocketAddress;  
  7. import java.net.SocketAddress;  
  8. import java.nio.ByteBuffer;  
  9. import java.nio.channels.SelectionKey;  
  10. import java.nio.channels.Selector;  
  11. import java.nio.channels.SocketChannel;  
  12. import java.nio.charset.Charset;  
  13.   
  14.   
  15. public class NIOClient {  
  16.   
  17.     /** 
  18.      * @param args 
  19.      * @throws IOException  
  20.      */  
  21.     public static void main(String[] args) throws IOException {  
  22.         int port =7889;  
  23.         SocketChannel channel=SocketChannel.open();  
  24.         channel.configureBlocking(false);  
  25.           
  26.         SocketAddress target=new InetSocketAddress("127.0.0.1",port);  
  27.         channel.connect(target);  
  28.         Selector selector=Selector.open();  
  29.         // Operation set bit for socket connection operation  
  30.         channel.register(selector, SelectionKey.OP_CONNECT);  
  31.         BufferedReader systemIn=new BufferedReader(new InputStreamReader(System.in));  
  32.           
  33.         while(true){  
  34.             if(channel.isConnected()){  
  35.                 String command=systemIn.readLine();  
  36.                 channel.write(Charset.forName("UTF-8").encode(command));  
  37.                   
  38.                 if(command==null||"quit".equalsIgnoreCase(command.trim())){  
  39.                     systemIn.close();  
  40.                     channel.close();  
  41.                     selector.close();  
  42.                     System.out.println("Client quit !");  
  43.                     System.exit(0);  
  44.                 }  
  45.             }  
  46.             int nKeys=selector.select(1000);  
  47.             if(nKeys>0){  
  48.                 for(SelectionKey key:selector.selectedKeys()){  
  49.                     if(key.isConnectable()){  
  50.                         SocketChannel sc=(SocketChannel) key.channel();  
  51.                         sc.configureBlocking(false);  
  52.                         sc.register(selector, SelectionKey.OP_READ);  
  53.                         sc.finishConnect();  
  54.                     }else if(key.isReadable()){  
  55.                         ByteBuffer buffer=ByteBuffer.allocate(1024);  
  56.                         SocketChannel sc=(SocketChannel) key.channel();  
  57.                         int readBytes=0;  
  58.                         try{  
  59.                             int ret = 0;  
  60.                             try{  
  61.                                 while((ret=sc.read(buffer))>0){  
  62.                                     read bytes = K +;  
  63.                                 }  
  64.                             }finally{  
  65.                                 buffer.flip();  
  66.                             }  
  67.                             if (readBytes > 0) {     
  68.                                 System.out.println(Charset.forName("UTF-8")     
  69.                                         .decode(buffer).toString());     
  70.                                 buffer = null;     
  71.                             }     
  72.   
  73.                         }finally {     
  74.                             if (buffer != null) {     
  75.                                 buffer.clear();     
  76.                             }  
  77.                         }  
  78.                     }  
  79.                 }  
  80.                     selector.selectedKeys().clear();     
  81.             }  
  82.         }  
  83.     }  
  84.   
  85. }  

Guess you like

Origin www.cnblogs.com/sharpest/p/12702762.html