コード例 #1
0
        void Open(string hostname, int port, bool secure)
        {
            this.hostname = hostname;
            IPAddress[] addresses = Dns.GetHostAddresses(hostname);
            if (addresses.Length == 0)
            {
                throw new Exception("Cant resolv '" + hostname + "'");
            }
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            socket.Connect(addresses[0], port);
            networkStream = new NetStream(socket, true);
            if (secure)
            {
                sslStream = new SslStream(networkStream, true, RemoteCertificateValidation, LocalCertificateSelection);
                sslStream.AuthenticateAsClient(hostname);
                stream = sslStream;
            }
            else
            {
                stream = networkStream;
            }
            bufferContext.Stream = stream;
        }
コード例 #2
0
        public HttpServerClient(HttpServer server, Socket socket)
        {
            this.server         = server;
            this.socket         = socket;
            this.socket.NoDelay = true;
            this.socket.SetSocketOption(SocketOptionLevel.Tcp, SocketOptionName.NoDelay, 1);

            requestCounter     = 0;
            stream             = new NetStream(socket, true);
            localEndPoint      = socket.LocalEndPoint;
            remoteEndPoint     = socket.RemoteEndPoint;
            KeepAliveCountdown = 100;
            KeepAliveTimeout   = 10;

            // handle HTTPS
            if (server.ServerCertificate != null)
            {
                SslStream sslStream = new SslStream(stream, true);
                sslStream.ReadTimeout  = 0;
                sslStream.WriteTimeout = 0;
                sslStream.AuthenticateAsServer(
                    server.ServerCertificate, false, SslProtocols.Tls, false);
                dataStream = sslStream;
            }
            else
            {
                dataStream = stream;
            }

            bufferContext             = new BufferContext();
            bufferContext.Offset      = 0;
            bufferContext.Count       = 0;
            bufferContext.ReadCounter = 0;
            bufferContext.Buffer      = new byte[4096];
            bufferContext.Stream      = dataStream;
        }