You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.5 KiB

// 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<TcpSocketChannel>()
.Option(ChannelOption.TcpNodelay, true)
.Handler(new ActionChannelInitializer<ISocketChannel>(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<IChannel> task = bootstrap.ConnectAsync("127.0.0.1", 9998);
IChannel channel = task.Result;
channel.WriteAndFlushAsync("1111111111");
}
}