예제 #1
0
        /// <summary>
        /// Establishes a connection to a remote host that is specified by an IP address and
        /// a port number.
        /// </summary>
        /// <param name="remoteaddr">The IP address of the remote host</param>
        /// <param name="port">The port number of the remote host</param>
        /// <param name="secret">Secret string to use for connection</param>
        public void Connect(IPAddress remoteaddr, int port, string secret)
        {
            _innerSocket.Connect(new IPEndPoint(remoteaddr, port));

            if (!string.IsNullOrWhiteSpace(secret))
            {
                using NetworkStream stream = CreateNetworkStream();
                if (!Authenticator.AuthenticateAsClient(stream, secret))
                {
                    throw new Exception($"Failed to authenticate for port: {port}.");
                }
            }
        }