TCP and SSL TCP applications use TCP and SSL TCP

TCP and SSL TCP application

TCP applications written communication is a relatively complex task for the average developer, after all, requires a series of bytes operation; If we believe will allow many developers for the general safety of handling SSL shut out in order to simplify this. problem BeetleXintroduced Streamoperating mode and provides PipiStreamoperational objects allow developers to very simple operation on a TCP stream; because the PipiStream​basic saea extended flow model, it also offers excellent performance while providing easy operation. Here's how to use the BeetleXTCP SSL-based application examples and TCP build.

Reference component

Use components can be referenced by Nuget components, the latest version is1.2.6.8

 

Construction of TCP service

Components to build communication application is very simple, in reference to the component after a simple implementation of IServerHandlerthe interface to, the following services are simple to build the code:

Copy the code
    class Program : BeetleX.ServerHandlerBase
    {
        private static BeetleX.IServer mServer;

        static void Main(string[] args)
        {
            mServer = SocketFactory.CreateTcpServer<Program>();
            mServer.Open();
            Console.WriteLine(mServer.Status);
            Console.Read();
        }

        protected override void OnLogToConsole(IServer server, ServerLogEventArgs e)
        {

            Console.WriteLine($"{DateTime.Now}\t{e.Type}\t{e.Session?.RemoteEndPoint} {e.Message}");
        }

        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            var stream = e.Stream.ToPipeStream();
            if (stream.TryReadLine(out string line))
            {
                Console.WriteLine(line);
                stream.WriteLine($"{DateTime.Now}");
                e.Stream.Flush();
            }
        }
    }
Copy the code

ServerHandlerBaseIt is an internal component implements IServerHandleran interface, simply override SessionReceiveevent to process the data can be received. The above code is an attempt to read a line of characters from the stream of information, displays the contents if the reading is successful and outputs the current time to the client.

Construction of TCP client

Since the client and server use the same set of Streamrealization system, so the client is using the same stream mode operation can be.

Copy the code
            var client = SocketFactory.CreateClient<Clients.TcpClient>("localhost", 9090);
            client.Connect();
            var stream = client.Stream.ToPipeStream();
            while (true)
            {
                string value = Console.ReadLine();
                stream.WriteLine($"{DateTime.Now} {value}");
                client.Stream.Flush();
                client.Receive();
                if (stream.TryReadLine(out string line))
                {
                    Console.WriteLine(line);
                }
            }
Copy the code

running result

 

SSL server

Many times applications require a reliable and secure communications mechanism, default components provide SSLsupport; simply configure what SSLinformation to enable secure SSLcommunications.

Copy the code
    class Program : BeetleX.ServerHandlerBase
    {
        private static BeetleX.IServer mServer;

        static void Main(string[] args)
        {
            mServer = SocketFactory.CreateTcpServer<Program>();
            mServer.Options.DefaultListen.SSL = true;
            mServer.Options.DefaultListen.CertificateFile = "c:\\ikende.com.pfx";
            mServer.Options.DefaultListen.CertificatePassword = "******";
            mServer.Open();
            Console.WriteLine(mServer.Status);
            Console.Read();
        }

        protected override void OnLogToConsole(IServer server, ServerLogEventArgs e)
        {

            Console.WriteLine($"{DateTime.Now}\t{e.Type}\t{e.Session?.RemoteEndPoint} {e.Message}");
        }

        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            var stream = e.Stream.ToPipeStream();
            if (stream.TryReadLine(out string line))
            {
                Console.WriteLine(line);
                stream.WriteLine($"{DateTime.Now}");
                e.Stream.Flush();
            }
        }
    }
Copy the code

SSL客户端

