Java NIO Server端不能向客户端发送消息?

伤神 发布于 2016/01/22 19:54
阅读 993
收藏 0

写了个程序,NIOServer,两个 Client,一个传统的 Client 和 一个 NIO Client,传统的 Client 和 NIO Client 都可以向服务器发送消息,但是 NIOServer 向客户端返回消息,却只有 NIO Client 可以收到消息,传统的 Client 却不能通过 Sockt.getInputStream() 得到消息。百思不得其解,NIO 不都是监听本来的 socket 连接,有数据则通知 NIO 获取连接并获得数据,然而传统的 Socket 连接,不是一直监听本来的 socket 连接, 照理说,不是一旦有数据写入,就可以获取,但是传统的 client Socket 连接,却不能获得数据?百思不得其解,请高手指点。下面是我写的一个简单的代码。

NioServer

public class NioServer {  
	
    private Selector selector;  
  
    public void initServer( int port ) throws IOException {  
        
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        
        serverSocketChannel.configureBlocking(false);
        
        serverSocketChannel.socket().bind( new InetSocketAddress(port) );
        
        this.selector = Selector.open();
        
        serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);  
        
    }  
  
    /** 
     * 采用轮询的方式监听selector上是否有需要处理的事件,如果有,则进行处理 
     * @throws IOException 
     */  
    public void listen() throws IOException {  
    	
        while (true) {  
            
            selector.select();  
            
            Iterator<SelectionKey> iterator = this.selector.selectedKeys().iterator();
            
            while (iterator.hasNext()) {  
            	
                SelectionKey key = (SelectionKey) iterator.next();  
                
                iterator.remove();
                
                if (key.isAcceptable()) {  
                
                	System.out.println("event is acceptable ~~");
                	
                	accept( key );
                	
                }
                
                if (key.isReadable()) {  
                	
                	System.out.println("event is readable ~~");
                	
                    read( key );  
                }
                
            }  
  
        }  
    }  
    
    public void accept(SelectionKey key) throws IOException{
    	
        ServerSocketChannel server = (ServerSocketChannel) key.channel();  
        
        SocketChannel channel = server.accept();  
        
        System.out.println("channel id:" + channel.hashCode() );
        
        channel.configureBlocking(false);
        
        channel.register( this.selector, SelectionKey.OP_READ );
        
    }
        
    public void read(SelectionKey key) throws IOException{  
        
        SocketChannel channel = (SocketChannel) key.channel();
        
        ByteBuffer buffer = ByteBuffer.allocate( 1024 );

        channel.read(buffer);
        
        byte[] data = buffer.array();  

        String msg = new String(data).trim();  
        
        System.out.println( "received from client:" + msg );
        
        // 写数据到客户端
        channel.write( ByteBuffer.wrap( "hi client, server received your message".getBytes() ) ); 
        
    }  
      
    /** 
     * 启动服务端测试 
     * @throws IOException  
     */  
    public static void main(String[] args) throws IOException {
    	
        NioServer server = new NioServer();
        
        server.initServer(5678);
        
        server.listen();
        
    }  
  
}
Client
public class Client {  
	
    public static void main(String[] args) throws Exception {  
        
    	Socket server = new Socket(InetAddress.getLocalHost(), 5678);  
    	
        BufferedReader in = new BufferedReader( new InputStreamReader( server.getInputStream() ) );  
        
        PrintWriter out = new PrintWriter(server.getOutputStream());
        
        while (true) {  
        	
            out.println( "hello, I'm Native Client ~ " );
            
            out.flush();
            
            System.out.println("read from server: " + in.readLine() );
            
        }
        
    }  
    
}
NioClient
public class NioClient {  
	
    private Selector selector;  
  
    public void initClient(String ip,int port) throws IOException {  
        
    	SocketChannel channel = SocketChannel.open();  

        channel.configureBlocking(false);  
        
        this.selector = Selector.open();  
          
        channel.connect( new InetSocketAddress(ip,port) );  
          
        channel.register( selector, SelectionKey.OP_CONNECT );  
    }  
  
    public void listen() throws IOException {  
    	
        while (true) {  
        	
            selector.select();  
            
            Iterator<SelectionKey> ite = this.selector.selectedKeys().iterator();
            
            while (ite.hasNext()) {  
            	
                SelectionKey key = (SelectionKey) ite.next();
                
                ite.remove();
                
                if (key.isConnectable()) {  
                	
                    connect(key);
                      
                } 
                
                if (key.isReadable()) {  
                	
                    read(key);
                
                }
                
                if (key.isWritable() ){
                	
                	write(key);
                     	
                }
  
            }  
  
        }  
    }  
    
    public void connect(SelectionKey key) throws IOException{
        
        SocketChannel channel = (SocketChannel) key.channel();

        if(channel.isConnectionPending()){  
            channel.finishConnect();  
        }  
        
        channel.configureBlocking(false);  
        
        channel.register( this.selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE );     
        
    }
    
    public void read(SelectionKey key) throws IOException{  
    	
        SocketChannel channel = (SocketChannel)key.channel();
        
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        
        channel.read(buffer);
        
        byte[] data = buffer.array();
        
        String message = new String(data);
        
        System.out.println("recevie message from server: " + message);
    
    }  
    
    public void write(SelectionKey key) throws IOException{
    	
        SocketChannel channel = (SocketChannel) key.channel();
        
        channel.write(ByteBuffer.wrap( "hello, I'm Nio Client ~ ".getBytes()));
        
        key.interestOps(key.interestOps() & ~SelectionKey.OP_WRITE); 

    }
      
    public static void main(String[] args) throws IOException {
    	
        NioClient client = new NioClient();
        
        client.initClient("localhost", 5678);
        
        client.listen();
        
    }  
  
}

程序代码逻辑很简单,Client 和 NioClient 均向 NioServer 发送消息,NioServer 收到消息后,向客户端反馈消息,但是只有 NioClient 可以收到服务器的响应,Client 却不行。



加载中
0
xpbob
xpbob
BufferedReader只有读到换行符号或者读满8kb返回空,你需要在你服务器发送的数据中加入换行符,想要平台通用的话,加上\r\n
返回顶部
顶部