Selector.select() won't work on MacOS X (JVM 1.6.0_05)

S

Sebastian Staudt

Hello.

I have a problem with a Selector.select() call.
I'm using a DatagramChannel for server queries (i.e. client sends
request, server sends response).
After sending a request, I wait for the response with select(). The
DatagramChannel is registered with OP_READ.
Everything works fine on Linux and Windows, but on MacOS X select()
always returns 0. select() always times out or blocks infinetly
(depending on timeout argument). Wireshark shows that the request goes
out and the response is received successfully, but the Selector won't
notice.

Thanks for your help.
 
O

Owen Jacobson

Hello.

I have a problem with a Selector.select() call.
I'm using a DatagramChannel for server queries (i.e. client sends
request, server sends response).
After sending a request, I wait for the response with select(). The
DatagramChannel is registered with OP_READ.
Everything works fine on Linux and Windows, but on MacOS X select()
always returns 0. select() always times out or blocks infinetly
(depending on timeout argument). Wireshark shows that the request goes
out and the response is received successfully, but the Selector won't
notice.

Thanks for your help.

Without seeing the code, it's going to be very difficult to offer a
useful suggestion. A demo program, included below, does not
demonstrate the bug.

-----
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Set;

public class DatagramSelectDemo {
public static void main(String[] args) throws IOException {
Selector s = Selector.open();
try {
DatagramChannel dgc = DatagramChannel.open();
try {
dgc.configureBlocking(false);
dgc.socket().bind(new InetSocketAddress(2600));
dgc.register(s, SelectionKey.OP_READ);

while (true) {
s.select();

Set<SelectionKey> keys = s.selectedKeys();
for (Iterator<SelectionKey> i = keys.iterator();
i.hasNext();) {
SelectionKey key = i.next();

if (key.isReadable()) {
SelectableChannel channel = key.channel();
if (channel == dgc) {
ByteBuffer buffer =
ByteBuffer.allocate(512);
SocketAddress source =
dgc.receive(buffer);
System.out.printf(
"Received %d bytes from %s\n",
buffer.position(), source);
}
}
i.remove();
}
}

} finally {
dgc.close();
}
} finally {
s.close();
}
}
}
-----

Sending messages to this with nc -u hostname 2600 triggers appropriate
"Received 16 bytes from /192.168.10.5:61862" messages.

-o
 
S

Sebastian Staudt

I have a problem with a Selector.select() call.
I'm using a DatagramChannel for server queries (i.e. client sends
request, server sends response).
After sending a request, I wait for the response with select(). The
DatagramChannel is registered with OP_READ.
Everything works fine on Linux and Windows, but on MacOS X select()
always returns 0. select() always times out or blocks infinetly
(depending on timeout argument). Wireshark shows that the request goes
out and the response is received successfully, but the Selector won't
notice.
Thanks for your help.

Without seeing the code, it's going to be very difficult to offer a
useful suggestion.  A demo program, included below, does not
demonstrate the bug.

-----
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Set;

public class DatagramSelectDemo {
    public static void main(String[] args) throws IOException {
        Selector s = Selector.open();
        try {
            DatagramChannel dgc = DatagramChannel.open();
            try {
                dgc.configureBlocking(false);
                dgc.socket().bind(new InetSocketAddress(2600));
                dgc.register(s, SelectionKey.OP_READ);

                while (true) {
                    s.select();

                    Set<SelectionKey> keys = s.selectedKeys();
                    for (Iterator<SelectionKey> i = keys.iterator();
i.hasNext();) {
                        SelectionKey key = i.next();

                        if (key.isReadable()) {
                            SelectableChannel channel = key.channel();
                            if (channel == dgc) {
                                ByteBuffer buffer =
ByteBuffer.allocate(512);
                                SocketAddress source =
dgc.receive(buffer);
                                System.out.printf(
                                        "Received %d bytes from %s\n",
                                        buffer.position(), source);
                            }
                        }
                        i.remove();
                    }
                }

            } finally {
                dgc.close();
            }
        } finally {
            s.close();
        }
    }}

-----

Sending messages to this with nc -u hostname 2600 triggers appropriate
"Received 16 bytes from /192.168.10.5:61862" messages.

-o

I forgot to mention that this is happening on the client socket. So
your code doesn't exactly match my problem.

Here's a short version of my code:

