is a game server written in java,netty, which is often requested by crawlers.
at present, after one night, the netty is stuck and the new request will remain stuck, that is, it will not be printed on the console or respond to the client request.
sometimes I press enter on the cmd of the server and respond to the client request. But this method obviously doesn"t work.
do I turn off ChannelHandlerContext chc.close ();
so that the connection cannot be disconnected, causing the thread to be full?
//
protected static final int BisGroupSize = 6;
//
protected static final int worGroupSize = 10;
the red box is output after I press enter, not according to the request of the client before enter.
//SocketControl
public static void run(int port) throws Exception {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup);
bootstrap.channel(NioServerSocketChannel.class);
bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
// ("\n")
pipeline.addLast("framer", new DelimiterBasedFrameDecoder(10240, Delimiters.lineDelimiter()));
pipeline.addLast("decoder", new MyStringDecoder(Charset.forName("UTF-8")));
pipeline.addLast("encoder", new StringEncoder(Charset.forName("UTF-8")));
pipeline.addLast(new SocketServerHandler());
}
});
bootstrap.bind(IP,port).sync();
log.info("Socket, "+port+" ");
}
public class MyStringDecoder extends MessageToMessageDecoder<ByteBuf> {
private final Charset charset;
/**
* Creates a new instance with the current system character set.
*/
public MyStringDecoder() {
this(Charset.defaultCharset());
}
/**
* Creates a new instance with the specified character set.
*/
public MyStringDecoder(Charset charset) {
if (charset == null) {
throw new NullPointerException("charset");
}
this.charset = charset;
}
@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
if(msg.getByte(0)!="{" || msg.getByte(msg.capacity()-1)!="}"){
}else{
out.add(msg.toString(charset));
}
}
}
public class SocketServerHandler extends SimpleChannelInboundHandler<String> {
private static final Logger log = Logger.getLogger("ruanxin");
@Override
public void exceptionCaught(ChannelHandlerContext chc, Throwable cause) throws Exception {
log.info(cause.getMessage());
chc.close();
}
@SuppressWarnings("unchecked")
@Override
public void channelRead(ChannelHandlerContext chc, Object data) throws Exception {
if(data==null || !(data instanceof String)){
log.info("");
chc.close();
return;
}
String dataStr=(String) data;
log.info("|||" + dataStr+"|||");
if(!dataStr.startsWith("{") || !dataStr.endsWith("}")){
log.info(":data=" + dataStr);
chc.close();
return;
}
log.info("Read:data=" + data);
//...
}
}