本文整理汇总了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...");
}
}