Exemple #1
0
        public void Connect()
        {
            var tasks = new Task[200];
            for (int i = 0; i < tasks.Length; i++)
            {
                tasks[i] = Task.Factory.StartNew((index) =>
                {
                    EndPoint LocalEndPoint = null;
                    IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), port);
                    using (ClientSocket client = new ClientSocket(new ClientSocketSettings(1024, remoteEndPoint), new RequestHandler(new MessageHandler())))
                    {
                        try
                        {
                            client.IsSyncReceived = true;
                            client.Connect();
                            LocalEndPoint = client.LocalEndPoint;
                            Trace.WriteLine(string.Format("Client connect ok,{0}", LocalEndPoint));
                            string str = "hello" + index;
                            byte[] data = Encoding.UTF8.GetBytes(str);
                            if (client.PostSend(data, 0, data.Length).Wait(3 * 1000) && client.Receive(3 * 1000))
                            {
                                byte[] buffer;
                                if (client.TryReceiveBytes(out buffer))
                                {
                                    Assert.AreEqual(Encoding.UTF8.GetString(buffer), str);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Trace.WriteLine(string.Format("Client {0} connect error:{1} ", LocalEndPoint == null ? "" : LocalEndPoint.ToString(), ex.Message));
                            Assert.Inconclusive();
                        }
                    }

                }, i);
            }

            Task.WaitAll(tasks);
            Trace.WriteLine("Client connect test end");
            Thread.Sleep(60000);
        }