简体   繁体   English

java.net.Socket中的Apache Mina Server和客户端

[英]Apache Mina Server and client in java.net.Socket

My application send data to Apache Mina Server which listens with the following configuration.. 我的应用程序将数据发送到Apache Mina Server,该服务器使用以下配置进行侦听。


        IoAcceptor acceptor = new NioSocketAcceptor();
        acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
        //acceptor.getFilterChain().addLast( "logger1", new TempFilter());
        acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
        acceptor.setHandler( new TimeServerHandler() );
        acceptor.getSessionConfig().setReadBufferSize( 2048 );
        acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 10 );
        acceptor.bind( new InetSocketAddress(PORT) );

Here's my Client code written in net.Socket 这是我用net.Socket编写的客户端代码


OutputStream oStrm = socket.getOutputStream();
byte[] byteSendBuffer = (requests[clientNo][j]).getBytes(Charset.forName("UTF-8"));


oStrm.write(byteSendBuffer);
oStrm.flush();

Although the logger show message recieved, the server handler's messageRecieved() is never called.. Please hlp. 虽然记录器显示消息已收到, messageRecieved()不调用服务器处理程序的messageRecieved() ..请hlp。

Try this: 尝试这个:

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;

public class JavaNetClient {

    public static void main(String[] args) throws IOException {

        Charset charset = Charset.forName("UTF-8");
        CharsetEncoder encoder = charset.newEncoder();

        SocketChannel socketChannel = SocketChannel.open(new InetSocketAddress(
                        "localhost", 1071));
        socketChannel.configureBlocking(false);
        CharBuffer charBuffer = CharBuffer.wrap("Hi\r\n");
        ByteBuffer buf = encoder.encode(charBuffer);
        socketChannel.write(buf);

        socketChannel.close();

    }
}

You are using TextLineCodecFactory as a protocol codec which expects your messages to end with line delimeter. 您正在使用TextLineCodecFactory作为协议编解码器,期望您的消息以行分隔符结束。 That is "\\n" on unix, "\\r\\n" on windows which can be get by System.lineSeparator() on Java. 这是unix上的“\\ n”,Windows上的“\\ r \\ n”,可以通过Java上的System.lineSeparator()获取。

Of course TextLineCodecFactory usability depends on your messages' content. 当然TextLineCodecFactory的可用性取决于你的消息的内容。 If your message includes line delimeter character in its content, you can not use TextLineCodecFactory. 如果您的消息在其内容中包含行分隔符,则不能使用TextLineCodecFactory。 In that case you may want to implement your own codec factory that use special character as delimeter ,fixed sized messages or type-length-value structure. 在这种情况下,您可能希望实现自己的编解码器工厂,该工厂使用特殊字符作为分隔符,固定大小的消息或类型长度值结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM