C# Hprose轻量级、跨语言、跨平台的面向对象的高性能远程动态通讯中间件

范嘉
2023-12-01

Hprose (High Performance Remote Object Service Engine) 是一个MIT开源许可的新型轻量级跨语言跨平台的面向对象的高性能远程动态通讯中间件。它支持众多语言,例如nodeJs, C++, .NET, Java, Delphi, Objective-C, ActionScript, JavaScript, ASP, PHP, Python, Ruby, Perl, Golang 等语言,通过 Hprose 可以在这些语言之间实现方便且高效的互通。
Hprose 易学易用,且功能强大,您只需很短时间的学习,就可以用它方便地构建出跨语言跨平台分布式的电信级应用系统。

国内码云git项目下载地址
https://gitee.com/andot/hprose-dotnet

github地址
https://github.com/hprose/hprose-dotnet

服务端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hprose.Server;

namespace HproseTcpServerTest {
    class Program {
        public static string Hello(string name) {
            return "Hello " + name + "!";
        }
        public static List<object> GetList(string word)
        {
            List<object> list = new List<object>();
            for (int i = 0; i < 100; i++)           
            {
                list.Add(word);
            }
            return list;
        }
        static void Main(string[] args) {
            try
            {
                HproseTcpListenerServer tcpserver = new HproseTcpListenerServer("tcp4://127.0.0.1:4321/");
                tcpserver.Add("Hello", typeof(Program));
                tcpserver.Add("GetList", typeof(Program));
                tcpserver.Start();


                HproseHttpListenerServer httpserver = new HproseHttpListenerServer("http://localhost:8888/");
                httpserver.Add("Hello", typeof(Program));
                httpserver.Add("GetList", typeof(Program));
                httpserver.Start();

                Console.WriteLine("服务已启动");
                Console.ReadKey();
                tcpserver.Stop();
                httpserver.Stop();
            }
            catch (Exception ex)
            {
                Console.WriteLine("启动异常:"+ex.Message);
            }         
        }
    }
}

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hprose.Client;
using System.Diagnostics;
using System.Threading;

namespace HproseTcpClientTest
{
    public interface IHello
    {
        string Hello(string name);
        /// <summary>
        /// 得到集合
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        List<object> GetList(string word);
    }
    class Program
    {
        static IHello hello;

        static void Test(string data) {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            for (int i = 0; i < 1000; i++) {
                //hello.Hello(data);
                hello.GetList(data);
            }
            stopwatch.Stop();
            Console.WriteLine(stopwatch.Elapsed);
        }
        static void Main(string[] args)
        {

            HproseClient client = HproseClient.Create("tcp4://127.0.0.1:4321/");
            hello = client.UseService<IHello>();

            //20秒后执行          
            while (true)
            {
                if (DateTime.Now.Second == DateTime.Parse("2018-04-19 16:43:20").Second)
                {
                    break;
                }
                Thread.Sleep(500);
            }

            Console.WriteLine("TCP");
            //Console.WriteLine(hello.Hello("World"));
            Test("World");
            Test("".PadRight(512, '苏'));
            Test("".PadRight(1024, '苏'));
            Test("".PadRight(2 * 1024, '苏'));
            Test("".PadRight(4 * 1024, '苏'));
            Test("".PadRight(8 * 1024, '苏'));
            Test("".PadRight(16 * 1024, '苏'));
            Test("".PadRight(32 * 1024, '苏'));
            Test("".PadRight(64 * 1024, '苏'));
            Console.WriteLine("完成");

            client = HproseClient.Create("http://localhost:8888/");
            hello = client.UseService<IHello>();
            Console.WriteLine("HTTP");
            //Console.WriteLine(hello.Hello("World"));
            Test("World");
            Test("".PadRight(512, '苏'));
            Test("".PadRight(1024, '苏'));
            Test("".PadRight(2 * 1024, '苏'));
            Test("".PadRight(4 * 1024, '苏'));
            Test("".PadRight(8 * 1024, '苏'));
            Test("".PadRight(16 * 1024, '苏'));
            Test("".PadRight(32 * 1024, '苏'));
            Test("".PadRight(64 * 1024, '苏'));
            Console.WriteLine("完成");

            Console.ReadKey();

            //            client.Invoke<string>("hello", new Object[] { "Async World" }, result => Console.WriteLine(result));

        }
    }
}

demo下载地址
https://download.csdn.net/download/u011511086/10360956

 类似资料: