Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.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
Java NIO UDP数据报通道数据未发送_Java_Sockets_Network Programming_Udp_Nio - Fatal编程技术网

Java NIO UDP数据报通道数据未发送

Java NIO UDP数据报通道数据未发送,java,sockets,network-programming,udp,nio,Java,Sockets,Network Programming,Udp,Nio,我正在编写一个程序,从一个端口获取数据输入,将其保存到一个缓冲区中,当缓冲区已满时,它将使用另一个端口发送数据。协议是UDP 这是我的代码: try { // UDP data-in settings DatagramChannel inChannel = DatagramChannel.open(); SocketAddress inAddress = new InetSocketAddress(FMBuffer.IN_PORT); inChannel.bind

我正在编写一个程序,从一个端口获取数据输入,将其保存到一个缓冲区中,当缓冲区已满时,它将使用另一个端口发送数据。协议是UDP

这是我的代码:

try {
    // UDP data-in settings
    DatagramChannel inChannel = DatagramChannel.open();
    SocketAddress inAddress = new InetSocketAddress(FMBuffer.IN_PORT);
    inChannel.bind(inAddress);

    // UDP data-out settings
    DatagramChannel outChannel = DatagramChannel.open();
    SocketAddress outAddress = new InetSocketAddress(FMBuffer.OUT_PORT);
    outChannel.bind(outAddress);

    LinkedList<ByteBuffer> buffer = new LinkedList<ByteBuffer>();
    ByteBuffer b = ByteBuffer
            .allocateDirect(FMBuffer.UDP_MAX_PACKET_SIZE);

    while (true) {          

        if (inChannel.receive(b) != null) { // Receive data and put into b
            b.flip();                   
            // Save b into the buffer
            buffer.addLast(b);

            System.out.print("In(" + buffer.size() + "): ");
            while (buffer.getLast().hasRemaining()) {                           
                System.out.write(buffer.getLast().get());
            }
            System.out.println();

            // If the buffer reaches the set limit
            if (buffer.size() > FMBuffer.BUFFER_LIST_SIZE) {
                // Send the first b out, then remove it
                System.out.print("Out(" + buffer.size() + "): ");
                while (buffer.getFirst().hasRemaining()) {                          
                    System.out.write(buffer.getFirst().get());
                    outChannel.send(buffer.getFirst(), outAddress);
                }
                System.out.println();
                buffer.removeFirst();
            }

            b.clear();

        } else { // Receive no data
            if (buffer.size() > 0) {
                // Send the first b out, then remove it
                System.out.print("Out remaining(" + buffer.size() + "): ");
                while (buffer.getFirst().hasRemaining()) {                          
                    System.out.write(buffer.getFirst().get());
                    outChannel.send(buffer.getFirst(), outAddress);
                }
                System.out.println();
                buffer.removeFirst();
            }
        }
    }
} catch (IOException e) {
    e.printStackTrace();
}

“程序说端口正在使用。我该怎么办?”呃,使用另一个端口?
In(1): ***** UDP Flood. Server stress test ****
In(2): ***** UDP Flood. Server stress test ****
In(3): ***** UDP Flood. Server stress test ****
In(4): ***** UDP Flood. Server stress test ****
In(5): ***** UDP Flood. Server stress test ****
In(6): ***** UDP Flood. Server stress test ****
Out(6): 
In(6): ***** UDP Flood. Server stress test ****
Out(6): 
In(6): ***** UDP Flood. Server stress test ****
Out(6): 
In(6): ***** UDP Flood. Server stress test ****
Out(6):