博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
万网博通NMSS平台二次开发(UDP方式传输)
阅读量:4954 次
发布时间:2019-06-12

本文共 6830 字,大约阅读时间需要 22 分钟。

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("
"); } //序列化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 != "
") { 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(); } }}

 

转载于:https://www.cnblogs.com/HansZimmer/p/10613626.html

你可能感兴趣的文章
Javascript 有用参考函数
查看>>
点群的判别(三)
查看>>
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>
【TP SRM 703 div2 500】 GCDGraph
查看>>
MapReduce 重要组件——Recordreader组件 [转]
查看>>
webdriver api
查看>>
转载-FileZilla Server源码分析(1)
查看>>
apache 实现图标缓存客户端
查看>>
MediaWiki左侧导航栏通过特殊页面就可以设置。
查看>>
揭秘:黑客必备的Kali Linux是什么,有哪些弊端?
查看>>
linux系统的远程控制方法——学神IT教育
查看>>