コード例 #1
0
        /// <summary>
        /// Allows this instance to receive incoming requests.
        /// </summary>
        /// <remarks>This method must be called before you call the
        /// <see cref="System.Net.HttpListener.GetContext"/> method.   If
        /// the service was already started, the call has no effect.  After you
        /// have started an <itemref>HttpListener</itemref> object, you can use
        /// the <see cref='System.Net.HttpListener.Stop'/> method to stop it.
        /// </remarks>
        public void Start()
        {
            lock (this)
            {
                if (m_Closed)
                {
                    throw new ObjectDisposedException();
                }

                // If service was already started, the call has no effect.
                if (m_ServiceRunning)
                {
                    return;
                }

                m_listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                try
                {
                    // set NoDelay to increase HTTP(s) response times
                    m_listener.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, true);
                }
                catch {}

                try
                {
                    // Start server socket to accept incoming connections.
                    m_listener.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
                }
                catch {}

                IPAddress addr;

                if (Microsoft.SPOT.Hardware.SystemInfo.IsEmulator)
                {
                    addr = IPAddress.Any;
                }
                else
                {
                    addr = IPAddress.GetDefaultLocalAddress();
                }

                IPEndPoint endPoint = new IPEndPoint(addr, m_Port);
                m_listener.Bind(endPoint);

                // Starts to listen to maximum of 10 connections.
                m_listener.Listen(MaxCountOfPendingConnections);

                // Create a thread that blocks on m_listener.Accept() - basically waits for connection from client.
                m_thAccept = new Thread(AcceptThreadFunc);
                m_thAccept.Start();

                // Waits for thread that calls Accept to start.
                m_RequestArrived.WaitOne();
            }
        }