Java Apache Mina TCPServer无法读取长字符串文本
我尝试使用ApacheMina进行TCP/IP通信。 在我的情况下,我必须发送长文本消息,如:Java Apache Mina TCPServer无法读取长字符串文本,java,serversocket,tcp-ip,apache-mina,Java,Serversocket,Tcp Ip,Apache Mina,我尝试使用ApacheMina进行TCP/IP通信。 在我的情况下,我必须发送长文本消息,如: 0400F23A40010881800200000040000000001623334444555566665000990000005670000725090909999999090909072507306011040001ab9999999999ab9999999999999028000008389999999900000056700036003110000002001000010725090909
0400F23A40010881800200000040000000001623334444555566665000990000005670000725090909999999090909072507306011040001ab9999999999ab9999999999999028000008389999999900000056700036003110000002001000010725090909000000000000000000
但当我尝试发送它时,我的TCP服务器无法在控制台中显示消息。
这是我的服务器代码:
public class TCPServer {
private static final int PORT = 3092;
public static void main(String[] args) throws IOException {
IoAcceptor acceptor = new NioSocketAcceptor();
acceptor.getFilterChain().addLast("logger", new LoggingFilter());
// acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory(Charset.forName("UTF-8"))));
//
// Set handler
acceptor.setHandler(new TimeServerHandler());
// Set config
acceptor.getSessionConfig().setMaxReadBufferSize(1048576);
acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);
// Bind port
acceptor.bind(new InetSocketAddress(PORT));
}
}
这是我的经纪人
public class TimeServerHandler extends IoHandlerAdapter {
private static final CharsetEncoder ENCODER = Charset.forName("UTF-8").newEncoder();
public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
cause.printStackTrace();
}
public void messageReceived(IoSession session, Object message) throws Exception {
String msg = message.toString();
if (message instanceof IoBuffer) {
IoBuffer ioBuffer = IoBuffer.allocate(msg.length(), false).setAutoExpand(true);
System.out.println(ioBuffer.capacity());
}
if (msg.trim().equalsIgnoreCase("quit")) {
session.close(true);
return;
}
}
public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
}
}
我只是想打印出容量,但服务器无法读取消息
如何解决这个问题?
谢谢您必须将其转换为字符串
if(ioBuffer.isDirect())
{
byte[] dst = new byte[ioBuffer.capacity()]
ioBuffer.get(dst);
System.out.println(new String(dst, "UTF-8"))
}
else
{
System.out.println(new String(ioBuffer.array(), "UTF-8"))
}