Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaNIO框架在没有写操作的重载下停止工作_Java_Nio - Fatal编程技术网

JavaNIO框架在没有写操作的重载下停止工作

JavaNIO框架在没有写操作的重载下停止工作,java,nio,Java,Nio,虽然我是个新手,但这个问题对我来说相当奇怪 发生的情况是,如果您强制服务器承受沉重的连接负载,并继续发送不代表策略XML数据包的无效数据包 我想说的是,若你们连接它,它将进入套接字读取操作。然后再也不会进入send(),它会将SelectionKey更改为WRITE操作。不知何故,读取操作会堆积起来,在2000个左右的连接请求之后,服务器将停止接受连接,不管发生什么。我曾尝试连接telnet,但始终无法建立连接。。但大约5分钟后,它开始再次接受连接,并变得功能齐全 非常奇怪的问题,但请注意,如果

虽然我是个新手,但这个问题对我来说相当奇怪

发生的情况是,如果您强制服务器承受沉重的连接负载,并继续发送不代表策略XML数据包的无效数据包

我想说的是,若你们连接它,它将进入套接字读取操作。然后再也不会进入send(),它会将SelectionKey更改为WRITE操作。不知何故,读取操作会堆积起来,在2000个左右的连接请求之后,服务器将停止接受连接,不管发生什么。我曾尝试连接telnet,但始终无法建立连接。。但大约5分钟后,它开始再次接受连接,并变得功能齐全

非常奇怪的问题,但请注意,如果我删除包匹配语句,它将类似于echo服务器。然后它将无休止地运行,而不会遇到任何连接接受问题,基本上会变得稳定

我在下面附上了整个服务器源代码。有人谁与NIO有广泛的知识,请检查它,让我知道,如果有一种方法来修复它

真正吸引我眼球的是send()中的选择器wakeup,它可能会在将下面的行放入read()后修复所有问题。它似乎什么也不做,问题仍然存在

// Finally, wake up our selecting thread so it can make the required changes
this.selector.wakeup();
下面是简单服务器的源代码

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import java.nio.channels.CancelledKeyException;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.*;

public class PolicyServer implements Runnable {
    public static final String POLICY_REQUEST = "<policy-file-request/>";
    public static final String POLICY_XML =
        "<?xml version=\"1.0\"?>"
        + "<cross-domain-policy>"
        + "<allow-access-from domain=\"*\" to-ports=\"*\" />"
        + "</cross-domain-policy>"
        + (char)0;


    // The host:port combination to listen on
    private InetAddress hostAddress;
    private int port;

    // The channel on which we'll accept connections
    private ServerSocketChannel serverChannel;

    // The selector we'll be monitoring
    private Selector selector;

    // The buffer into which we'll read data when it's available
    private ByteBuffer readBuffer = ByteBuffer.allocate(255);

    // This decodes raw bytes into ascii data.
    private CharsetDecoder asciiDecoder;

    // A list of PendingChange instances
    private List<ChangeRequest> pendingChanges = new LinkedList<ChangeRequest>();

    // Maps a SocketChannel to a list of ByteBuffer instances
    private Map<SocketChannel, List<ByteBuffer>> pendingData = new HashMap<SocketChannel, List<ByteBuffer>>();

    public PolicyServer(InetAddress hostAddress, int port) throws IOException {
        this.hostAddress = hostAddress;
        this.port = port;
        this.selector = this.initSelector();
        this.asciiDecoder = Charset.forName("US-ASCII").newDecoder().onMalformedInput(
                                CodingErrorAction.REPLACE).onUnmappableCharacter(
                                CodingErrorAction.REPLACE);
    }

    public void send(SocketChannel socket, byte[] data) {
        synchronized (this.pendingChanges) {
            // Indicate we want the interest ops set changed
            this.pendingChanges.add(new ChangeRequest(socket, ChangeRequest.CHANGEOPS, SelectionKey.OP_WRITE));

            // And queue the data we want written
            synchronized (this.pendingData) {
                List<ByteBuffer> queue = (List<ByteBuffer>) this.pendingData.get(socket);
                if (queue == null) {
                    queue = new ArrayList<ByteBuffer>();
                    this.pendingData.put(socket, queue);
                }
                queue.add(ByteBuffer.wrap(data));
            }
        }

        // Finally, wake up our selecting thread so it can make the required changes
        this.selector.wakeup();
    }

