非阻塞 write() 变为无限 - java

Nonblocking write() goes infinitie - java

我有一个简单的网络抓取器。 OP_WRITE 会一直触发 selector,所以 select() 永远不会结束。 Select 始终是 return 1,等待写入,尽管通道已完成其工作并关闭。我该如何处理?我的 CPU 也非常高。

代码如下:

public static void main(String [] args)
{

try
{
    String adder="";

    Selector selector= simpleFetcher.selector;
    selector = Selector.open();

    Charset charset = Charset.forName("ISO-8859-1");

    try{

    //add to selector the host and settings
    addHost(selector, "localhost", 80);
    addHost(selector, "site.org", 80);

    } catch (UnresolvedAddressException ex) {System.out.println("Address Not exist "+ex.getMessage());}

    while(true)
    {

    int selectedn = selector.select(2000);
    if (selectedn==0)
    {
        Set<SelectionKey> SelectorKeys = selector.keys();
        Iterator<SelectionKey> iterator  = SelectorKeys.iterator();

        while(iterator.hasNext())
        {
            SelectionKey k = (SelectionKey) iterator.next();
            SocketChannel channel = (SocketChannel) k.channel();

            String rHost;
            InetAddress addr = channel.socket().getInetAddress();
            rHost= addr.getHostAddress();
            System.out.println(rHost+" is timeout");

        }
        System.out.println("adder:"+adder);
        selector.close();

        System.out.println("No more keys.. Exiting");
        return;
    }

    Set<SelectionKey> selectedKeys = selector.selectedKeys();
    Iterator<SelectionKey> iterator  = selectedKeys.iterator();

    while(iterator.hasNext())
    {
        SelectionKey key = (SelectionKey) iterator.next();
        iterator.remove();

        if (key.isValid()==false) {System.out.println("key not valid"); continue;}


        try{
            if (key.isConnectable())
            {

                System.out.println("Will connect right now");

                SocketChannel channel = (SocketChannel) key.channel();

                if (!channel.finishConnect()) { System.out.println("Not finished connection. continue.."); continue; }
                if (channel.isConnectionPending()==false && channel.socket().isClosed()==true) System.out.println("CLOSED?");


                SelectionKey k = channel.register(key.selector(),SelectionKey.OP_READ);
                SelectionKey k2 = channel.register(key.selector(),SelectionKey.OP_WRITE);
                //SelectionKey k = channel.register(key.selector(),SelectionKey.OP_READ | SelectionKey.OP_WRITE);
                k.attach(ByteBuffer.allocate(4000));


                System.out.println("Just Connected");
                continue;
            }
            if (key.isReadable())
            {
                int readB;
                System.out.println("Ready for reading..");
                SocketChannel channel = (SocketChannel) key.channel();
                ByteBuffer buffer = (ByteBuffer) key.attachment();
                CharBuffer charbuffer = buffer.asCharBuffer();

                if ((readB=channel.read(buffer)) !=-1) 
                {
                    System.out.println("Key accepted - reading..");
                    int current_capacity=buffer.position();

                    buffer.flip();
                    CharBuffer c = charset.decode(buffer);
                    char[] arr = c.array();


                    String data = new String(arr);

                    String[] lines = data.split("\r\n\r\n");
                    if (lines.length>1) 
                    {
                        String header = lines[0];
                        String rest_body = lines[1];
                        //System.out.println("Header is: "+header);
                        //System.out.println("Body is: "+rest_body);
                        adder+=header+rest_body;
                    }
                    else { adder+=new String(arr); }


                }
                else 
                {
                    key.channel().close();
                    key.cancel();
                    System.out.println("Key cancled");
                    System.out.println(adder);

                    continue;
                }
                if (readB==0) System.out.println("The READ RETURNS 0");

                    System.out.println();
                    buffer.clear();

                continue;

            }
            if (key.isWritable())
            {

                //System.out.println("Ready to write");

                    SocketChannel channel = (SocketChannel) key.channel();
                    String rHost;

                    Socket s = channel.socket();
                    s.shutdownOutput();



                    InetAddress addr = channel.socket().getInetAddress();
                    rHost= addr.getHostAddress();
                    //System.out.println(rHost);

                    if (s.isOutputShutdown()) continue;

                    String bytestowrite="GET / HTTP/1.1\r\nHost: "+rHost+"\r\nUser-agent: Agent 1.0 Experimental\r\nAccept: */*\r\nAccept-Language: en-US,en;q=0.5\r\nConnection: keep-alive\r\n\r\n";
                    ByteBuffer buffer = ByteBuffer.wrap(bytestowrite.getBytes());

                    while(buffer.hasRemaining()) channel.write(buffer);
                    key.cancel();
                continue;
            }


        } catch (IOException ex) {
            System.out.println("EXCEPTION: "+ex.getMessage());
            key.cancel(); 
            try { key.channel().close();} 
            catch (IOException cex) {}; 
        }

        System.out.println("End of iterator loop");
    }
    }
}catch (IOException ex) {System.out.println("Timeout");}



}

套接字通道几乎总是可写的,这意味着套接字发送缓冲区中几乎总是space可用。只有当发送者超过接收者时,这才不再成立。

因此,为 OP_WRITE 注册频道是不正确的,除非它们已经经历了缓冲区已满的情况,即如果 write() 已返回零,并且应该取消注册 OP_WRITE一旦后续写入成功。