DatagramChannel.read() returns -1 when receiving empty UDP multicast
I am using a connected DatagramChannel in non-blocking mode to receive UDP multicast. I have found that DatagramChannel.read(ByteBuffer)
returns -1 whenever my program receive an empty UDP multicast message. But the API documentation says: "The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream".
I would expect end-of-stream to be a condition upon which the channel can no longer be used - just like a closed TCP connection, but it is perfectly possible to keep using the DatagramChannel for reception after getting the -1.
Is it safe to assume that it is always possible to continue using a DatagramChannel even after read(ByteBuffer)
has returned -1?
Is it a little strange to have a return value indicating end-of-stream when all that has happened is the reception of an empty UDP message? Wouldn't it be more appropriate if the return value was 0?
Here are my test programs, first the multicast transmitter:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class LocalServer {
final static int PORT = 7001;
final static String MCAST_ADDRESS = "233.2.3.3";
final static String INTERFACE_ADDRESS = "127.0.0.1";
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelServer: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
SocketAddress dst = new InetSocketAddress(group, PORT);
int i = 0;
while (true) {
System.out.println("Transmitting #" + i);
ByteBuffer asterixBuf = ByteBuffer.wrap(
("line " + i + "123456789012345678901234567890123456789012345678901234567890").getBytes());
dc.send(asterixBuf, dst);
i++;
// Randomly transmit an empty UDP datagram and then sleep.
if (Math.random() > 0.50d) {
System.out.println("Transmitting an empty buffer");
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
dc.send(emptyBuf, dst);
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And then the multicast receiver:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
public class LocalClient {
final static int PORT = LocalServer.PORT;
final static String MCAST_ADDRESS = LocalServer.MCAST_ADDRESS;
final static String CONNECT_HOST = LocalServer.INTERFACE_ADDRESS;
final static String INTERFACE_ADDRESS = CONNECT_HOST;
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelClient: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
MembershipKey key = dc.join(group, ni);
dc.configureBlocking(false);
InetSocketAddress foreignInetAddress = new InetSocketAddress(InetAddress.getByName(CONNECT_HOST), 0);
dc.connect(foreignInetAddress);
ByteBuffer receiveBuffer = ByteBuffer.allocate(64 * 1024);
while (true) {
dc.receive(receiveBuffer);
int bytesRead = dc.read(receiveBuffer);
if (bytesRead == -1) {
System.out.println("bytesRead=" + bytesRead);
}
else if (bytesRead > 0) {
receiveBuffer.flip();
System.out.println();
while (receiveBuffer.hasRemaining())
System.out.print((char) receiveBuffer.get());
System.out.println();
receiveBuffer.clear();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
java udp nio multicast
add a comment |
I am using a connected DatagramChannel in non-blocking mode to receive UDP multicast. I have found that DatagramChannel.read(ByteBuffer)
returns -1 whenever my program receive an empty UDP multicast message. But the API documentation says: "The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream".
I would expect end-of-stream to be a condition upon which the channel can no longer be used - just like a closed TCP connection, but it is perfectly possible to keep using the DatagramChannel for reception after getting the -1.
Is it safe to assume that it is always possible to continue using a DatagramChannel even after read(ByteBuffer)
has returned -1?
Is it a little strange to have a return value indicating end-of-stream when all that has happened is the reception of an empty UDP message? Wouldn't it be more appropriate if the return value was 0?
Here are my test programs, first the multicast transmitter:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class LocalServer {
final static int PORT = 7001;
final static String MCAST_ADDRESS = "233.2.3.3";
final static String INTERFACE_ADDRESS = "127.0.0.1";
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelServer: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
SocketAddress dst = new InetSocketAddress(group, PORT);
int i = 0;
while (true) {
System.out.println("Transmitting #" + i);
ByteBuffer asterixBuf = ByteBuffer.wrap(
("line " + i + "123456789012345678901234567890123456789012345678901234567890").getBytes());
dc.send(asterixBuf, dst);
i++;
// Randomly transmit an empty UDP datagram and then sleep.
if (Math.random() > 0.50d) {
System.out.println("Transmitting an empty buffer");
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
dc.send(emptyBuf, dst);
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And then the multicast receiver:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
public class LocalClient {
final static int PORT = LocalServer.PORT;
final static String MCAST_ADDRESS = LocalServer.MCAST_ADDRESS;
final static String CONNECT_HOST = LocalServer.INTERFACE_ADDRESS;
final static String INTERFACE_ADDRESS = CONNECT_HOST;
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelClient: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
MembershipKey key = dc.join(group, ni);
dc.configureBlocking(false);
InetSocketAddress foreignInetAddress = new InetSocketAddress(InetAddress.getByName(CONNECT_HOST), 0);
dc.connect(foreignInetAddress);
ByteBuffer receiveBuffer = ByteBuffer.allocate(64 * 1024);
while (true) {
dc.receive(receiveBuffer);
int bytesRead = dc.read(receiveBuffer);
if (bytesRead == -1) {
System.out.println("bytesRead=" + bytesRead);
}
else if (bytesRead > 0) {
receiveBuffer.flip();
System.out.println();
while (receiveBuffer.hasRemaining())
System.out.print((char) receiveBuffer.get());
System.out.println();
receiveBuffer.clear();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
java udp nio multicast
add a comment |
I am using a connected DatagramChannel in non-blocking mode to receive UDP multicast. I have found that DatagramChannel.read(ByteBuffer)
returns -1 whenever my program receive an empty UDP multicast message. But the API documentation says: "The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream".
I would expect end-of-stream to be a condition upon which the channel can no longer be used - just like a closed TCP connection, but it is perfectly possible to keep using the DatagramChannel for reception after getting the -1.
Is it safe to assume that it is always possible to continue using a DatagramChannel even after read(ByteBuffer)
has returned -1?
Is it a little strange to have a return value indicating end-of-stream when all that has happened is the reception of an empty UDP message? Wouldn't it be more appropriate if the return value was 0?
Here are my test programs, first the multicast transmitter:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class LocalServer {
final static int PORT = 7001;
final static String MCAST_ADDRESS = "233.2.3.3";
final static String INTERFACE_ADDRESS = "127.0.0.1";
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelServer: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
SocketAddress dst = new InetSocketAddress(group, PORT);
int i = 0;
while (true) {
System.out.println("Transmitting #" + i);
ByteBuffer asterixBuf = ByteBuffer.wrap(
("line " + i + "123456789012345678901234567890123456789012345678901234567890").getBytes());
dc.send(asterixBuf, dst);
i++;
// Randomly transmit an empty UDP datagram and then sleep.
if (Math.random() > 0.50d) {
System.out.println("Transmitting an empty buffer");
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
dc.send(emptyBuf, dst);
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And then the multicast receiver:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
public class LocalClient {
final static int PORT = LocalServer.PORT;
final static String MCAST_ADDRESS = LocalServer.MCAST_ADDRESS;
final static String CONNECT_HOST = LocalServer.INTERFACE_ADDRESS;
final static String INTERFACE_ADDRESS = CONNECT_HOST;
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelClient: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
MembershipKey key = dc.join(group, ni);
dc.configureBlocking(false);
InetSocketAddress foreignInetAddress = new InetSocketAddress(InetAddress.getByName(CONNECT_HOST), 0);
dc.connect(foreignInetAddress);
ByteBuffer receiveBuffer = ByteBuffer.allocate(64 * 1024);
while (true) {
dc.receive(receiveBuffer);
int bytesRead = dc.read(receiveBuffer);
if (bytesRead == -1) {
System.out.println("bytesRead=" + bytesRead);
}
else if (bytesRead > 0) {
receiveBuffer.flip();
System.out.println();
while (receiveBuffer.hasRemaining())
System.out.print((char) receiveBuffer.get());
System.out.println();
receiveBuffer.clear();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
java udp nio multicast
I am using a connected DatagramChannel in non-blocking mode to receive UDP multicast. I have found that DatagramChannel.read(ByteBuffer)
returns -1 whenever my program receive an empty UDP multicast message. But the API documentation says: "The number of bytes read, possibly zero, or -1 if the channel has reached end-of-stream".
I would expect end-of-stream to be a condition upon which the channel can no longer be used - just like a closed TCP connection, but it is perfectly possible to keep using the DatagramChannel for reception after getting the -1.
Is it safe to assume that it is always possible to continue using a DatagramChannel even after read(ByteBuffer)
has returned -1?
Is it a little strange to have a return value indicating end-of-stream when all that has happened is the reception of an empty UDP message? Wouldn't it be more appropriate if the return value was 0?
Here are my test programs, first the multicast transmitter:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.SocketAddress;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
public class LocalServer {
final static int PORT = 7001;
final static String MCAST_ADDRESS = "233.2.3.3";
final static String INTERFACE_ADDRESS = "127.0.0.1";
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelServer: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
SocketAddress dst = new InetSocketAddress(group, PORT);
int i = 0;
while (true) {
System.out.println("Transmitting #" + i);
ByteBuffer asterixBuf = ByteBuffer.wrap(
("line " + i + "123456789012345678901234567890123456789012345678901234567890").getBytes());
dc.send(asterixBuf, dst);
i++;
// Randomly transmit an empty UDP datagram and then sleep.
if (Math.random() > 0.50d) {
System.out.println("Transmitting an empty buffer");
ByteBuffer emptyBuf = ByteBuffer.allocate(0);
dc.send(emptyBuf, dst);
try {
Thread.sleep(300);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
And then the multicast receiver:
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.NetworkInterface;
import java.net.StandardProtocolFamily;
import java.net.StandardSocketOptions;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.MembershipKey;
public class LocalClient {
final static int PORT = LocalServer.PORT;
final static String MCAST_ADDRESS = LocalServer.MCAST_ADDRESS;
final static String CONNECT_HOST = LocalServer.INTERFACE_ADDRESS;
final static String INTERFACE_ADDRESS = CONNECT_HOST;
public static void main(String args) throws IOException {
NetworkInterface ni = NetworkInterface.getByInetAddress(InetAddress.getByName(INTERFACE_ADDRESS));
System.out.println("ChannelClient: ni=" + ni.getDisplayName());
DatagramChannel dc = DatagramChannel.open(StandardProtocolFamily.INET).setOption(StandardSocketOptions.SO_REUSEADDR, true).bind(
new InetSocketAddress(PORT)).setOption(StandardSocketOptions.IP_MULTICAST_IF, ni);
InetAddress group = InetAddress.getByName(MCAST_ADDRESS);
MembershipKey key = dc.join(group, ni);
dc.configureBlocking(false);
InetSocketAddress foreignInetAddress = new InetSocketAddress(InetAddress.getByName(CONNECT_HOST), 0);
dc.connect(foreignInetAddress);
ByteBuffer receiveBuffer = ByteBuffer.allocate(64 * 1024);
while (true) {
dc.receive(receiveBuffer);
int bytesRead = dc.read(receiveBuffer);
if (bytesRead == -1) {
System.out.println("bytesRead=" + bytesRead);
}
else if (bytesRead > 0) {
receiveBuffer.flip();
System.out.println();
while (receiveBuffer.hasRemaining())
System.out.print((char) receiveBuffer.get());
System.out.println();
receiveBuffer.clear();
try {
Thread.sleep(100);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
java udp nio multicast
java udp nio multicast
asked Nov 24 '18 at 13:20
user10698994user10698994
11
11
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458559%2fdatagramchannel-read-returns-1-when-receiving-empty-udp-multicast%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458559%2fdatagramchannel-read-returns-1-when-receiving-empty-udp-multicast%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown