0

テストで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");
  }
}

public class SmsServer は IoHandler を実装します{

public void initUDPServer(){
    NioDatagramAcceptor acceptor = new NioDatagramAcceptor();
    DatagramSessionConfig dcfg = acceptor.getSessionConfig();
    try {
        acceptor.setHandler(this);
        acceptor.bind(new InetSocketAddress(8080));
        DefaultIoFilterChainBuilder chain = acceptor.getFilterChain();
        chain.addLast("encode", new ProtocolCodecFilter(
                new TextLineCodecFactory(Charset.forName("UTF-8"))
                ));
        chain.addLast("logger", new LoggingFilter());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
public void sessionCreated(IoSession session) throws Exception {
    SocketAddress remoteAddress = session.getRemoteAddress();
}


public void messageReceived(IoSession session, Object message)
        throws Exception {
    SocketAddress remoteAddress = session.getRemoteAddress();

    System.out.println(message);
    if (message instanceof IoBuffer) {
        IoBuffer buffer = (IoBuffer) message;
        String sendContent = "hello";
        byte[]b = sendContent.getBytes(Charset.forName("utf-8"));
        IoBuffer ioBuffer = IoBuffer.allocate(sendContent.length(),false);
        ioBuffer.put(b);
        ioBuffer.flip();

        session.write(ioBuffer, remoteAddress);
    }

}
public static void main(String[] args) {
     new SmsServer().initUDPServer();
  }

}

間違ったコードの場所を見つけるのを手伝ってもらえますか? あなたの提案と助けに感謝します!

4

1 に答える 1

0

この例を機能させるには、SmsServer クラスのメソッド initUDPServer() から ProtocolCodecFilter 行を削除します。

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

SmsClient クラスのコンストラクターから ProtocolCodecFilter 行を削除します。

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

この変更により、クライアント ログは次のようになります。

2015-10-18 19:01:26,021 0 [NioProcessor-2] INFO LoggingFilter - 作成済み

2015-10-18 19:01:26,022 1 [NioProcessor-2] INFO LoggingFilter - OPENED

2015-10-18 19:01:26,023 2 [メイン] 情報 SMS - K&18610413435&hiii

2015-10-18 19:01:26,031 10 [NioProcessor-2] INFO LoggingFilter - SENT: HeapBuffer[pos=0 lim=18 cap=18: 4B 26 31 38 36 31 30 34 31 33 34 33 35 26 68 69。 ..]

2015-10-18 19:01:26,052 31 [NioProcessor-2] INFO LoggingFilter - 受信: HeapBuffer[pos=0 lim=5 cap=2048: 68 65 6C 6C 6F] messageReceived........ .......HeapBuffer[pos=0 lim=5 cap=2048: 68 65 6C 6C 6F]

サーバーログは次のとおりです。

2015-10-18 19:01:26,046 0 [NioDatagramAcceptor-1] INFO LoggingFilter - 作成済み

2015-10-18 19:01:26,048 2 [NioDatagramAcceptor-1] INFO LoggingFilter - OPENED

2015-10-18 19:01:26,049 3 [NioDatagramAcceptor-1] INFO LoggingFilter - 受信: HeapBuffer[pos=0 lim=18 cap=2048: 4B 26 31 38 36 31 30 34 31 33 34 33 35 26 68 69。 ..] HeapBuffer[pos=0 lim=18 cap=2048: 4B 26 31 38 36 31 30 34 31 33 34 33 35 26 68 69...]

2015-10-18 19:01:26,051 5 [NioDatagramAcceptor-1] INFO LoggingFilter - 送信: HeapBuffer[pos=0 lim=5 cap=5: 68 65 6C 6C 6F] 2015-10-18 19:02:26,053 60007 [ExpiringMapExpirer-1] INFO LoggingFilter - 閉鎖

于 2015-10-18T07:12:28.897 に答える