开发平台:VS2012及以上版本 .Net 4.0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
namespace TestModel
{
public partial class Form1 : Form
{
/// <summary>
/// 服务
/// </summary>
AsySocket.Server sever = null;
/// <summary>
/// 服务端口
/// </summary>
int Port;
/// <summary>
/// 服务类型
/// </summary>
AsySocket.Service_Type severType;
/// <summary>
/// 消息缓存
/// </summary>
AsySocket.MemQueue<string> msgQueue = new AsySocket.MemQueue<string>();
public Form1()
{
Control.CheckForIllegalCrossThreadCalls = false;
InitializeComponent();
}
private void btn_StartSever_Click(object sender, EventArgs e)
{
if (!int.TryParse(tbox_SeverPort.Text.Trim(), out Port))
{
Port = 10001;
tbox_SeverPort.Text = Port.ToString();
}
if (rbtn_SeverType.Checked)
{
severType = AsySocket.Service_Type.TCP;
}else{
severType = AsySocket.Service_Type.UDP;
}
sever = new AsySocket.Server();
sever.onClose += new AsySocket.onClose(sever_onClose);
sever.OnRegist += new AsySocket.onRegist(sever_OnRegist);
sever.onCmdRecv += new AsySocket.onCmdRecv(sever_onCmdRecv);
sever.onError += new AsySocket.onError(sever_onError);
sever.Start(severType, Port);
btn_StartSever.Enabled = false;
btn_StopSever.Enabled = true;
}
#region 事件处理
private void sever_onClose(AsySocket.CmdTag cmd)
{
msgQueue.Add("设备" + cmd.DeviceID + "断开。");
lbox_deviceList.Items.Remove(cmd.DeviceID.ToString());
}
private void sever_OnRegist(AsySocket.CmdTag cmd)
{
msgQueue.Add("收到设备" + cmd.DeviceID + "注册登录信息。");
sever.SetOnLine(cmd.DeviceID, 1); //响应设备注册;1允许,0不允许
if (!lbox_deviceList.Items.Contains(cmd.DeviceID.ToString()))
{
lbox_deviceList.Items.Add(cmd.DeviceID.ToString());
}
}
private void sever_onCmdRecv(AsySocket.CmdTag cmd)
{
Exectue(cmd);
}
private void sever_onError(string msg)
{
msgQueue.Add(msg);
}
#endregion
private void btn_StopSever_Click(object sender, EventArgs e)
{
if (sever == null)
{
return;
}
try
{
sever.Stop();
sever = null;
btn_StartSever.Enabled = true;
btn_StopSever.Enabled = false;
}
catch { }
}
/// <summary>
/// 解析收到的数据
/// </summary>
/// <param name="cmd"></param>
private void Exectue(AsySocket.CmdTag cmd)
{
try
{
switch (cmd.CmdType)
{
default:
{
msgQueue.Add(string.Format("收到设备{0}数据——{1}。", cmd.DeviceID, AsySocket.FormatHelper.ByteArrayToHexString(cmd.Param)));
break;
}
}
}
catch (Exception ex){
msgQueue.Add(string.Format("解析数据错误{0},{1}\r\n{2}", cmd.DeviceID, AsySocket.FormatHelper.ByteArrayToHexString(cmd.Param), ex.Message));
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
while (msgQueue.Count > 0)
{
if (tbox_Msg.TextLength > 32768)
{
tbox_Msg.Clear();
}
tbox_Msg.AppendText(string.Format("{0}:{1}\r\n", DateTime.Now.ToLongTimeString(), msgQueue.Get()));
}
lb_DeviceCount.Text = lbox_deviceList.Items.Count.ToString();
}
private void Form1_Closing(object sender, FormClosingEventArgs e)
{
timer1.Enabled = false;
try
{
sever.Stop();
}
catch { }
}
private void btn_ClearMsg_Click(object sender, EventArgs e)
{
tbox_Msg.Clear();
}
}
}