Beispiel #1
0
        //利用同步方式进行发送
        private static void TestSyncSend()
        {
            byte[] readBuffer;
            string str    = "68 20 38 04 21 08 00 59 42 81 2E 90 1F 00 05 19 28 53 00 05 19 28 53 00 17 00 00 00 00 35 00 00 00 00 2C 42 33 18 00 03 19 00 10 19 00 61 27 00 26 57 23 31 03 15 20 03 01 F7 16";
            var    tmpArr = str.Split(' ');

            readBuffer = new byte[tmpArr.Count()];
            for (int i = 0; i < tmpArr.Count(); i++)
            {
                string strTmp = tmpArr[i];
                int    intTmp = Convert.ToInt32(strTmp, 16);//先转成10进制
                readBuffer[i] = Convert.ToByte(intTmp);
            }

            Logger.ErrorFormat("开始发送数据");
            Task task = new Task(() =>
            {
                Thread.Sleep(20000);
                AsyncSocketUserToken[] userTokenArray = null;
                AsyncSocketSvr.AsyncSocketUserTokenList.CopyList(ref userTokenArray);
                //并行循环不停的发送数据
                while (true)
                {
                    Parallel.ForEach(userTokenArray, token =>
                    {
                        AsyncSocketSvr.Send(token, readBuffer);
                        Monitor.IntTotalMsg++;
                    });
                }

                //串行操作的时候,自己的笔记本发送数量只能在2500个包
                //while (true)
                //{
                //      foreach (AsyncSocketUserToken token in userTokenArray)
                //    {
                //        AsyncSocketSvr.Send(token, readBuffer);
                //        Monitor.IntTotalMsg++;
                //    }
                //}
            });

            task.Start();

            //Logger.ErrorFormat("发送数据结束,花费时间: {0}毫秒", (time2-time1).TotalMilliseconds);
        }
Beispiel #2
0
        //利用同步方式进行发送
        private static void TestSyncSend()
        {
            byte[] readBuffer;
            string str    = "68 20 38 04 21 08 00 59 42 81 2E 90 1F 00 05 19 28 53 00 05 19 28 53 00 17 00 00 00 00 35 00 00 00 00 2C 42 33 18 00 03 19 00 10 19 00 61 27 00 26 57 23 31 03 15 20 03 01 F7 16";
            var    tmpArr = str.Split(' ');

            readBuffer = new byte[tmpArr.Count()];
            for (int i = 0; i < tmpArr.Count(); i++)
            {
                string strTmp = tmpArr[i];
                int    intTmp = Convert.ToInt32(strTmp, 16); //先转成10进制
                readBuffer[i] = Convert.ToByte(intTmp);
            }

            Logger.ErrorFormat("开始发送数据");
            Task task = new Task(() =>
            {
                AsyncSocketUserToken[] userTokenArray = null;
                //循环不停的发送数据
                AsyncSocketSvr.AsyncSocketUserTokenList.CopyList(ref userTokenArray);
                Monitor.SendBeginTime = DateTime.Now;
                for (int i = 0; i < userTokenArray.Length; i++)
                {
                    AsyncSocketUserToken token = userTokenArray[i];
                    AsyncSocketSvr.Send(token, readBuffer);
                    Monitor.IntTotalMsg++;
                }
                Monitor.SendFinishedTime = DateTime.Now;
                Logger.ErrorFormat("Socket: {0},发送一次循环时间/毫秒:{1},", userTokenArray.Length, (Monitor.SendFinishedTime - Monitor.SendBeginTime).TotalMilliseconds);
                Thread.Sleep(2000);
            });

            task.Start();

            //Logger.ErrorFormat("发送数据结束,花费时间: {0}毫秒", (time2-time1).TotalMilliseconds);
        }