Apache Mina UDP Client: java.net.PortUnreachableException

933 views Asked by At

There seems to be no documentation anywhere about the java.net.PortUnreachableException.

I've made a simple Apache Mina UDP Server and ran it. I then used telnet 127.0.0.1 3450 to check whether it was listening, and the connection was not refused, so the server definitely works.

The client consisits of the following files.

This is the main class below:

public static void main(String[] args) {
    System.out.println("started");
    NioDatagramConnector connector = new NioDatagramConnector();

    connector.setHandler(new PacketHandler());
    ConnectFuture cf = connector.connect(new InetSocketAddress("127.0.0.1", 3450));
}

This is part of the PacketHandler class:

@Override
public void sessionOpened(IoSession s) {
    System.out.println("opened " + s.getId() + " " + s.getLocalAddress().toString() + " " + s.getRemoteAddress().toString());
    byte[] data = new byte[]{(byte) 0x01};
    System.out.println("Sending " + data.length);
    IoBuffer buffer = IoBuffer.allocate(data.length);
    buffer.put(data);
    buffer.flip();
    s.write(buffer);
}

When the client is ran, I get the following error:

started
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
created 1
opened 1 /127.0.0.1:56777 /127.0.0.1:3450
Sending 1
java.net.PortUnreachableException
    at sun.nio.ch.DatagramDispatcher.read0(Native Method)
    at sun.nio.ch.DatagramDispatcher.read(DatagramDispatcher.java:43)
    at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:223)
    at sun.nio.ch.IOUtil.read(IOUtil.java:197)
    at sun.nio.ch.DatagramChannelImpl.read(DatagramChannelImpl.java:560)
    at org.apache.mina.transport.socket.nio.NioProcessor.read(NioProcessor.java:311)
    at org.apache.mina.transport.socket.nio.NioProcessor.read(NioProcessor.java:45)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.read(AbstractPollingIoProcessor.java:702)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:668)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.process(AbstractPollingIoProcessor.java:657)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor.access$600(AbstractPollingIoProcessor.java:67)
    at org.apache.mina.core.polling.AbstractPollingIoProcessor$Processor.run(AbstractPollingIoProcessor.java:1121)
    at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnable.java:64)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:744)
closed 1

The server also logs connections and when the client is ran, no connections are logged. However, when the telnet 127.0.0.1 3450 is ran from my Terminal, a connection is logged in the server logs. So apparently, the client isn't actually connecting.

1

There are 1 answers

1
zks On

Telnet can't connect to UDP. You are therefore testing a TCP server with Telnet, which proves nothing.

check the code,you can paste it here.