コード例 #1
0
        internal EndPointListener(IPEndPoint endpoint, bool secure, string certificateFolderPath,
#if !NETCF || BCC || SSL
                                  ServerSslConfiguration sslConfig,
#endif
                                  bool reuseAddress,
#if SSHARP
                                  EthernetAdapterType adapter,
#endif
                                  Logger logger)
        {
            _logger = logger;

#if !NETCF || BCC || SSL
            if (secure)
            {
                var cert = getCertificate(endpoint.Port, certificateFolderPath, sslConfig.ServerCertificate);

                if (cert == null)
                {
                    throw new ArgumentException("No server certificate could be found.");
                }

                _secure    = true;
                _sslConfig =
                    new ServerSslConfiguration(
                        cert,
                        sslConfig.ClientCertificateRequired,
                        sslConfig.EnabledSslProtocols,
                        sslConfig.CheckCertificateRevocation
                        );

                _sslConfig.ClientCertificateValidationCallback =
                    sslConfig.ClientCertificateValidationCallback;
            }
#endif

            _endpoint         = endpoint;
            _prefixes         = new Dictionary <HttpListenerPrefix, HttpListener> ();
            _unregistered     = new Dictionary <HttpConnection, HttpConnection> ();
            _unregisteredSync = ((ICollection)_unregistered).SyncRoot;
#if SSHARP
            _socket = new CrestronListenerSocket(IPAddress.Any, endpoint.Port, 16, adapter);
#else
            _socket = new Socket(address.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
            if (reuseAddress)
            {
                _socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, true);
            }
#endif

            _socket.Bind(endpoint);
            _socket.Listen(500);
            _socket.BeginAccept(onAccept, this);
        }
コード例 #2
0
        private Socket InitDataConnection()
#endif
        {
            FtpStatus status;

            if (usePassive)
            {
                status = SendCommand(PassiveCommand);
                if (status.StatusCode != FtpStatusCode.EnteringPassive)
                {
                    throw CreateExceptionFromResponse(status);
                }

                return(SetupPassiveConnection(status.StatusDescription));
            }

            // Open a socket to listen the server's connection
#if SSHARP
            CrestronListenerSocket sock = new CrestronListenerSocket(IPAddress.Any, 0);
#else
            Socket sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
#endif
            try
            {
                sock.Bind(new IPEndPoint(localEndPoint.Address, 0));
                sock.Listen(1);                  // We only expect a connection from server
            }
            catch (SocketException e)
            {
                sock.Close();

                throw new WebException("Couldn't open listening socket on client", e);
            }

            IPEndPoint ep       = (IPEndPoint)sock.LocalEndPoint;
            string     ipString = ep.Address.ToString().Replace('.', ',');
            int        h1       = ep.Port >> 8; // ep.Port / 256
            int        h2       = ep.Port % 256;

            string portParam = ipString + "," + h1 + "," + h2;
            status = SendCommand(PortCommand, portParam);

            if (status.StatusCode != FtpStatusCode.CommandOK)
            {
                sock.Close();
                throw (CreateExceptionFromResponse(status));
            }

            return(sock);
        }