PlainDatagramSocketImpl(IOException:不允许操作)

时间:2015-01-22 20:46:46

标签: java sockets udp

我遇到了DatagramSockets的以下问题:

java.io.IOException: Operation is not permitted.
    at java.net.PlainDatagramSocketImpl.send(Native Method)
    at java.net.DatagramSocket.send(DatagramSocket.java:693)

异常是随机发生的,我无法真正看到模式。 这使我更难调试它。

尽管如此,我怀疑当我发送大量数据时会更频繁地发生这种情况。

我有多个线程通过此套接字发送,但这不应该是一个问题,因为我读Java套接字是线程安全的。

有人可以告诉我何时以及在什么条件下可以抛出这样的例外?

这是我的基本网络代码:

package de.oompf.netwrk;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

class Server implements Runnable {

    private final EventBus bus;
    private final Thread serverThread;
    private final DatagramSocket socket;

    Server(EventBus bus) throws SocketException {
    this.bus = bus;
    serverThread = new Thread(this, "Server Thread");
    socket = getBoundSocket();
    socket.setSoTimeout(2400);
    }

    private static DatagramSocket getBoundSocket() throws SocketException {
    for (int port : Configuration.getPortList()) {
        try {
            return new DatagramSocket(port);
        } catch (SocketException e) {
        }
    }
    return new DatagramSocket(0);
    }

    int getPort() {
    return socket.getLocalPort();
    }

    void start() {
    bus.subscribe(this);
    serverThread.start();
    }

    void stop() {
    serverThread.interrupt();
    socket.close();
    }

    @Override
    public void run() {
    DatagramPacket p = new DatagramPacket(new byte[4096], 4096);
    while (!serverThread.isInterrupted()) {
        try {
            socket.receive(p);
            bus.publish(new IncomingPacket(p.getData(), p.getLength(), p.getAddress(), p.getPort()));
        } catch (IOException e) {
            if (socket.isClosed()) {
                break;
            }
        }
    }
    }

    void send(OutgoingPacket p) {
    try {
        if (p.getData()[0] == 0x03) {
        }
        socket.send(new DatagramPacket(p.getData(), p.getData().length, p.getSocketAddress()));
    } catch (IOException e) {
        if (socket.isClosed()) {
            serverThread.interrupt();
        } else {
            e.printStackTrace();
        }
    }
    }
}

这背后有很多课程。我只想发布我的堆栈跟踪结束的几行。

private void handleBootstrapRequest(IncomingPacket p) {
if (p.getLength() == 21) {
    byte[] requestNodeBytes = new byte[20];
    System.arraycopy(p.getData(), 1, requestNodeBytes, 0, 20);
    try {
        Node requestNode = new Node(requestNodeBytes);
        if (needsRelay(requestNode)) {
            byte[] forwardPacket = new byte[47];
            forwardPacket[0] = 0x07;
            System.arraycopy(requestNode.getBytes(), 0, forwardPacket, 1, 20);
            System.arraycopy(me.getBytes(), 0, forwardPacket, 21, 20);
            System.arraycopy(p.getAddress().getAddress(), 0, forwardPacket, 41, 4);
            System.arraycopy(ByteBuffer.allocate(2).putShort((short) (p.getPort() - Short.MAX_VALUE)).array(), 0, forwardPacket, 45, 2);
            /* Will send a packet (doing some routing first) */
            relay(forwardPacket, requestNode);
        } else {
            List<Neighbour> references = routing.getClosest(requestNode, 7);
            byte[] answerPacket = new byte[2 + references.size() * 26];
            answerPacket[0] = 0x06;
            answerPacket[1] = (byte) references.size();
            for (int i = 0; i < references.size(); i++) {
                Neighbour n = references.get(i);
                System.arraycopy(n.getBytes(), 0, answerPacket, 2 + i * 26, 20);
                System.arraycopy(n.getAddress().getAddress().getAddress(), 0, answerPacket, 22 + i * 26, 4);
                System.arraycopy(ByteBuffer.allocate(2).putShort((short) (n.getAddress().getPort() - Short.MAX_VALUE)).array(), 0, answerPacket, 26 + i * 26, 2);
            }
            /* That's where my stack trace ends and where the packet gets onto an event bus (100% working properly) */
            bus.publish(new OutgoingPacket(answerPacket, p.getSocketAddress()));
        }

        byte[] quickResponse = new byte[21];
        quickResponse[0] = 0x02;
        System.arraycopy(me.getBytes(), 0, quickResponse, 1, 20);

        /* see last comment */
        bus.publish(new OutgoingPacket(quickResponse, p.getSocketAddress()));
    } catch (InvalidNodeException e) {
    }
}
}

正如我所说,当我的数据包处理程序池调用多个数据包处理程序时,事件总线上可能存在多个传出数据包。

2 个答案:

答案 0 :(得分:1)

几年后就遇到了同样的问题。

在我的情况下,失败是由于conntrack填充引起的,这不是Java特有的,并且已在其他地方(12)进行了报告。解决该问题的一种简单方法是使所有udp流量都绕过conntrack:

iptables -I PREROUTING -t raw -p udp -j NOTRACK
iptables -I OUTPUT -t raw -p udp -j NOTRACK

如下所示,原始的PREROUTING和OUTPUT链就在conntrack之前:

linux networking packet flow

如果您不想完全绕过conntrack,可以改为在sysctl中调整其配置以允许更多连接。

答案 1 :(得分:0)

进行更多调查,我发现我的问题是在多个线程同时访问DatagramSocket.send时引起的。

要解决此问题,您需要同步发送方法。

试试这段代码。它可能会重现 java.io.IOException:不允许操作。

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;

public class Test {

    public static void main(String[] args) {
        new Test().now();
    }

    public void now() {
        try {
            DatagramSocket sock = new DatagramSocket(0);
            for (int i = 0; i < 32; i++) {
                new Thread(new Sender(sock)).start();
            }
        } catch (SocketException e) {
        }
    }

    public class Sender implements Runnable {

        private final DatagramSocket sock;

        public Sender(DatagramSocket sock) {
            this.sock = sock;
        }

        @Override
        public void run() {
            for(int i=0; i<12; i++) {
                try {
                    sock.send(new DatagramPacket(new byte[20], 20, new InetSocketAddress(InetAddress.getByName("example.com"), 80)));
                } catch(IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}