當前位置: 首頁>>代碼示例>>C#>>正文


C# SecureSocket.Accept方法代碼示例

本文整理匯總了C#中Org.Mentalis.Security.Ssl.SecureSocket.Accept方法的典型用法代碼示例。如果您正苦於以下問題:C# SecureSocket.Accept方法的具體用法?C# SecureSocket.Accept怎麽用?C# SecureSocket.Accept使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Org.Mentalis.Security.Ssl.SecureSocket的用法示例。


在下文中一共展示了SecureSocket.Accept方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: StartServer

 /// <summary>
 /// Starts listening for incoming server connections.
 /// </summary>
 /// <param name="ep">The EndPoint on which to listen.</param>
 /// <param name="sp">The protocol to use.</param>
 /// <param name="pfxfile">An optional PFX file.</param>
 /// <param name="password">An optional PFX password.</param>
 public void StartServer(IPEndPoint ep, SecureProtocol sp, Certificate cert)
 {
     // initialize a SecurityOptions instance
     SecurityOptions options = new SecurityOptions(sp, cert, ConnectionEnd.Server);
     // create a new SecureSocket with the above security options
     SecureSocket s = new SecureSocket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp, options);
     // from here on, act as if the SecureSocket is a normal Socket
     s.Bind(ep);
     s.Listen(10);
     Console.WriteLine("Listening on " + s.LocalEndPoint.ToString());
     SecureSocket ss;
     string query = "";
     byte[] buffer = new byte[1024];
     int ret;
     while(true) {
         ss = (SecureSocket)s.Accept();
         Console.WriteLine("Incoming socket accepted.");
         // receive HTTP query
         Console.WriteLine("Receiving HTTP request...");
         ret = 0;
         query = "";
         while(!IsComplete(query)) { // wait until we've received the entire HTTP query
             try {
                 ret = ss.Receive(buffer, 0, buffer.Length, SocketFlags.None);
             } catch (Exception e) {
                 Console.WriteLine("Error while receiving data from client [" + e.Message + "].");
                 Console.WriteLine(e);
                 break;
             }
             if (ret == 0) {
                 Console.WriteLine("Client closed connection too soon.");
                 ss.Close();
                 break;
             }
             query += Encoding.ASCII.GetString(buffer, 0, ret);
         }
         if (IsComplete(query)) {
             // Send HTTP reply
             Console.WriteLine("Sending reply...");
             ret = 0;
             try {
                 while(ret != MentalisPage.Length) {
                     ret += ss.Send(Encoding.ASCII.GetBytes(MentalisPage), ret, MentalisPage.Length - ret, SocketFlags.None);
                 }
                 ss.Shutdown(SocketShutdown.Both);
                 ss.Close();
             } catch (Exception e) {
                 Console.WriteLine("Error while sending data to the client [" + e.Message + "].");
                 Console.WriteLine(e);
             }
         }
         Console.WriteLine("Waiting for another connection...");
     }
 }
開發者ID:maikgreubel,項目名稱:securitylibrary,代碼行數:61,代碼來源:WebServer.cs


注:本文中的Org.Mentalis.Security.Ssl.SecureSocket.Accept方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。