12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- using System;
- using System.Text;
- using System.Threading.Tasks;
- using DotNetty.Transport.Bootstrapping;
- using DotNetty.Transport.Channels;
- using DotNetty.Transport.Channels.Sockets;
- using DotNetty.Codecs;
- using System.Net;
- using System.Security.Cryptography.X509Certificates;
- using System.IO;
- using DotNetty.Handlers.Tls;
- using System.Net.Security;
- using System.Threading;
- namespace nettyCommunication
- {
- public class ClientDemo
- {
- public static async Task RunClientAsync()
- {
- var group = new MultithreadEventLoopGroup();
- X509Certificate2 cert = null;
- string targetHost = null;
- if (ClientSettings.IsSsl)
- {
- cert = new X509Certificate2(Path.Combine(ConfigurationTools.ProcessDirectory, "dotnetty.com.pfx"), "password");
- targetHost = cert.GetNameInfo(X509NameType.DnsName, false);
- }
- try
- {
- Bootstrap bootstrap = new Bootstrap();
- bootstrap
- .Group(group)
- .Channel<TcpSocketChannel>()
- .Option(ChannelOption.TcpNodelay, true)
- .Handler(new ActionChannelInitializer<ISocketChannel>(channel =>
- {
- IChannelPipeline pipeline = channel.Pipeline;
- pipeline.AddLast(new ClientDemoHandler());
- }));
- IChannel bootstrapChannel = await bootstrap.ConnectAsync(new IPEndPoint(ClientSettings.Host, ClientSettings.Port));
- Console.WriteLine("连接已建立,已执行完channelActive,如果buffer有数据,然后切换到ChannelRead");
- Console.WriteLine("ClientDemo RunClientAsync()方法中等待输入:切换到ChannelRead.请输入任意字符:");
- //Console.WriteLine("next执行连接关闭");
- await bootstrapChannel.CloseAsync();
- }
- finally
- {
- // Console.WriteLine("连接不能关闭呀");
- //group.ShutdownGracefullyAsync().Wait(1000);
- //await group.ShutdownGracefullyAsync();
- }
- }
-
- }
- }
|