예제 #1
0
        /// <summary>
        /// Starts a new WebServer that listens on all connections at the specified port.
        /// The WebServer will begin listening immediately after construction.
        /// </summary>
        /// <remarks>
        /// Normally, incoming connections are only possible on WiFi networks.
        /// Each connection gets its own thread.
        /// </remarks>
        /// <param name="port">The TCP port to listen on</param>
        /// <param name="serv">The function which handles received requests.</param>
        /// <exception cref="System.Net.Sockets.SocketException">Opening the socket for listening failed</exception>
        public WebServer(IPAddress address, ushort port, RequestServicer serv)
        {
            if (null == address)
            {
                address = IPAddress.Any;
            }
            servicer     = serv;
            cancelsource = new CancellationTokenSource();
            serversock   = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            EndPoint local = new IPEndPoint(address, port);

            serversock.Bind(local);
            serversock.Listen(5);
            // Launch a thread that asynchronously calls accept
            listener();
        }
예제 #2
0
 public WebServer(ushort port, RequestServicer serv)
     : this(IPAddress.Any, port, serv)
 {
 }