• java nio


    server:

    package com.zyw.javaio.test;
    
    import java.io.IOException;
    import java.net.InetSocketAddress;
    import java.net.ServerSocket;
    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;
    
    public class Server3 {
    	public static void main(String[] args) {
    		try {
    			//return new Selector
    			Selector selector=Selector.open();
    			//return ssc
    			ServerSocketChannel ssc=ServerSocketChannel.open();
    			ssc.configureBlocking(false);
    			ServerSocket ss=ssc.socket();
    			ss.bind(new InetSocketAddress(8888));
    			ssc.register(selector, SelectionKey.OP_ACCEPT);
    			ServerSocketChannel ssc1=ServerSocketChannel.open();
    			ssc1.configureBlocking(false);
    			ServerSocket ss1=ssc1.socket();
    			ss1.bind(new InetSocketAddress(9999));
    			// SelectionKey mark this SSC.
    			ssc1.register(selector, SelectionKey.OP_ACCEPT);
    			while (true){
    				int caseNum=selector.select();
    				if (caseNum==0){continue;}
    				//we case thing is hanppening
    				Set<SelectionKey> keys=selector.selectedKeys();
    				Iterator it=keys.iterator();
    				while (it.hasNext()){
    					SelectionKey sk=(SelectionKey) it.next();
    
    					if (sk.isAcceptable()){
    						// dont worry  about accept  blocking.
    						ServerSocketChannel sl=(ServerSocketChannel) sk.channel();
    						SocketChannel sc1=sl.accept();
    						sc1.configureBlocking(false);
    						//now newconnect can read.
    						SelectionKey newKey = sc1.register( selector, SelectionKey.OP_READ );
    						System.out.println(sk+"can read");
    						it.remove();		
    					}else if(sk.isReadable()){
    						SocketChannel sl=(SocketChannel) sk.channel();
    						 ByteBuffer buffer=ByteBuffer.allocate(1024);    
    		                    try{    
    		                    sl.read(buffer);    
    		                    }    
    		                    catch(Exception ex){    
    		                        sl.close();    
    		                    }    
    		                    System.out.println(new String(buffer.array()));    		                    
    					}
    				}
    			}
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    }
    

    client:

    package com.zyw.javaio.test;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.Socket;
    import java.net.UnknownHostException;
    
    public class Client {
    
    	public static void main(String[] args) {
    		try {
    			Socket sc=new Socket("127.0.0.1",9999);
    			OutputStream out=sc.getOutputStream();
    			out.write("hello server!!! I want to connect.".getBytes());
    			out.flush();
    		} catch (UnknownHostException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (IOException e) {
    			// TODO Auto-generated catch blockG
    			e.printStackTrace();
    		}
    	}
    
    }
    

      

  • 相关阅读:
    everything is nothing
    基础算法
    OC 优化目录
    iOS 更改启动视图
    单例--iOS
    OC-Objection 学习笔记之一:简单的开始
    iOS 类库列表
    IOS 上线问题
    OC强弱引用的使用规则
    设置桌面图标
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5493243.html
Copyright © 2020-2023  润新知