Beispiel #1
0
        static void AsyncReceiveCallback(IAsyncResult result)
        {
            SocketAndBuffer c = result.AsyncState as SocketAndBuffer;

            try
            {
                int length = c.s.EndReceive(result);
                if (length != 0)
                {
                    Console.WriteLine(DateTime.Now + " receive buffer " + GetBufferIndex(c.buffer, length) + ", length " + length);
                    Thread.Sleep(20);

                    BeginReceive(c.s);
                }
                else
                {
                    c.s.Close();
                }
            }
            catch (Exception ex)
            {
                c.s.Close();
                Console.WriteLine(ex.ToString());
            }
        }
Beispiel #2
0
        static void BeginReceive(Socket s)
        {
            byte[] buffer = new byte[8 * 1024];

            var state = new SocketAndBuffer();

            state.s      = s;
            state.buffer = buffer;

            s.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None,
                           AsyncReceiveCallback, state);
        }