Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: NIO UDP DatagramChannel data not sending out…

  1. #1
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default NIO UDP DatagramChannel data not sending out…

    Nice to meet you, dear all.

    I'm a beginner and this is my first time programming such thing using Java.

    I'm writing a program which gets data input from a port, saves it into a buffer, and when the buffer is full, it'll send out the data using another port. The protocol is UDP.

    So here is my code:

    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();
    }

    There are several problems...

    I tried to use a program to read from 127.0.0.1:OUT_PORT. The program said that the port is in use. What shall I do?

    Say BUFFER_LIST_SIZE = 5 and outChannel.send(...) is commented. The output data is always blank, not the input one. Also, if I stop sending data to it, it will not output the remaining data from its buffer. With inputs like "* UDP Flood. Server stress test ****", it will output:

     
        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):

    Thank you so much!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NIO UDP DatagramChannel data not sending out…

    The program said that the port is in use.
    Has the program been executed more than once? Has the first execution stopped completely before the second one has started?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2013
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: NIO UDP DatagramChannel data not sending out…

    Quote Originally Posted by Norm View Post
    Has the program been executed more than once? Has the first execution stopped completely before the second one has started?
    You mean the 3rd party test program that reads the OUT_PORT, or my Java program?
    There is only one instance running at the same time.
    Thank you so much.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: NIO UDP DatagramChannel data not sending out…

    Have you checked that the port was not in use before starting your program?
    Could your program try to use the post more than once?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. DatagramChannel & NIO Selector
    By amit1983 in forum Java Networking
    Replies: 0
    Last Post: May 20th, 2014, 04:01 AM
  2. DatagramChannel - UDP receiving OK, but unable to send data :(
    By cr0ck3t in forum File I/O & Other I/O Streams
    Replies: 7
    Last Post: April 29th, 2014, 04:18 PM
  3. Sending and Receiving Via Tcp And Udp
    By amit1983 in forum Java Theory & Questions
    Replies: 4
    Last Post: December 9th, 2013, 03:56 PM
  4. Replies: 2
    Last Post: July 9th, 2013, 06:01 PM
  5. Need Help to acquire data from a connection UDP
    By clavius11 in forum Java Networking
    Replies: 4
    Last Post: May 3rd, 2013, 06:41 AM

Tags for this Thread