// See https://aka.ms/new-console-template for more information using DotNetty.Codecs; using DotNetty.Common.Internal.Logging; using DotNetty.Handlers.Logging; using DotNetty.Handlers.Timeout; using DotNetty.Transport.Bootstrapping; using DotNetty.Transport.Channels; using DotNetty.Transport.Channels.Sockets; using log4net.Config; using Microsoft.Extensions.Logging; using LogLevel = DotNetty.Handlers.Logging.LogLevel; internal class Program { public static void Main(string[] args) { XmlConfigurator.ConfigureAndWatch(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + @"\log4net.xml")); InternalLoggerFactory.DefaultFactory.AddProvider(new Log4NetProvider()); Bootstrap bootstrap = new Bootstrap(); bootstrap .Group(new MultithreadEventLoopGroup()) .Channel() .Option(ChannelOption.TcpNodelay, true) .Handler(new ActionChannelInitializer(channel => { var pipeline = channel.Pipeline; pipeline.AddLast(new StringDecoder()); pipeline.AddLast(new StringEncoder()); // 监听器 pipeline.AddLast(new LoggingHandler(LogLevel.TRACE)); pipeline.AddLast("idleStateHandler", new IdleStateHandler(30, 0, 0)); // 触发读取超时 })); Task task = bootstrap.ConnectAsync("127.0.0.1", 9998); IChannel channel = task.Result; channel.WriteAndFlushAsync("1111111111"); } }