Org.Mentalis.Security.Ssl.SecureSocket.Accept C# (CSharp) Method

Accept() public method

Creates a new SecureSocket to handle an incoming connection request.
The returned VirtualSocket can be cast to a SecureSocket if necessary.
An operating system error occurs while accessing the SecureSocket. The SecureSocket has been closed. Unable to create the credentials.
public Accept ( ) : VirtualSocket
return VirtualSocket
		public override VirtualSocket Accept() {
			return EndAccept(BeginAccept(null, null));
		}
		/// <summary>

Usage Example

Esempio n. 1
0
 /// <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...");
     }
 }