Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            try
            {
                using (Socket s = new Socket(SocketType.Stream, ProtocolType.Tcp))
                {
                    s.Connect(new System.Net.IPEndPoint(IPAddress.Parse("172.16.4.98"), 3333));

                    while (true)
                    {
                        UInt32 index;
                        byte[] buffer = MakeBufferWithIndex(1024, out index);

                        var state = new SocketAndIndex();
                        state.s     = s;
                        state.index = index;

                        s.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, AsyncSendCallback, state);
                        Thread.Sleep(1000);
                        if (Console.KeyAvailable)
                        {
                            break;
                        }
                    }

                    Console.WriteLine("Stop Fast Send");
                    Console.ReadKey();

                    Console.WriteLine("Begin Slow Send");
                    while (true)
                    {
                        byte[] buffer = new byte[1024];
                        s.BeginSend(buffer, 0, buffer.Length, SocketFlags.None, AsyncSendCallback, s);
                        Thread.Sleep(1000);
                        if (Console.KeyAvailable)
                        {
                            break;
                        }
                    }

                    Console.WriteLine("Stop Slow Send");
                    Console.ReadKey();

                    Console.ReadKey();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
Ejemplo n.º 2
0
        static void AsyncSendCallback(IAsyncResult result)
        {
            SocketAndIndex state = result.AsyncState as SocketAndIndex;

            try
            {
                Console.WriteLine(DateTime.Now + " send buffer " + state.index);
                state.s.EndSend(result);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                state.s.Close();
            }
        }