public class Test
{
public void main(String[] argv)
throws Exception
{
DatagramChannel dc = DatagramChannel.open();
dc.configureBlocking(false);
dc.connect(new InetSocketAddress(SOME_IP, SOME_PORT);

ByteBuffer bb = ByteBuffer.wrap("test".getBytes());
dc.write(bb);

Selector selector = Selector.open();
dc.register(selector, SelectionKey.OP_READ);
if(selector.select(1000) == 0)
{
throw new TimeoutException();
}

bb = ByteBuffer.allocate(1500);
dc.read(bb);

/* MORE CODE HERE */
}
}

This code always bails out with a TimeoutException on MacOS X.
An additional note: Commenting out the Selector stuff results in
dc.read() to block forever.
So maybe it's not a Selector problem, but a DatagramSocket problem.
 
S

Sebastian Staudt

I did more testing and the problem seems to be really hard to
identify.
Like said before, it's not a problem with the DatagramChannel, but
with the DatagramSocket.

Even without using a channel and without using select, the socket
still loses data (aka blocks forever).
Wireshark shows that the data has arrived, but my program is unable to
read it from the socket.

I'm pretty at a loss now, tried every possible code change and JVM
1.5.0 - nothing helps. :(

Without seeing the code, it's going to be very difficult to offer a
useful suggestion.  A demo program, included below, does not
demonstrate the bug.
-----
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Set;
public class DatagramSelectDemo {
    public static void main(String[] args) throws IOException {
        Selector s = Selector.open();
        try {
            DatagramChannel dgc = DatagramChannel.open();
            try {
                dgc.configureBlocking(false);
                dgc.socket().bind(new InetSocketAddress(2600));
                dgc.register(s, SelectionKey.OP_READ);
                while (true) {
                    s.select();
                    Set<SelectionKey> keys = s.selectedKeys();
                    for (Iterator<SelectionKey> i = keys.iterator();
i.hasNext();) {
                        SelectionKey key = i.next();
                        if (key.isReadable()) {
                            SelectableChannel channel = key.channel();
                            if (channel == dgc) {
                                ByteBuffer buffer =
ByteBuffer.allocate(512);
                                SocketAddress source =
dgc.receive(buffer);
                                System.out.printf(
                                        "Received %d bytes from %s\n",
                                        buffer.position(), source);
                            }
                        }
                        i.remove();
                    }
                }
            } finally {
                dgc.close();
            }
        } finally {
            s.close();
        }
    }}

Sending messages to this with nc -u hostname 2600 triggers appropriate
"Received 16 bytes from /192.168.10.5:61862" messages.

I forgot to mention that this is happening on the client socket. So
your code doesn't exactly match my problem.

Here's a short version of my code:

public class Test
{
        public void main(String[] argv)
                throws Exception
        {
                DatagramChannel dc = DatagramChannel.open();
                dc.configureBlocking(false);
                dc.connect(new InetSocketAddress(SOME_IP, SOME_PORT);

                ByteBuffer bb = ByteBuffer.wrap("test".getBytes());
                dc.write(bb);

                Selector selector = Selector.open();
                dc.register(selector, SelectionKey.OP_READ);
                if(selector.select(1000) == 0)
                {
                        throw new TimeoutException();
                }

                bb = ByteBuffer.allocate(1500);
                dc.read(bb);

                /* MORE CODE HERE */
        }

}

This code always bails out with a TimeoutException on MacOS X.
An additional note: Commenting out the Selector stuff results in
dc.read() to block forever.
So maybe it's not a Selector problem, but a DatagramSocket problem.
 
S

Sebastian Staudt

So I finally found something...
Using a connected DatagramSocket does NOT WORK, while using an
unconnected DatagramSocket WORKS.

The problem is, that DatagramChannel doesn't support unconnected
sending. So this shows only the source of the problem and doesn't
serve as a workaround.

Here's my example code, sending the string "test" to an echo server
and receiving the response.
While the first transmit works, the second one will block.

public static void main(String[] argv)
throws Exception
{
String host = "koraktor.de";
int port = 7;
byte[] data = "test".getBytes();

DatagramSocket s = new DatagramSocket();
DatagramPacket pi = new DatagramPacket(new byte[1500], 1500);
DatagramPacket po;
InetSocketAddress sa = new
InetSocketAddress(InetAddress.getByName(host), port);

po = new DatagramPacket(data, data.length, sa);
s.send(po);
System.out.println("Data sent: " + new String(po.getData()));
s.receive(pi);
System.out.println("Data received: " + new String(pi.getData()));

po = new DatagramPacket(data, data.length);
s.connect(sa);
s.send(po);
System.out.println("Data sent: " + new String(po.getData()));
s.receive(pi);
System.out.println("Data received: " + new String(pi.getData()));
}


I did more testing and the problem seems to be really hard to
identify.
Like said before, it's not a problem with the DatagramChannel, but
with the DatagramSocket.

Even without using a channel and without using select, the socket
still loses data (aka blocks forever).
Wireshark shows that the data has arrived, but my program is unable to
read it from the socket.

I'm pretty at a loss now, tried every possible code change and JVM
1.5.0 - nothing helps. :(

Hello.
I have a problem with a Selector.select() call.
I'm using a DatagramChannel for server queries (i.e. client sends
request, server sends response).
After sending a request, I wait for the response with select(). The
DatagramChannel is registered with OP_READ.
Everything works fine on Linux and Windows, but on MacOS X select()
always returns 0. select() always times out or blocks infinetly
(depending on timeout argument). Wireshark shows that the request goes
out and the response is received successfully, but the Selector won't
notice.
Thanks for your help.
Without seeing the code, it's going to be very difficult to offer a
useful suggestion.  A demo program, included below, does not
demonstrate the bug.
-----
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.DatagramChannel;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Iterator;
import java.util.Set;
public class DatagramSelectDemo {
    public static void main(String[] args) throws IOException {
        Selector s = Selector.open();
        try {
            DatagramChannel dgc = DatagramChannel.open();
            try {
                dgc.configureBlocking(false);
                dgc.socket().bind(new InetSocketAddress(2600));
                dgc.register(s, SelectionKey.OP_READ);
                while (true) {
                    s.select();
                    Set<SelectionKey> keys = s.selectedKeys();
                    for (Iterator<SelectionKey> i = keys.iterator();
i.hasNext();) {
                        SelectionKey key = i.next();
                        if (key.isReadable()) {
                            SelectableChannel channel = key.channel();
                            if (channel == dgc) {
                                ByteBuffer buffer =
ByteBuffer.allocate(512);
                                SocketAddress source =
dgc.receive(buffer);
                                System.out.printf(
                                        "Received %d bytes from %s\n",
                                        buffer.position(), source);
                            }
                        }
                        i.remove();
                    }
                }
            } finally {
                dgc.close();
            }
        } finally {
            s.close();
        }
    }}
I forgot to mention that this is happening on the client socket. So
your code doesn't exactly match my problem.
Here's a short version of my code:
public class Test
{
        public void main(String[] argv)
                throws Exception
        {
                DatagramChannel dc = DatagramChannel.open();
                dc.configureBlocking(false);
                dc.connect(new InetSocketAddress(SOME_IP, SOME_PORT);
                ByteBuffer bb = ByteBuffer.wrap("test".getBytes());
                dc.write(bb);
                Selector selector = Selector.open();
                dc.register(selector, SelectionKey.OP_READ);
                if(selector.select(1000) == 0)
                {
                        throw new TimeoutException();
                }
                bb = ByteBuffer.allocate(1500);
                dc.read(bb);
                /* MORE CODE HERE */
        }

This code always bails out with a TimeoutException on MacOS X.
An additional note: Commenting out the Selector stuff results in
dc.read() to block forever.
So maybe it's not a Selector problem, but a DatagramSocket problem.
 
J

John B. Matthews

Sebastian Staudt said:
So I finally found something...
Using a connected DatagramSocket does NOT WORK, while using an
unconnected DatagramSocket WORKS.

The problem is, that DatagramChannel doesn't support unconnected
sending.

I'm confused. I thought, "A datagram channel need not be connected in
order for the send and receive methods to be used."

<http://java.sun.com/javase/6/docs/api/java/nio/channels/DatagramChannel.
html>

I assumed that DatagramChannel#connect() was merely for efficiency, as I
understood UDP packets to be stateless.

<http://en.wikipedia.org/wiki/User_Datagram_Protocol>
 
E

EJP

Sebastian said:
So I finally found something...
Using a connected DatagramSocket does NOT WORK, while using an
unconnected DatagramSocket WORKS.

The problem is, that DatagramChannel doesn't support unconnected
sending.

Err, see DatagramChannel.send(ByteBuffer src, SocketAddress target).
 
S

Sebastian Staudt

Yes sorry... for some reason I overlooked this methods (send and
receive).
Nevertheless connecting a channel should not break it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,268
Latest member
AshliMacin

Latest Threads

Top