标签:option 端口号 .net client component [] eve ast 行业
在我们的程序开发中,有些行业用到座机的来电弹屏,而C#作为微软的一个重要开发工具,应用广泛,本代码实现了C#调用DLL实现来电弹屏(以中盛座机来电显示盒为例,该盒子稳定可靠,编程简单)。
using ServiceDesk; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Net; using System.Net.Sockets; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinF { public partial class Form1 : Form {
//最关键的是下面4行调用DLL的声明 [DllImport("ZSCIDUDP.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "ZSCIDInit")] public static extern int ZSCIDInit(string AUDPS_IP, int AUDPS_Port, bool AIsSendState); [DllImport("ZSCIDUDP.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi, EntryPoint = "ZSCIDFree")] public static extern void ZSCIDFree(int ARef); public int laidianCount { get; set; } public int FRef { get; set; } public void zsinit() { FRef = ZSCIDInit("127.0.0.1", 9013, false);//第一个参数是来电后要发到UDP监听的IP,第二个参数是UDP监听的端口号 } public void zsfree() { if (FRef > 0) { ZSCIDFree(FRef); FRef = 0; } } public Form1() { InitializeComponent(); UdpLisenter(); //这里实现UDP监听 } private void Form1_Load(object sender, EventArgs e) { zsinit(); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { free(); } private void btn_init_Click(object sender, EventArgs e) { init(); } private void btn_free_Click(object sender, EventArgs e) { free(); } void init() { try { zsinit(); btn_free.Enabled = true; btn_init.Enabled = false; } catch (Exception ex) { MessageBox.Show(ex.Message, "ZSCIDInit异常"); } finally { txt_FRef.AppendText("初始化【FRef=" + FRef + "】" + Environment.NewLine); } } void free() { try { txt_FRef.AppendText("释放前【FRef=" + FRef + "】" + Environment.NewLine); zsfree(); btn_free.Enabled = false; btn_init.Enabled = true; } catch (Exception ex) { MessageBox.Show(ex.Message, "ZSCIDFree异常"); } finally { txt_FRef.AppendText("释放后【FRef=" + FRef + "】" + Environment.NewLine); } } void StateHandler(string data) { txt_state.AppendText(data.ToString() + Environment.NewLine); } private void UdpLisenter() { Task.Factory.StartNew(() => { int updPort = 9013; UdpClient listener = new UdpClient(updPort); listener.DontFragment = true; listener.EnableBroadcast = true; IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Loopback, updPort); while (true) { try { byte[] bytes = listener.Receive(ref RemoteIpEndPoint); string data = System.Text.Encoding.Default.GetString(bytes); string PhoneNumber = getnum(data); writelog(PhoneNumber); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }, TaskCreationOptions.LongRunning | TaskCreationOptions.PreferFairness); } string getnum(string data) { string PhoneNumber = ""; if (!string.IsNullOrWhiteSpace(data)) { string[] strs = data.Split(new string[] { "\r\n" }, StringSplitOptions.None); foreach (string k in strs) { string[] tttmp = k.Split(‘=‘); if (tttmp.Length > 1 && tttmp[0] == "NUM") PhoneNumber = tttmp[1]; } } return PhoneNumber; } void writelog(string number) { if (!string.IsNullOrWhiteSpace(number)) { txt_udprecieve.AppendText("[" + laidianCount++ + "]: " + number + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + Environment.NewLine); LaidianWriter wer = new LaidianWriter(); wer.Push(number); } } } }
标签:option 端口号 .net client component [] eve ast 行业
原文地址:http://www.cnblogs.com/codesoeasy/p/7860710.html