    public void run() {
        while (true) {
            try {
                // Process any pending changes
                synchronized (this.pendingChanges) {
                    Iterator changes = this.pendingChanges.iterator();
                    while (changes.hasNext()) {
                        ChangeRequest change = (ChangeRequest) changes.next();
                        changes.remove();
                        if(change == null) continue;
                        switch (change.type) {
                        case ChangeRequest.CHANGEOPS:
                            SelectionKey key = change.socket.keyFor(this.selector);
                            try {
                                if(key!=null)
                                    key.interestOps(change.ops);
                            } catch(Exception ex) {
                                if (key!=null)
                                    key.cancel();
                            }
                        }
                    }
                    this.pendingChanges.clear();
                }

                // Wait for an event one of the registered channels
                this.selector.select();

                // Iterate over the set of keys for which events are available
                Iterator selectedKeys = this.selector.selectedKeys().iterator();
                while (selectedKeys.hasNext()) {
                    SelectionKey key = (SelectionKey) selectedKeys.next();
                    selectedKeys.remove();

                    if (!key.isValid()) {
                        key.cancel();
                        continue;
                    }

                    // Check what event is available and deal with it
                    try {
                        if (key.isAcceptable()) {
                            this.accept(key);
                        } else if (key.isReadable()) {
                            this.read(key);
                        } else if (key.isWritable()) {
                            this.write(key);
                        }
                    } catch(IOException io) {
                        this.pendingData.remove(key.channel());
                        try {
                            ((SocketChannel)key.channel()).socket().close();
                        } catch (IOException e) {}
                        key.channel().close();
                        key.cancel();
                        key.attach(null);
                        key = null;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private void accept(SelectionKey key) throws IOException {
        // For an accept to be pending the channel must be a server socket channel.
        ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();

        // Accept the connection and make it non-blocking
        SocketChannel socketChannel = serverSocketChannel.accept();
        Socket socket = socketChannel.socket();
        socketChannel.configureBlocking(false);

        // Register the new SocketChannel with our Selector, indicating
        // we'd like to be notified when there's data waiting to be read
        // also contains a attachment of a new StringBuffer (for storing imcomplete/multi packets)
        socketChannel.register(this.selector, SelectionKey.OP_READ, new StringBuffer());
    }

    private void read(SelectionKey key) throws IOException {
        SocketChannel socketChannel = (SocketChannel) key.channel();

        // Clear out our read buffer so it's ready for new data
        this.readBuffer.clear();

        // Attempt to read off the channel
        int numRead = socketChannel.read(this.readBuffer);

        if (numRead == -1) {
            // Remote entity shut the socket down cleanly. Do the
            // same from our end and cancel the channel.
            throw new IOException("");
        }

        // Grab the StringBuffer we stored as the attachment
        StringBuffer sb = (StringBuffer)key.attachment();

        // Flips the readBuffer by setting the current position of
        // packet stream to beginning.
        // Append the data to the attachment StringBuffer
        this.readBuffer.flip();
        sb.append(this.asciiDecoder.decode(this.readBuffer).toString());
        this.readBuffer.clear();

        // Get the policy request as complete packet
        if(sb.indexOf("\0") != -1) {
            String packets = new String(sb.substring(0, sb.lastIndexOf("\0")+1));
            sb.delete(0, sb.lastIndexOf("\0")+1);
            if(packets.indexOf(POLICY_REQUEST) != -1)
                send(socketChannel, POLICY_XML.getBytes());
        } else if(sb.length() >  8192) {
            sb.setLength(0);
            //Force disconnect.
            throw new IOException("");
        }
    }

    private void write(SelectionKey key) throws IOException {
        SocketChannel socketChannel = (SocketChannel) key.channel();

        synchronized (this.pendingData) {
            List<ByteBuffer> queue = (List<ByteBuffer>) this.pendingData.get(socketChannel);

            if(queue == null || queue.isEmpty()) {
                // We wrote away all data, so we're no longer interested
                // in writing on this socket. Switch back to waiting for
                // data.
                try {
                    if (key!=null)
                        key.interestOps(SelectionKey.OP_READ);
                } catch(Exception ex) {
                    if (key!=null)
                        key.cancel();
                }
            }

            // Write until there's not more data ...
            while (!queue.isEmpty()) {
                ByteBuffer buf = (ByteBuffer) queue.get(0);
                socketChannel.write(buf);
                if (buf.remaining() > 0) {
                    // ... or the socket's buffer fills up
                    break;
                }
                queue.remove(0);
            }
        }
    }

    private Selector initSelector() throws IOException {
        // Create a new selector
        Selector socketSelector = SelectorProvider.provider().openSelector();

        // Create a new non-blocking server socket channel
        this.serverChannel = ServerSocketChannel.open();
        serverChannel.configureBlocking(false);

        // Bind the server socket to the specified address and port
        InetSocketAddress isa = new InetSocketAddress(this.hostAddress, this.port);
        serverChannel.socket().bind(isa);

        // Register the server socket channel, indicating an interest in
        // accepting new connections
        serverChannel.register(socketSelector, SelectionKey.OP_ACCEPT);

        return socketSelector;
    }

    public static void main(String[] args) {
        try {
            new Thread(new PolicyServer(null, 5556)).start();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


import java.nio.channels.SocketChannel;

public class ChangeRequest {
    public static final int CHANGEOPS = 1;

    public SocketChannel socket;
    public int type;
    public int ops;

    public ChangeRequest(SocketChannel socket, int type, int ops) {
        this.socket = socket;
        this.type = type;
        this.ops = ops;
    }
}
import java.io.IOException;
导入java.net.InetAddress;
导入java.net.InetSocketAddress;
导入java.net.Socket;
导入java.nio.ByteBuffer;
导入java.nio.charset.charset;
导入java.nio.charset.CharsetDecoder;
导入java.nio.charset.CodingErrorAction;
导入java.nio.channels.CancelledKeyException;
导入java.nio.channels.SelectionKey;
导入java.nio.channels.Selector;
导入java.nio.channels.ServerSocketChannel;
导入java.nio.channels.SocketChannel;
导入java.nio.channels.spi.SelectorProvider;
导入java.util.*;
公共类PolicyServer实现可运行{
公共静态最终字符串策略_REQUEST=“”;
公共静态最终字符串策略\u XML=
""
+ ""
+ ""
+ ""
+(char)0;
//要侦听的主机:端口组合
私有地址主机地址;
专用int端口;
//我们将接受连接的频道
专用服务器socketchannel服务器通道;
//我们将监视的选择器
专用选择器;
//当数据可用时,我们将在其中读取数据的缓冲区
private ByteBuffer readBuffer=ByteBuffer.allocate(255);
//这将原始字节解码为ascii数据。
专用字符集译码器;
//PendingChange实例的列表
private List pendingChanges=new LinkedList();
//将SocketChannel映射到ByteBuffer实例列表
私有映射pendingData=newHashMap();
公共策略服务器(InetAddress主机地址,int端口)引发IOException{
this.hostAddress=主机地址;
this.port=端口;
this.selector=this.initSelector();
this.asciiDecoder=Charset.forName(“US-ASCII”).newDecoder()(
CodingErrorAction.REPLACE).onUnmappableCharacter(
编码错误动作。替换);
}
public void send(SocketChannel套接字,字节[]数据){
已同步(此.pendingChanges){
//表示我们希望更改利息操作集
this.pendingChanges.add(新的变更请求(socket,ChangeRequest.CHANGEOPS,SelectionKey.OP_WRITE));
//并将要写入的数据排队
已同步(此.pendingData){
List queue=(List)this.pendingData.get(socket);
if(队列==null){
queue=newarraylist();
this.pendingData.put(套接字、队列);
}
add(ByteBuffer.wrap(数据));
}
}
//最后,唤醒我们正在选择的线程,以便它可以进行所需的更改
这个.selector.wakeup();
}
公开募捐{
while(true){
试一试{
//处理任何挂起的更改
已同步(此.pendingChanges){
迭代器更改=this.pendingChanges.Iterator();
while(changes.hasNext()){
changeRequestChange=(ChangeRequest)changes.next();
更改。删除();
如果(change==null)继续;
开关(更改类型){
案例ChangeRequest.CHANGEOPS:
SelectionKey=change.socket.keyFor(this.selector);
试一试{
if(key!=null)
键.键(change.ops);
}捕获(例外情况除外){
if(key!=null)
键。取消();
}
}
}
此.pendingChanges.clear();
}
//等待已注册通道之一的事件
this.selector.select();
//迭代事件可用的键集
迭代器selectedKeys=this.selector.selectedKeys().Iterator();
while(selectedKeys.hasNext()){
SelectionKey=(SelectionKey)selectedKeys.next();
selectedKeys.remove();
如果(!key.isValid()){
键。取消();
继续;
}
//检查哪些活动可用并进行处理
试一试{
if(key.isAcceptable()){
这个。接受(键);
}else if(key.isReadable()){
 ulimit -u unlimited
key.channel().close()