private void btnStart_Click(object sender, EventArgs e) { try { String ip = this.txtIpAddress.Text.Trim(); ushort port = ushort.Parse(this.txtPort.Text.Trim()); // 写在这个位置是上面可能会异常 SetAppState(AppState.Starting); AddMsg(string.Format("$Client Starting ... -> ({0}:{1})", ip, port)); if (client.Connetion(ip, port, this.cbxAsyncConn.Checked)) { if (cbxAsyncConn.Checked == false) { SetAppState(AppState.Started); } AddMsg(string.Format("$Client Start OK -> ({0}:{1})", ip, port)); } else { SetAppState(AppState.Stoped); throw new Exception(string.Format("$Client Start Error -> {0}({1})", client.ErrorMessage, client.ErrorCode)); } } catch (Exception ex) { AddMsg(ex.Message); } }
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事件 client.OnPrepareConnect += new TcpClientEvent.OnPrepareConnectEventHandler(OnPrepareConnect); client.OnConnect += new TcpClientEvent.OnConnectEventHandler(OnConnect); client.OnSend += new TcpClientEvent.OnSendEventHandler(OnSend); client.OnReceive += new TcpClientEvent.OnReceiveEventHandler(OnReceive); client.OnClose += new TcpClientEvent.OnCloseEventHandler(OnClose); client.OnError += new TcpClientEvent.OnErrorEventHandler(OnError); if (client.Connetion(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.ErrorCode, client.ErrorMessage, 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)); } }