UDP problem

I tried to handle UDP connections exactly as TCP connections in my thread, and I got exceptions:

java.net.PortUnreachableException
        at sun.nio.ch.DatagramDispatcher.read0(Native Method)
        at sun.nio.ch.DatagramDispatcher.read(DatagramDispatcher.java:25)
        at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:233)
        at sun.nio.ch.IOUtil.read(IOUtil.java:206)
        at sun.nio.ch.DatagramChannelImpl.read(DatagramChannelImpl.java:312)
        at OverConn.OverChannel.read(OverChannel.java:75)
        at OverConn.OverConn.run(OverConn.java:42)

This is the code that creates the DatagramChannel:

case UDP: {
                
                channel = DatagramChannel.open();
                ((DatagramChannel)channel).connect(new InetSocketAddress(host, port));
                ((DatagramChannel)channel).configureBlocking(false);
                break;
            }

And then I register it at a appropriate time using:

case UDP: key = ((DatagramChannel)channel).register(selector, SelectionKey. OP_READ| SelectionKey. OP_WRITE); break;

Then every cycle I read it and decrypt exactly as any TCP stream using read(buffer);

Any idea?

Silly question here, but have you opened a datagram port on the other end?

Kev

Should that matter? I mean, UDP is connectionless, so this would only allow me to fire off packets without knowing if the port on the other end is open?

I think I found the problem, this is probably the way to do it, right? :slight_smile:

case UDP: {
                
                channel = DatagramChannel.open();
                ((DatagramChannel)channel).socket().bind(new InetSocketAddress("localhost", 10000));
                ((DatagramChannel)channel).connect(new InetSocketAddress(host, port));
                ((DatagramChannel)channel).configureBlocking(false);
                break;
            }

I’m not sure tbh. However, I guess there is some ICMP stuff that goes across first and tells you if the port is open at all.

Best bet would be just to try it, you’ll get an answer pretty quickly that way :wink:

Kev

Did that above actually stop you getting this exception? You’ve just given the datagram socket a fixed local address, this shouldn’t really have stopped the port unreachable exception.

From the PortUnreachableException java doc:

I imply from that a packet reached the remote end but there was no port open on the other end to recieve it on, so an ICMP message gets sent back indicating this.

Kev

Yeah the exception disappeared when I did that… Strange :slight_smile:

Oh well, as long as you’re up and running again :slight_smile:

Kev