Copy the code
    class Program
    {
        static void Main(string[] args)
        {
            var client = SocketFactory.CreateSslClient<Clients.TcpClient>("localhost", 9090, "ikende.com");
            client.Connect();
            var stream = client.Stream.ToPipeStream();
            while (true)
            {
                string value = Console.ReadLine();
                stream.WriteLine($"{DateTime.Now} {value}");
                client.Stream.Flush();
                client.Receive();
                if (stream.TryReadLine(out string line))
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
Copy the code

在创建客户端的SSL里需要指定证书对应的机构名称,用于进行SSL验证。

运行效果

组件在开启SSL服务的日志输出打印出详细的证书信息,用于方便查看SSL的启动状态

支持平台

组件支持2.1或更高版本的dorecore 和Standard2.0

https://ikende.com/Files/BeetleX.Samples.Echo.zip


专注dotnet core 服务应用技术研发

什么时候使用BeetleX.FastHttpApi

  • 当你需要一个高性能的webapi服务的时候,因为它具有asp.net core mvc的简便使用特性的同时提升了200%或更高的性能。
  • 当你希望接口同时支持http和websocket,组件支持一个逻辑控制器同时提供http和websocket两种服务,即一份代码即可服务http和websocket请求
  • 当你需要一个嵌入的HTTP服务应用,组件支持netstandard2.0,可以灵活地在winform或wpf中集成一个可靠的HTTP服务。

对于普通开发者而言编写TCP应用通讯是一件相对复杂的工作,毕竟需要一系列的bytes操作;如果再针对SSL的安全性处理相信会把很多普通开发者拒之门外.为了简化这一问题BeetleX引入了Stream操作模式并提供PipiStream操作对象让开发者在TCP流操作上变得非常简便;由于PipiStream​是基本saea扩展的流操作模型,因此在提供简便操作的同时还能提供出色的性能。以下介绍如何使用BeetleX构建TCP和基于SSL的TCP应用示例。

引用组件

使用组件可以通过Nuget引用组件,最新版本是1.2.6.8

 

构建TCP服务

组件构建通讯应用非常简单,在引用组件后简单实现IServerHandler接口即可,以下是简单构建的服务代码:

Copy the code
    class Program : BeetleX.ServerHandlerBase
    {
        private static BeetleX.IServer mServer;

        static void Main(string[] args)
        {
            mServer = SocketFactory.CreateTcpServer<Program>();
            mServer.Open();
            Console.WriteLine(mServer.Status);
            Console.Read();
        }

        protected override void OnLogToConsole(IServer server, ServerLogEventArgs e)
        {

            Console.WriteLine($"{DateTime.Now}\t{e.Type}\t{e.Session?.RemoteEndPoint} {e.Message}");
        }

        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            var stream = e.Stream.ToPipeStream();
            if (stream.TryReadLine(out string line))
            {
                Console.WriteLine(line);
                stream.WriteLine($"{DateTime.Now}");
                e.Stream.Flush();
            }
        }
    }
Copy the code

ServerHandlerBase是组件内部实现IServerHandler接口,只需要简单重写SessionReceive事件来处理接收的数据可。以上代码是尝试从流中读取一行字符信息,如果读成功后显示内容并把当前时间输出给客户端。

构建TCP客户端

由于客户端和服务端使用同一套Stream实现体系,所以在客户端上也是使用同样的流模式操作即可.

Copy the code
            var client = SocketFactory.CreateClient<Clients.TcpClient>("localhost", 9090);
            client.Connect();
            var stream = client.Stream.ToPipeStream();
            while (true)
            {
                string value = Console.ReadLine();
                stream.WriteLine($"{DateTime.Now} {value}");
                client.Stream.Flush();
                client.Receive();
                if (stream.TryReadLine(out string line))
                {
                    Console.WriteLine(line);
                }
            }
Copy the code

运行效果

 

SSL服务端

很多时候应用需要一个可靠安全的通讯机制,组件默认提供SSL的支持;只需要简单地配置一下SSL信息即可实现安全的SSL通讯。

Copy the code
    class Program : BeetleX.ServerHandlerBase
    {
        private static BeetleX.IServer mServer;

        static void Main(string[] args)
        {
            mServer = SocketFactory.CreateTcpServer<Program>();
            mServer.Options.DefaultListen.SSL = true;
            mServer.Options.DefaultListen.CertificateFile = "c:\\ikende.com.pfx";
            mServer.Options.DefaultListen.CertificatePassword = "******";
            mServer.Open();
            Console.WriteLine(mServer.Status);
            Console.Read();
        }

        protected override void OnLogToConsole(IServer server, ServerLogEventArgs e)
        {

            Console.WriteLine($"{DateTime.Now}\t{e.Type}\t{e.Session?.RemoteEndPoint} {e.Message}");
        }

        public override void SessionReceive(IServer server, SessionReceiveEventArgs e)
        {
            base.SessionReceive(server, e);
            var stream = e.Stream.ToPipeStream();
            if (stream.TryReadLine(out string line))
            {
                Console.WriteLine(line);
                stream.WriteLine($"{DateTime.Now}");
                e.Stream.Flush();
            }
        }
    }
Copy the code

SSL客户端

Copy the code
    class Program
    {
        static void Main(string[] args)
        {
            var client = SocketFactory.CreateSslClient<Clients.TcpClient>("localhost", 9090, "ikende.com");
            client.Connect();
            var stream = client.Stream.ToPipeStream();
            while (true)
            {
                string value = Console.ReadLine();
                stream.WriteLine($"{DateTime.Now} {value}");
                client.Stream.Flush();
                client.Receive();
                if (stream.TryReadLine(out string line))
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
Copy the code

Agencies need to specify the name of the corresponding certificate in the creation of the client's SSL, the SSL for authentication.

running result

Print components in the open SSL service log output a detailed certificate information for easy viewing SSL startup state

Supported Platforms

Component supports 2.1 or later dorecore and Standard2.0

https://ikende.com/Files/BeetleX.Samples.Echo.zip

Guess you like

Origin www.cnblogs.com/Leo_wl/p/10964449.html