ZeroC Ice(四)构建汉字拼音查询微服务:客户端

同系列文章:

源代码下载

MandarinIce 20170906.zip

演示程序界面

源代码
MandarinIceClientForm.cs

/* ----------------------------------------------------------
 * 文件名称:MandarinIceClientForm.cs
 * 
 * 作者:秦建辉
 * 
 * 微信:splashcn
 * 
 * 博客:http://www.firstsolver.com/wordpress/
 * 
 * 开发环境:
 *      Visual Studio 2017
 *      .NET Framework 4.5.2
 *      ZeroC Ice 3.7.0
 *      
 * 版本历史:
 *      V1.0    2017年09月06日
 *              基于ZeroC Ice框架实现汉字拼音查询服务-客户端
 *
 * 参考资料:
 *      https://zeroc.com/
------------------------------------------------------------ */
using Com.FirstSolver.Mandarin;
using System;
using System.Collections.Generic;
using System.Net;
using System.Windows.Forms;

namespace Splash
{
    public partial class MandarinIceClientForm : Form
    {
        public MandarinIceClientForm()
        {
            InitializeComponent();
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            string Question = textBoxQuestion.Text;
            if (string.IsNullOrEmpty(Question)) return;

            // 服务器地址和端口
            IPAddress ServerIP = IPAddress.Parse(textBoxServerIP.Text);
            int ServerPort = Convert.ToInt32(textBoxServerPort.Text);
            string ServerEndpoints = "MandarinIceService:tcp -h " + ServerIP.ToString() + " -p " + ServerPort.ToString();

            try
            {
                using (Ice.Communicator ic = Ice.Util.initialize())
                {
                    Ice.ObjectPrx obj = ic.stringToProxy(ServerEndpoints).ice_twoway().ice_timeout(-1).ice_secure(false);
                    IMandarinServicePrx Service = IMandarinServicePrxHelper.checkedCast(obj);
                    if (Service != null)
                    {
                        if (checkBoxHomophone.Checked)
                        {   // 查询同音字
                            textBoxAnswer.Clear();
                            Dictionary<string, string> Source = Service.QueryHomophone(Question, GBCharset.GB2312);
                            if (Source != null)
                            {
                                foreach (KeyValuePair<string, string> Item in Source)
                                {
                                    textBoxAnswer.AppendText(Item.Key + "\r\n");
                                    textBoxAnswer.AppendText(Item.Value + "\r\n");
                                }
                            }
                        }
                        else
                        {   // 查询拼音
                            textBoxAnswer.Text = Service.QueryPinyin(Question, checkBoxDigitTone.Checked, checkBoxUppercase.Checked);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                textBoxAnswer.Text = exception.Message;
            }            
        }
    }
}

Comments are closed.