using System; using System.Net; using System.Net.Sockets; using System.Text; using System.Threading; namespace ServiceConsoleDemo { class Program { static Socket send; //Socket建立一个本地通信套接字 static EndPoint receivePoint; //服务端端口 private static readonly object obj = new object(); static void Main(string[] args) { SendFun(); Thread ReceiveThd = new Thread(new ThreadStart(ReceiveFun)); ReceiveThd.Start(); Thread SendScoreThd = new Thread(new ThreadStart(ScoreCmdFun)); SendScoreThd.Start(); Thread SendDeviceThd = new Thread(new ThreadStart(DeviceCmdFun)); SendDeviceThd.Start(); } static void SendFun() { try { send = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp); send.Bind(new IPEndPoint(IPAddress.Parse("192.168.1.52"), 2000)); Console.WriteLine("本地Socket通信已打开..."); receivePoint = new IPEndPoint(IPAddress.Parse("192.168.1.249"),8388); } catch(Exception ex) { Console.WriteLine("打开发送命令失败:",ex.Message); } } static void ReceiveFun() { lock(obj) { while(true) { try { byte[] buffer = new byte[65535]; int length = send.ReceiveFrom(buffer, ref receivePoint); string message = Encoding.UTF8.GetString(buffer, 0, length); Console.WriteLine(message); } catch(Exception ex) { Console.WriteLine("连接服务端失败:",ex.Message); } } } } static void ScoreCmdFun() { Thread.Sleep(5000); send.SendTo(Encoding.UTF8.GetBytes(scoreCmd()), receivePoint); } static string scoreCmd() { JsonHelper help = new JsonHelper(); help.Reset(); help.version = "0"; help.msgid = "461100725"; help.type = "51"; help.cmd = "7"; return help.ToJson(); } static void DeviceCmdFun() { Thread.Sleep(10000); send.SendTo(Encoding.UTF8.GetBytes(deviceCmd()), receivePoint); } static string deviceCmd() { JsonHelper help = new JsonHelper(); help.Reset(); help.version = "0"; help.msgid = "1487326767"; help.type = "1"; help.cmd = "4"; help.AddItem("pageNum", 0); help.AddItem("rowCount", 50); help.AddItem("search", ""); return help.ToJson(); } } }
需要JsonHelper类,如下
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ServiceConsoleDemo { public class JsonHelper { //对应JSON的singleInfo成员 protected string _version = string.Empty; protected string _msgid = string.Empty; protected string _type = string.Empty; protected string _cmd = string.Empty; public System.Collections.ArrayList arrData = new ArrayList(); protected System.Collections.ArrayList arrDataItem = new ArrayList(); public JsonHelper() { } public string version { get { return _version; } set { //如设置error,则自动设置success为false if (value != "") _version = string.Empty; _version = value; } } public string msgid { get { return _msgid; } set { //如设置error,则自动设置success为false if (value != "") _msgid = string.Empty; _msgid = value; } } public string type { get { return _type; } set { //如设置error,则自动设置success为false if (value != "") _type = string.Empty; _type = value; } } public string cmd { get { return _cmd; } set { //如设置error,则自动设置success为false if (value != "") _cmd = string.Empty; _cmd = value; } } //重置,每次新生成一个json对象时必须执行该方法 public void Reset() { _version = string.Empty; _msgid = string.Empty; _type = string.Empty; _cmd = string.Empty; arrData.Clear(); arrDataItem.Clear(); } public void AddItem(string name, string value) { arrData.Add(""" + name + "":" + """ + value + """); } public void AddItem(string name, int value) { arrData.Add(""" + name + "":" + "" + value + ""); } public void ItemOk() { arrData.Add("<BR>"); } //序列化JSON对象,得到返回的JSON代码 public string ToJson() { StringBuilder sb = new StringBuilder(); sb.Append("{"); sb.Append(""version":" + _version.ToString().ToLower() + ","); sb.Append(""msgid":" + """ + _msgid.ToString().ToLower() + "","); sb.Append(""type":" + _type.ToString().ToLower() + ","); sb.Append(""cmd":" + _cmd.ToString().ToLower() + ","); sb.Append(""data":{"); int index = 0; if (arrData.Count <= 0) { sb.Append("}"); } else { foreach (string val in arrData) { index++; if (val != "<BR>") { sb.Append(val + ","); } else { sb = sb.Replace(",", "", sb.Length - 1, 1); sb.Append("},"); if (index < arrData.Count) { sb.Append("{"); } } } sb = sb.Replace(",", "", sb.Length - 1, 1); sb.Append("}"); } sb.Append("}"); return sb.ToString(); } } }