同系列文章:
源代码下载
源代码
MandarinIceServerForm.cs
/* ---------------------------------------------------------- * 文件名称:MandarinIceServerForm.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.Management; using System.Net; using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; namespace Splash { public partial class MandarinIceServerForm : Form { /// <summary> /// 服务器是否已运行 /// </summary> private bool IsServerRunning = false; private Ice.Communicator mCommunicator = null; public MandarinIceServerForm() { InitializeComponent(); } private void buttonClear_Click(object sender, EventArgs e) { listBoxQuery.Items.Clear(); } private void buttonService_Click(object sender, EventArgs e) { if (IsServerRunning) { buttonService.Enabled = false; mCommunicator.shutdown(); } else { try { IPAddress ServerIP = IPAddress.Parse(comboBoxServerIP.Text); int ServerPort = Convert.ToInt32(textBoxServerPort.Text); string ServerEndpoints = "tcp -h " + ServerIP.ToString() + " -p " + ServerPort.ToString(); // 线程池任务 ThreadPool.QueueUserWorkItem(StartServiceServer, ServerEndpoints); } catch (Exception exception) { AppendLog("异常: " + exception.Message); } } } // 开启服务 private void StartServiceServer(object state) { try { mCommunicator = Ice.Util.initialize(); Ice.ObjectAdapter adapter = mCommunicator.createObjectAdapterWithEndpoints("MandarinIceService", (string)state); adapter.add(new MandarinIceService(), Ice.Util.stringToIdentity("MandarinIceService")); adapter.activate(); SetStatus(true); mCommunicator.waitForShutdown(); // 挂起线程 mCommunicator.destroy(); SetStatus(false); } catch (Exception exception) { AppendLog("异常: " + exception.Message); } } private void Form1_Load(object sender, EventArgs e) { // 列举本机所有的有效IP地址(自动过滤掉虚拟网卡IP地址) string[] IPCollection = GetLocalIPv4Address(); if (IPCollection != null) { comboBoxServerIP.DataSource = IPCollection; comboBoxServerIP.Enabled = !(IPCollection.Length == 1); // 如果本机只有一个有效IP地址,则直接锁定 } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (IsServerRunning) { mCommunicator.shutdown(); } } public void AppendLog(string text) { this.Invoke(new Action<string>((msg) => { this.listBoxQuery.Items.Add(msg); }), text); } public void SetStatus(bool isServerRunning) { this.Invoke(new Action<bool>((status) => { if (status) { AppendLog("服务已启动!"); this.buttonService.Text = "停止服务"; IsServerRunning = true; } else { AppendLog("服务已停止!"); this.buttonService.Text = "开启服务"; this.buttonService.Enabled = true; IsServerRunning = false; } }), isServerRunning); } /// <summary> /// 获取本机IP地址列表 /// </summary> /// <param name="isIncludeUsb">是否包含USB网卡,默认为不包含</param> /// <returns>本机真实网卡信息</returns> public static string[] GetLocalIPv4Address(bool isIncludeUsb = false) { // IPv4正则表达式 const string IPv4RegularExpression = "^(?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))$"; // 注意:只获取已连接的网卡 string NetworkAdapterQueryString; if (isIncludeUsb) NetworkAdapterQueryString = "SELECT * FROM Win32_NetworkAdapter WHERE (NetConnectionStatus = 2) AND (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%'))"; else NetworkAdapterQueryString = "SELECT * FROM Win32_NetworkAdapter WHERE (NetConnectionStatus = 2) AND (MACAddress IS NOT NULL) AND (NOT (PNPDeviceID LIKE 'ROOT%')) AND (NOT (PNPDeviceID LIKE 'USB%'))"; ManagementObjectCollection NetworkAdapterQueryCollection = new ManagementObjectSearcher(NetworkAdapterQueryString).Get(); if (NetworkAdapterQueryCollection == null) return null; List<string> IPv4AddressList = new List<string>(NetworkAdapterQueryCollection.Count); foreach (ManagementObject mo in NetworkAdapterQueryCollection) { // 获取网卡配置信息 string ConfigurationQueryString = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE Index = " + mo["Index"]; ManagementObjectCollection ConfigurationQueryCollection = new ManagementObjectSearcher(ConfigurationQueryString).Get(); if (ConfigurationQueryCollection == null) continue; foreach (ManagementObject nacmo in ConfigurationQueryCollection) { if ((bool)nacmo["IPEnabled"]) { string[] IPCollection = nacmo["IPAddress"] as string[]; // IP地址 if (IPCollection != null) { foreach (string adress in IPCollection) { Match match = Regex.Match(adress, IPv4RegularExpression); if (match.Success) { IPv4AddressList.Add(adress); break; } } } } } } if (IPv4AddressList.Count > 0) return IPv4AddressList.ToArray(); else return null; } } }