Example #1
0
        private void SendOneDatagram()
        {
            int maxLength = int.Parse(cb_MaxDatagramSize.Text) * 10240;

            Random rnd            = new Random();
            int    datagramLength = rnd.Next(1, maxLength);

            //就这里,弄个类的序列化。
            DbTools     db = new DbTools();
            ModelMember m  = new ModelMember();

            m.email     = "赵钦@sina.com";
            m.hdinfo    = HardWare.getHardCode();
            m.logintime = DateTime.Now;
            string datagramText = db.ClasstoString(m, "VCDS");

            datagramText = "<" + datagramText + ">";
            //if (ck_ErrorDatagram.Checked && datagramLength < maxLength * 0.20)  // 产生一个无 > 的包
            //{
            //    datagramText = "<" + tb_ClientName.Text.Trim() + ",".PadRight(datagramLength, 'a');
            //} else if (ck_ErrorDatagram.Checked && datagramLength > maxLength * 0.80)  // 产生一个无 < 的包
            //{
            //    datagramText = tb_ClientName.Text.Trim() + ",".PadRight(datagramLength, 'a') + ">";
            //} else  // 正常包
            //{
            //    string header = "<" + tb_ClientName.Text.Trim() + ",";
            //    string tailer = ",".PadRight(datagramLength, 'a') + ">";
            //    datagramText = header + (header.Length + tailer.Length + 10).ToString("0000000000") + tailer;  // 第二个字节是长度
            //}

            byte[] datagram = Encoding.ASCII.GetBytes(datagramText);

            try {
                m_socketClient.Client.Send(datagram);

                this.RefreshSendCount();
                this.AddInfo("send text len = " + datagramText.Length.ToString());

                if (ck_AsyncReceive.Checked)  // 异步接收回答
                {
                    m_socketClient.Client.BeginReceive(m_receiveBuffer, 0, m_receiveBuffer.Length, SocketFlags.None, this.EndReceiveDatagram, this);
                }
                else
                {
                    this.Receive();
                }
            } catch (Exception err) {
                this.AddInfo("send exception: " + err.Message);
                this.CloseClientSocket();
            }
        }