private static void SendMessages() { Random random = new Random(); RequestMessage msg = new RequestMessage(); //msg.String = RandomString(2000); int msgId = 0; while (!_cancellationToken.IsCancellationRequested) { //for (int i = 0; i < 1e4; i++) { msg.Integer = msgId++; // msg.String = RandomString(256); if (!_clientSocket.Connected) { //_cancellationToken.Cancel(); //break; continue; } _clientSocket.SendAsync(msg); Interlocked.Increment(ref _messageCounter); //_messageReceivedEvent.WaitOne(1000); } //Thread.Sleep(random.Next(10, 1000)); } }
private static void TcpClient() { System.Net.Sockets.TcpClient tcpClient = new TcpClient("127.0.0.1", 9900); var stream = tcpClient.GetStream(); RequestMessage msg = new RequestMessage(); msg.Integer = 5; Serializer serializer = new Serializer(); var bytes = serializer.Serialize(msg); //for (int i = 0; i < 1000; i++) while (true) { stream.Write(bytes, 0, bytes.Length); Interlocked.Increment(ref _messageCounter); } }