Example #1
0
        private void Send()
        {
            while (true)
            {
                if (ByteQueue.Any())
                {
                    try
                    {
                        string      strMsg = this.txt_Msg.Text.Trim();
                        byte[]      buffer = Encoding.Default.GetBytes(strMsg);
                        List <byte> list   = new List <byte>();
                        list.Add(0);
                        list.AddRange(buffer);
                        //将泛型集合转换为数组
                        byte[] newBuffer = list.ToArray();
                        //获得用户选择的IP地址

                        //var mess = new byte[] { 0xff, 0x08, 0x07 };
                        //= @"FF 08 07";
                        var mess = DivisionHEX(txt_Msg.Text).TrimEnd();

                        //var strings = mess.Split(' ');
                        //var bytes = Array.ConvertAll(strings, input => Convert.ToByte(input, 16));
                        var bytes = ByteQueue.Dequeue();
                        foreach (var ip in dicPCSocket.Keys)
                        {
                            try
                            {
                                dicPCSocket[ip].Send(bytes);
                            }
                            catch (Exception ex)
                            { };
                        }
                        txt_Log.Invoke(receiveCallBack, $"发送成功");
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("给客户端发送消息出错:" + ex.Message);
                    }
                }
                else
                {
                    Thread.Sleep(1);
                }
            }
        }