Exemple #1
0
        static public void Main()
        {
            Sodao.FastSocket.SocketBase.Log.Trace.EnableConsole();
            var client = new SocketClient <CommandLineMessage>(new CommandLineProtocol(), 1024, 1024, 3000, 3000);

            //建立50个socket连接
            for (int i = 0; i < 50; i++)
            {
                client.TryRegisterEndPoint(i.ToString(), new[] { new IPEndPoint(IPAddress.Parse("127.0.0.1"), 1500) },
                                           connection =>
                {
                    var source  = new TaskCompletionSource <bool>();
                    var request = client.NewRequest("init", Encoding.UTF8.GetBytes("init" + System.Environment.NewLine), 3000,
                                                    ex => source.TrySetException(ex),
                                                    message => source.TrySetResult(true));
                    connection.BeginSend(request);
                    return(source.Task);
                });
            }

            //100个同时发送
            for (int i = 100; i < 0; i++)
            {
                Task.Factory.StartNew(() => Do(client));
            }

            Console.ReadLine();
        }
Exemple #2
0
        private void button1_Click(object sender, EventArgs e)
        {
            IPAddress ip   = IPAddress.Parse(txtAddress.Text);
            int       port = int.Parse(txtPort.Text);

            client = new SocketClient <CommandLineMessage>(new CommandLineProtocol(), 1024, 1024, 3000, 3000);
            client.TryRegisterEndPoint(MyId, new[] { new IPEndPoint(ip, port) },
                                       connection =>
            {
                var source  = new TaskCompletionSource <bool>();
                var request = client.NewRequest("init", Encoding.UTF8.GetBytes("init" + System.Environment.NewLine), 3000,
                                                ex => source.TrySetException(ex),
                                                message => source.TrySetResult(true));
                connection.BeginSend(request);
                return(source.Task);
            });
        }