Apache Mina UDP客户端:java.net.PortUnreachableException

时间:2014-11-15 13:28:13

标签: java apache sockets udp apache-mina

似乎没有关于java.net.PortUnreachableException的任何文档。

我制作了一个简单的Apache Mina UDP服务器并运行它。然后我使用telnet 127.0.0.1 3450检查它是否正在侦听,并且连接没有被拒绝,因此服务器肯定能够正常工作。

客户端包含以下文件。

这是下面的主要课程:

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));
}

这是PacketHandler类的一部分:

@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);
}

当客户端运行时,我收到以下错误:

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

服务器还记录连接,当客户端运行时,不记录任何连接。但是,从我的终端运行telnet 127.0.0.1 3450时,服务器日志中会记录一个连接。显然,客户实际上并没有连接。

1 个答案:

答案 0 :(得分:0)

Telnet无法连接到UDP。因此,您正在使用Telnet测试TCP服务器,这证明什么都没有。

检查代码,您可以将其粘贴到此处。