当前位置: 首页>>代码示例>>C#>>正文


C# SecureSocket.Shutdown方法代码示例

本文整理汇总了C#中Org.Mentalis.Security.Ssl.SecureSocket.Shutdown方法的典型用法代码示例。如果您正苦于以下问题:C# SecureSocket.Shutdown方法的具体用法?C# SecureSocket.Shutdown怎么用?C# SecureSocket.Shutdown使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Org.Mentalis.Security.Ssl.SecureSocket的用法示例。


在下文中一共展示了SecureSocket.Shutdown方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Start

 public void Start()
 {
     try {
         // ask the user for SMTP server, port and the type of connection
         Console.Write("Host to connect to: ");
         string server = Console.ReadLine().Trim();
         Console.Write("Port: ");
         string port = Console.ReadLine().Trim();
         Console.Write("Connection type [0 = normal connection, 1 = direct TLS connection, 2 = indirect TLS connection (using STARTTLS command)]: ");
         string type = Console.ReadLine().Trim();
         if (Array.IndexOf(new string[]{"0", "1", "2"}, type) == -1) {
             Console.WriteLine("Invalid connection type.");
             return;
         }
         Console.WriteLine("Please enter the email address you wish to send an email to:");
         string address = Console.ReadLine().Trim();
         // create a new SecurityOptions instance
         SecurityOptions options = new SecurityOptions(SecureProtocol.None);
         // allow only secure ciphers to be used. Currently, the seure ciphers are:
         // the AES algorithm [128 or 256 bit keys], the RC4 algorithm [128 bit keys]
         // or TipleDES [168 bit keys]
         options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS;
         // the SecureSocket should be a client socket
         options.Entity = ConnectionEnd.Client;
         // we will use manual verification of the server certificate
         options.VerificationType = CredentialVerification.Manual;
         // when the server certificate is receives, the SecureSocket should call
         // the OnVerify method
         options.Verifier = new CertVerifyEventHandler(OnVerify);
         // use the default flags
         options.Flags = SecurityFlags.Default;
         // the common name is the domain name or IP address of the server
         options.CommonName = server;
         // if the user chose a direct TLS connection, set the protocol to TLS1
         if (type == "1") {
             options.Protocol = SecureProtocol.Tls1;
         }
         // create the new secure socket
         Connection = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
         // connect to the SMTP server
         Connection.Connect(new IPEndPoint(Dns.GetHostEntry(server).AddressList[0], int.Parse(port)));
         // wait for the server hello message
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("Server disallowed connection.");
             return;
         }
         // if the user selected an indirect TLS connection, issue
         // a EHLO command. Otherwise, stick to the standard HELO command.
         if (type == "2") // STARTTLS connection
             Send("EHLO SmtpClient.Mentalis.org\r\n");
         else
             Send("HELO SmtpClient.Mentalis.org\r\n");
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("HELLO failed.");
             return;
         }
         // if the user selected an indirect TLS connection, issue
         // the STARTTLS command
         if (type == "2") {
             // send the STARTTLS command to the server
             Send("STARTTLS\r\n");
             // make sure the server supports the STARTTLS command
             if (!IsReplyOK(Receive())) {
                 Console.WriteLine("STARTTLS failed.");
                 return;
             }
             // change the protocol from SecureProtocol.None
             // to SecureProtocol.Tls1
             options.Protocol = SecureProtocol.Tls1;
             // start the TLS handshake
             Connection.ChangeSecurityProtocol(options);
             // after this line, we're using an encrypted TLS connection
         }
         // from here on, act as if the SecureSocket is a normal Socket
         Send("MAIL FROM: [email protected]\r\n"); // [email protected] is not a real email address
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("MAIL FROM failed.");
             return;
         }
         Send("RCPT TO:" + address + "\r\n");
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("RCPT TO failed.");
             return;
         }
         Send("DATA\r\n");
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("DATA failed.");
             return;
         }
         Send(TestMail.Replace("#ADDRESS#", address) + "\r\n.\r\n");
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("Sending of e-mail failed.");
             return;
         }
         Send("QUIT\r\n");
         if (!IsReplyOK(Receive())) {
             Console.WriteLine("QUIT failed.");
         }
         Connection.Shutdown(SocketShutdown.Both);
     } catch (Exception e) {
//.........这里部分代码省略.........
开发者ID:maikgreubel,项目名称:securitylibrary,代码行数:101,代码来源:SmtpClient.cs

示例2: DownloadFile

 private void DownloadFile(Url url, int choice)
 {
     SecurityOptions options = new SecurityOptions(SecureProtocol.None);;
     m_Socket = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
     // connect to the FTP server using a normal TCP connection
     m_Socket.Connect(new IPEndPoint(Dns.GetHostEntry(url.Host).AddressList[0], url.Port));
     // wait for the server hello
     ReceiveReply();
     // if the user selected to use the AUTH command..
     if (choice == 2) {
         // ..send the command to the server and start the SSL handshake
         DoAuthCommand(url.Host);
     }
     // log on and quit
     if (!SendCommand("USER " + url.Username))
         return;
     if (!SendCommand("PASS " + url.Password))
         return;
     if (!SendCommand("QUIT"))
         return;
     // clean up
     m_Socket.Shutdown(SocketShutdown.Both);
     m_Socket.Close();
 }
开发者ID:maikgreubel,项目名称:securitylibrary,代码行数:24,代码来源:FtpClient.cs

示例3: DownloadFile

 /// <summary>
 /// Download a file using the synchronous Socket methods.
 /// </summary>
 /// <param name="url">The URL to download.
 /// </param>
 /// <param name="sp">The protocol to use.</param>
 protected void DownloadFile(Url url, SecureProtocol sp)
 {
     string request = GetHttpRequest(url); // holds the HTTP request for the given URL
     SecureSocket s;
     try {
         // First we create a new SecurityOptions instance
         // SecurityOptions objects hold information about the security
         // protocols the SecureSocket should use and how the SecureSocket
         // should react in certain situations.
         SecurityOptions options = new SecurityOptions(sp);
         // The Certificate field holds a certificate associated with
         // a client [you, for instance]. Because client certificates
         // are not often used for HTTP over SSL or TLS, we'll simply
         // set this field to a null reference.
         options.Certificate = null;
         // The Entity specifies whether the SecureSocket should act
         // as a client socket or as a server socket. In this case,
         // it should act as a client socket.
         options.Entity = ConnectionEnd.Client;
         // The CommonName field specifies the name of the remote
         // party we're connecting to. This is usually the domain name
         // of the remote host.
         options.CommonName = url.Host;
         // The VerificationType field specifies how we intend to verify
         // the certificate. In this case, we tell the SecureSocket that
         // we will manually verify the certificate. Look in the documentation
         // for other options.
         options.VerificationType = CredentialVerification.Manual;
         // When specifying the CredentialVerification.Manual option, we
         // must also specify a CertVerifyEventHandler delegate that will
         // be called when the SecureSocket receives the remote certificate.
         // The Verifier field holds this delegate. If no manual verification
         // is done, this field can be set to a null reference.
         options.Verifier = new CertVerifyEventHandler(OnVerify);
         // The Flags field specifies which flags should be used for the
         // connection. In this case, we will simply use the default behavior.
         options.Flags = SecurityFlags.Default;
         // Allow only secure ciphers to be used. If the server only supports
         // weak encryption, the connections will be shut down.
         options.AllowedAlgorithms = SslAlgorithms.SECURE_CIPHERS;
         // create a new SecureSocket instance and initialize it with
         // the security options we specified above.
         s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
         // connect to the remote host
         s.Connect(new IPEndPoint(Dns.GetHostEntry(url.Host).AddressList[0], url.Port));
     } catch (Exception e) {
         Console.WriteLine("Exception occurred while connecting: " + e.ToString());
         return;
     }
     // send the HTTP request to the remote host
     Console.Write("HTTP Query string:\r\n------------------\r\n" + request);
     try {
         byte[] reqBytes = Encoding.ASCII.GetBytes(request);
         int sent = s.Send(reqBytes, 0, reqBytes.Length, SocketFlags.None);
         while(sent != reqBytes.Length) {
             sent += s.Send(reqBytes, sent, reqBytes.Length - sent, SocketFlags.None);
         }
     } catch (Exception e) {
         Console.WriteLine("Exception occurred while sending: " + e.ToString());
         return;
     }
     // receive the reply
     Console.WriteLine("HTTP Server reply:\r\n------------------");
     try {
         byte[] buffer = new byte[4096];
         int ret = s.Receive(buffer);
         while(ret != 0) {
             Console.Write(Encoding.ASCII.GetString(buffer, 0, ret));
             ret = s.Receive(buffer);
         }
     } catch (Exception e) {
         Console.WriteLine("Exception occurred while receiving: " + e.ToString());
         return;
     }
     try {
         s.Shutdown(SocketShutdown.Both); // shut down the TCP connection
     } catch (Exception e) {
         Console.WriteLine("Exception occurred while shutting the connection down: " + e.ToString());
         return;
     }
     s.Close();
 }
开发者ID:maikgreubel,项目名称:securitylibrary,代码行数:88,代码来源:WebClient.cs


注:本文中的Org.Mentalis.Security.Ssl.SecureSocket.Shutdown方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。