Apache mina SocketAcceptor和java.net.Socket通信
我正在为Apache Mina Tcp服务器和java.net.Socket客户端编写一个网络程序 服务器代码Apache mina SocketAcceptor和java.net.Socket通信,java,sockets,tcp,communication,mina,Java,Sockets,Tcp,Communication,Mina,我正在为Apache Mina Tcp服务器和java.net.Socket客户端编写一个网络程序 服务器代码 SimpleIoProcessorPool<NioSession> pool = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, 20); SocketAcceptor acceptor = new NioSocketAcceptor(pool); acceptor.s
SimpleIoProcessorPool<NioSession> pool = new SimpleIoProcessorPool<NioSession>(NioProcessor.class, 20);
SocketAcceptor acceptor = new NioSocketAcceptor(pool);
acceptor.setReuseAddress(true);
// Add two filters : a logger and a codec
acceptor.getFilterChain().addLast( "logger", new LoggingFilter() );
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter( new TextLineCodecFactory( Charset.forName( "UTF-8" ))));
acceptor.setHandler( new TimeServerHandler() );
// Configurate the buffer size and the iddle time
acceptor.getSessionConfig().setReadBufferSize( 2048 );
acceptor.getSessionConfig().setIdleTime( IdleStatus.BOTH_IDLE, 1 );
acceptor.bind( new InetSocketAddress(8001) );
}
我的套接字客户端是
InetAddress inetAddress = InetAddress.getByName("localhost");
Socket socket = new Socket(inetAddress, 8001);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
bufferedWriter.write("Test");
bufferedWriter.flush();
System.out.println(bufferedReader.readLine());
System.out.println("Done....");
bufferedWriter.close();
bufferedReader.close();
问题是服务器套接字无法与java.net.socket通信。
有人能说出什么是错误的吗?在服务器代码中听到..替换为此 LineDelimiter(“$”)“$”是解码定界符
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.DEFAULT, new LineDelimiter("$"))));
检查此链接
acceptor.getFilterChain().addLast( "codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"), LineDelimiter.DEFAULT, new LineDelimiter("$"))));