Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java udp服务器不接受带有mina的消息_Java_Sockets_Mina - Fatal编程技术网

Java udp服务器不接受带有mina的消息

Java udp服务器不接受带有mina的消息,java,sockets,mina,Java,Sockets,Mina,我在测试中使用mina udp,服务器和客户端都在同一台计算机上,客户端和服务器运行时不会抛出异常,但服务器无法接收消息。 代码是 public class SmsClient extends IoHandlerAdapter { private final static Logger logger = LoggerFactory.getLogger("sms"); private IoSession session; private IoConnector con

我在测试中使用mina udp,服务器和客户端都在同一台计算机上,客户端和服务器运行时不会抛出异常,但服务器无法接收消息。 代码是

public class SmsClient extends IoHandlerAdapter {

    private final static Logger logger = LoggerFactory.getLogger("sms");

    private IoSession session;

    private IoConnector connector;

    public SmsClient(final String phone, final String content) {
        connector = new NioDatagramConnector();
        DefaultIoFilterChainBuilder chain = connector.getFilterChain();
        chain.addLast("myChin", new ProtocolCodecFilter(
                new TextLineCodecFactory(Charset.forName("UTF-8"))));
        chain.addLast("logger", new LoggingFilter());
        connector.setHandler(this);

         String ip = "127.0.0.1";
        String port = "8080";


        ConnectFuture connFuture = connector.connect(new InetSocketAddress(ip,
                Integer.valueOf(port)));

        connFuture.awaitUninterruptibly();
        connFuture.addListener(new IoFutureListener<ConnectFuture>() {
            public void operationComplete(ConnectFuture future) {
                if (future.isConnected()) {
                    session = future.getSession();
                    try {
                        sendData(phone, content);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    try {
                        throw new Exception("connect failed....");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }

    private void sendData(String phone, String content)
            throws InterruptedException {


        String s = "K&" + phone + "&" + content;


        logger.info(s);

        Charset c = Charset.forName("utf-8");

        byte[] b = s.getBytes(c);
        IoBuffer buffer = IoBuffer.allocate(b.length, false);
        buffer.put(b);
        buffer.flip();
        session.write(buffer);
    }

    @Override
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        cause.printStackTrace();
        System.out.println("exceptionCaught.................");
        session.close(true);
    }

    @Override
    public void messageReceived(IoSession session, Object message)
            throws Exception {
        System.out.println("messageReceived................."+message);

    }

  public static void main(String[] args) {
    new SmsClient("18610413435", "hiii");
  }
}
}

你能帮我找出哪里的密码错了吗?
谢谢你的建议和帮助

要使此示例正常工作,请从SmsServer类的方法initUDPServer中删除ProtocolCodeFilter行:

        chain.addLast("encode", new ProtocolCodecFilter(
            new TextLineCodecFactory(Charset.forName("UTF-8"))
            ));
并从SmsClient类的构造函数中删除ProtocolCodeFilter行:

    chain.addLast("myChin", new ProtocolCodecFilter(
            new TextLineCodecFactory(Charset.forName("UTF-8"))));
通过此修改,以下是客户端日志:

2015-10-18 19:01:26021 0[NioProcessor-2]信息记录过滤器-已创建

2015-10-18 19:01:26022 1[NioProcessor-2]信息记录过滤器-打开

2015-10-18 19:01:26023 2[主要]信息短信-K&18610413435&hiii

2015-10-18 19:01:26031 10[NioProcessor-2]信息记录过滤器-发送:HeapBuffer[pos=0 lim=18 cap=18:4B 26 31 38 36 31 33 35 26 68 69…]

2015-10-18 19:01:26052 31[NioProcessor-2]信息记录过滤器-收到:HeapBuffer[pos=0 lim=5 cap=2048:68 65 6C 6C 6F] 收到的消息……….HeapBuffer[pos=0 lim=5 cap=2048:68 65 6C 6C 6F]

这是服务器日志:

2015-10-18 19:01:26046 0[NioDatagramAcceptor-1]信息记录过滤器-已创建

2015-10-18 19:01:26048 2[NioDatagramAcceptor-1]信息记录过滤器-打开

2015-10-18 19:01:26049 3[NioDatagramAcceptor-1]信息记录过滤器-收到:HeapBuffer[pos=0 lim=18 cap=2048:4B 26 31 38 36 30 33 26 68 69] 堆垫[pos=0 lim=18 cap=2048:4B 26 31 38 36 31 34 33 35 26 68 69…]

2015-10-18 19:01:260515[NioDatagramAcceptor-1]信息记录过滤器-发送:HeapBuffer[pos=0 lim=5 cap=5:68 65 6C 6C 6F] 2015-10-18 19:02:26053 60007[ExpiringMapExpirer-1]信息记录过滤器-关闭

    chain.addLast("myChin", new ProtocolCodecFilter(
            new TextLineCodecFactory(Charset.forName("UTF-8"))));