Example #1
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            try
            {
                string address = this.txtIpAddress.Text.Trim();
                ushort port = ushort.Parse(this.txtPort.Text.Trim());
                TestTimes = int.Parse(this.cbxTestTime.Text.Trim());
                TestInterv = int.Parse(this.cbxTestInterv.Text.Trim());
                ThreadCount = int.Parse(this.cbxThreadCount.Text.Trim());
                ThreadInterv = int.Parse(this.cbxThreadInterv.Text.Trim());
                DataLength = int.Parse(this.cbxDataLength.Text.Trim());
                if (CheckParams() == false)
                {
                    throw new Exception("params error!");
                }

                SetAppState(AppState.Starting);

                Timeconsuming = 0;
                TotalReceived = 0;
                TotalSent = 0;
                ExpectReceived = (Int64)TestTimes * (Int64)ThreadCount * (Int64)DataLength;

                clientList.Clear();

                // 创建指定线程个数的客户端
                for (int i = 0; i < ThreadCount; i++)
                {
                    TcpClient client = new TcpClient();
                    client.SetCallback(OnPrepareConnect, OnConnect, OnSend, OnReceive, OnClose, OnError);
                    if (client.Start(address, port) == true)
                    {
                        clientList.Add(client);
                    }
                    else
                    {
                        foreach (var c in clientList)
                        {
                            c.Stop();
                        }
                        clientList.Clear();
                        throw new Exception(string.Format(" > {2}, Connection to server fail ->({0},{1})", client.GetlastError(), client.GetLastErrorDesc(), i));
                    }
                }

                AddMsg(string.Format("$ Client start ok -> ({0}:{1})", address, port));

                // 延迟3秒
                int sendDelay = 3;
                AddMsg(string.Format(" *** willing to send data after {0} seconds ...", sendDelay));
                // Delay2(sendDelay * 1000);

                SetAppState(AppState.Started);

                testThread = new Thread(delegate()
                {
                    Thread.Sleep(sendDelay * 1000);
                    AddMsg(string.Format(" *** begin... ***", sendDelay));
                    // 发送的数据包
                    byte[] sendBytes = new byte[DataLength];

                    StopWatch.Restart();
                    bool isTerminated = false;
                    for (int i = 0; i < TestTimes; i++)
                    {
                        for (int j = 0; j < ThreadCount; j++)
                        {
                            TcpClient client = clientList[j];
                            if (client.Send(sendBytes, sendBytes.Length) == false)
                            {
                                AddMsg(string.Format("$ Client send fail [sock: {0}, seq: {1}] -> {3} ({2})",
                                                     i + 1,
                                                     j + 1,
                                                     client.SYSGetLastError(),
                                                     client.GetSocketErrorDesc(SocketError.DataSend)));
                                isTerminated = true;
                                break;
                            }

                            if (ThreadInterv > 0 && j + 1 < ThreadCount)
                            {
                                Thread.Sleep(ThreadInterv);
                                //Delay2(ThreadInterv);
                            }
                        }

                        if (isTerminated == true)
                        {
                            break;
                        }

                        if (TestInterv > 0 && i + 1 < TestTimes)
                        {
                            Thread.Sleep(TestInterv);
                            //Delay2(TestInterv);
                        }
                    }
                });
                testThread.Start();
            }
            catch (Exception ex)
            {
                SetAppState(AppState.Stoped);
                AddMsg(string.Format("$ Start fail msg:{0}", ex.Message));
            }
        }