usingServiceDesk;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Runtime.InteropServices;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceWinF
{public partial classForm1 : 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(intARef);public int laidianCount { get; set; }public int FRef { get; set; }public voidzsinit()
{
FRef= ZSCIDInit("127.0.0.1", 9013, false);//第一个参数是来电后要发到UDP监听的IP,第二个参数是UDP监听的端口号
}public voidzsfree()
{if (FRef > 0)
{
ZSCIDFree(FRef);
FRef= 0;
}
}publicForm1()
{
InitializeComponent();
UdpLisenter(); //这里实现UDP监听
}private void Form1_Load(objectsender, EventArgs e)
{
zsinit();
}private void Form1_FormClosing(objectsender, FormClosingEventArgs e)
{
free();
}private void btn_init_Click(objectsender, EventArgs e)
{
init();
}private void btn_free_Click(objectsender, EventArgs e)
{
free();
}voidinit()
{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);
}
}voidfree()
{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(stringdata)
{
txt_state.AppendText(data.ToString()+Environment.NewLine);
}private voidUdpLisenter()
{
Task.Factory.StartNew(()=>{int updPort = 9013;
UdpClient listener= newUdpClient(updPort);
listener.DontFragment= true;
listener.EnableBroadcast= true;
IPEndPoint RemoteIpEndPoint= newIPEndPoint(IPAddress.Loopback, updPort);while (true)
{try{byte[] bytes = listener.Receive(refRemoteIpEndPoint);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(stringdata)
{string PhoneNumber = "";if (!string.IsNullOrWhiteSpace(data))
{string[] strs = data.Split(new string[] { "\r\n"}, StringSplitOptions.None);foreach (string k instrs)
{string[] tttmp = k.Split('=');if (tttmp.Length > 1 && tttmp[0] == "NUM")
PhoneNumber= tttmp[1];
}
}returnPhoneNumber;
}void writelog(stringnumber)
{if (!string.IsNullOrWhiteSpace(number))
{
txt_udprecieve.AppendText("[" + laidianCount++ + "]:" + number + " " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") +Environment.NewLine);
LaidianWriter wer= newLaidianWriter();
wer.Push(number);
}
}
}
}