-
August 27th, 2014, 04:35 AM
#1
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:
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:
Code:
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!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|